forked from loafle/openapi-generator-original
Ruby client: include empty arrays in model serialization
This commit is contained in:
parent
a6e4203cd5
commit
940e76f45f
@ -60,14 +60,13 @@ module {{moduleName}}
|
|||||||
# return the object in the form of hash
|
# return the object in the form of hash
|
||||||
def to_hash
|
def to_hash
|
||||||
hash = {}
|
hash = {}
|
||||||
self.class.attribute_map.each_pair do |key, value|
|
self.class.attribute_map.each_pair do |attr, param|
|
||||||
if self.send(key).is_a?(Array)
|
value = self.send(attr)
|
||||||
next if self.send(key).empty?
|
next if value.nil?
|
||||||
hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil?
|
if value.is_a?(Array)
|
||||||
|
hash[param] = value.compact.map{ |v| _to_hash(v) }
|
||||||
else
|
else
|
||||||
unless (_tmp_value = _to_hash self.send(key)).nil?
|
hash[param] = value
|
||||||
hash[value] = _tmp_value
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
hash
|
hash
|
||||||
|
@ -60,14 +60,13 @@ module Petstore
|
|||||||
# return the object in the form of hash
|
# return the object in the form of hash
|
||||||
def to_hash
|
def to_hash
|
||||||
hash = {}
|
hash = {}
|
||||||
self.class.attribute_map.each_pair do |key, value|
|
self.class.attribute_map.each_pair do |attr, param|
|
||||||
if self.send(key).is_a?(Array)
|
value = self.send(attr)
|
||||||
next if self.send(key).empty?
|
next if value.nil?
|
||||||
hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil?
|
if value.is_a?(Array)
|
||||||
|
hash[param] = value.compact.map{ |v| _to_hash(v) }
|
||||||
else
|
else
|
||||||
unless (_tmp_value = _to_hash self.send(key)).nil?
|
hash[param] = value
|
||||||
hash[value] = _tmp_value
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
hash
|
hash
|
||||||
|
@ -102,4 +102,18 @@ describe Petstore::ApiClient do
|
|||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user