From 940e76f45f2ae82ac1f7b09707a93e39b46ae4a1 Mon Sep 17 00:00:00 2001 From: xhh Date: Fri, 23 Oct 2015 17:08:12 +0800 Subject: [PATCH] Ruby client: include empty arrays in model serialization --- .../src/main/resources/ruby/base_object.mustache | 13 ++++++------- .../ruby/lib/petstore/models/base_object.rb | 13 ++++++------- .../client/petstore/ruby/spec/api_client_spec.rb | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache b/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache index 77b320d07f4..733537cb14e 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache @@ -60,14 +60,13 @@ module {{moduleName}} # return the object in the form of hash def to_hash hash = {} - self.class.attribute_map.each_pair do |key, value| - if self.send(key).is_a?(Array) - next if self.send(key).empty? - hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? + self.class.attribute_map.each_pair do |attr, param| + value = self.send(attr) + next if value.nil? + if value.is_a?(Array) + hash[param] = value.compact.map{ |v| _to_hash(v) } else - unless (_tmp_value = _to_hash self.send(key)).nil? - hash[value] = _tmp_value - end + hash[param] = value end end hash diff --git a/samples/client/petstore/ruby/lib/petstore/models/base_object.rb b/samples/client/petstore/ruby/lib/petstore/models/base_object.rb index f17eef349e2..d5cc842164a 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/base_object.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/base_object.rb @@ -60,14 +60,13 @@ module Petstore # return the object in the form of hash def to_hash hash = {} - self.class.attribute_map.each_pair do |key, value| - if self.send(key).is_a?(Array) - next if self.send(key).empty? - hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? + self.class.attribute_map.each_pair do |attr, param| + value = self.send(attr) + next if value.nil? + if value.is_a?(Array) + hash[param] = value.compact.map{ |v| _to_hash(v) } else - unless (_tmp_value = _to_hash self.send(key)).nil? - hash[value] = _tmp_value - end + hash[param] = value end end hash diff --git a/samples/client/petstore/ruby/spec/api_client_spec.rb b/samples/client/petstore/ruby/spec/api_client_spec.rb index b63ec94ccaa..cd9016c9a0b 100644 --- a/samples/client/petstore/ruby/spec/api_client_spec.rb +++ b/samples/client/petstore/ruby/spec/api_client_spec.rb @@ -102,4 +102,18 @@ describe Petstore::ApiClient do end end + describe "#object_to_hash" do + it "ignores nils and includes empty arrays" do + api_client = Petstore::ApiClient.new + pet = Petstore::Pet.new + pet.id = 1 + pet.name = '' + pet.status = nil + pet.photo_urls = nil + pet.tags = [] + expected = {id: 1, name: '', tags: []} + api_client.object_to_hash(pet).should == expected + end + end + end