diff --git a/modules/openapi-generator/src/main/resources/ruby-nextgen/polymorphism.mustache b/modules/openapi-generator/src/main/resources/ruby-nextgen/polymorphism.mustache index cda9a2866e55..9537c2c177da 100644 --- a/modules/openapi-generator/src/main/resources/ruby-nextgen/polymorphism.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-nextgen/polymorphism.mustache @@ -29,6 +29,8 @@ module {{moduleName}} end def self.cast_raw(type_name, data) + return nil if data.nil? + case type_name when 'Boolean' [true, false].include?(data) ? data : NO_MATCH @@ -47,7 +49,7 @@ module {{moduleName}} when HASH_TYPE cast_hash(Regexp.last_match[:sub_type], data) else - data.nil? ? nil : cast_model(type_name, data) + cast_model(type_name, data) end end private_class_method :cast_raw diff --git a/samples/client/others/ruby-nextgen-qdrant/lib/qdrant/polymorphism.rb b/samples/client/others/ruby-nextgen-qdrant/lib/qdrant/polymorphism.rb index 427dcbda2a4c..aac4698fc31b 100644 --- a/samples/client/others/ruby-nextgen-qdrant/lib/qdrant/polymorphism.rb +++ b/samples/client/others/ruby-nextgen-qdrant/lib/qdrant/polymorphism.rb @@ -29,6 +29,8 @@ def self.cast(type_name, data) end def self.cast_raw(type_name, data) + return nil if data.nil? + case type_name when 'Boolean' [true, false].include?(data) ? data : NO_MATCH @@ -47,7 +49,7 @@ def self.cast_raw(type_name, data) when HASH_TYPE cast_hash(Regexp.last_match[:sub_type], data) else - data.nil? ? nil : cast_model(type_name, data) + cast_model(type_name, data) end end private_class_method :cast_raw diff --git a/samples/client/petstore/ruby-nextgen/lib/petstore/polymorphism.rb b/samples/client/petstore/ruby-nextgen/lib/petstore/polymorphism.rb index 8f18bf11fc31..eb11918a3742 100644 --- a/samples/client/petstore/ruby-nextgen/lib/petstore/polymorphism.rb +++ b/samples/client/petstore/ruby-nextgen/lib/petstore/polymorphism.rb @@ -29,6 +29,8 @@ def self.cast(type_name, data) end def self.cast_raw(type_name, data) + return nil if data.nil? + case type_name when 'Boolean' [true, false].include?(data) ? data : NO_MATCH @@ -47,7 +49,7 @@ def self.cast_raw(type_name, data) when HASH_TYPE cast_hash(Regexp.last_match[:sub_type], data) else - data.nil? ? nil : cast_model(type_name, data) + cast_model(type_name, data) end end private_class_method :cast_raw diff --git a/samples/client/petstore/ruby-nextgen/spec/polymorphism_spec.rb b/samples/client/petstore/ruby-nextgen/spec/polymorphism_spec.rb new file mode 100644 index 000000000000..b7090919e75f --- /dev/null +++ b/samples/client/petstore/ruby-nextgen/spec/polymorphism_spec.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Petstore::Polymorphism do + describe '.cast' do + it 'keeps nil array elements for primitive candidates' do + expect(described_class.cast('Array', [1, nil])).to eq([1, nil]) + expect(described_class.cast('Array', ['a', nil])).to eq(['a', nil]) + expect(described_class.cast('Array', [true, nil])).to eq([true, nil]) + end + + it 'keeps nil hash values for primitive candidates' do + expect(described_class.cast('Hash', { 'a' => 1, 'b' => nil })).to eq('a' => 1, 'b' => nil) + end + + it 'keeps nil array elements for model candidates' do + pet = { 'id' => 1, 'name' => 'Rex', 'photoUrls' => [] } + result = described_class.cast('Array', [pet, nil]) + expect(result.size).to eq(2) + expect(result[0]).to be_a(Petstore::Models::Pet) + expect(result[0].name).to eq('Rex') + expect(result[1]).to be_nil + end + + it 'still rejects elements that do not match the candidate' do + expect(described_class.cast('Array', [1, 'nope'])).to be_nil + expect(described_class.cast('Array', 'nope')).to be_nil + expect(described_class.cast('Hash', { 'a' => 'nope' })).to be_nil + expect(described_class.cast('Array', [{}, nil])).to be_nil + end + + it 'matches nil data on every candidate type' do + expect(described_class.cast('Integer', nil)).to be_nil + expect(described_class.cast('String', nil)).to be_nil + expect(described_class.cast('Pet', nil)).to be_nil + end + end +end