File tree Expand file tree Collapse file tree 3 files changed +26
-14
lines changed
modules/swagger-codegen/src/main/resources/ruby
samples/client/petstore/ruby Expand file tree Collapse file tree 3 files changed +26
-14
lines changed Original file line number Diff line number Diff line change @@ -60,14 +60,13 @@ module {{moduleName}}
60
60
# return the object in the form of hash
61
61
def to_hash
62
62
hash = { }
63
- self.class.attribute_map.each_pair do |key, value|
64
- if self.send(key).is_a?(Array)
65
- next if self.send(key).empty?
66
- hash[value] = self.send(key).select{ |v| ! v.nil?} .map{ |v| _to_hash v} unless self.send(key).nil?
63
+ self.class.attribute_map.each_pair do |attr, param|
64
+ value = self.send(attr)
65
+ next if value.nil?
66
+ if value.is_a?(Array)
67
+ hash[param] = value.compact.map{ |v| _to_hash(v) }
67
68
else
68
- unless (_tmp_value = _to_hash self.send(key)).nil?
69
- hash[value] = _tmp_value
70
- end
69
+ hash[param] = _to_hash(value)
71
70
end
72
71
end
73
72
hash
Original file line number Diff line number Diff line change @@ -60,14 +60,13 @@ def to_body
60
60
# return the object in the form of hash
61
61
def to_hash
62
62
hash = { }
63
- self . class . attribute_map . each_pair do |key , value |
64
- if self . send ( key ) . is_a? ( Array )
65
- next if self . send ( key ) . empty?
66
- hash [ value ] = self . send ( key ) . select { |v | !v . nil? } . map { |v | _to_hash v } unless self . send ( key ) . nil?
63
+ self . class . attribute_map . each_pair do |attr , param |
64
+ value = self . send ( attr )
65
+ next if value . nil?
66
+ if value . is_a? ( Array )
67
+ hash [ param ] = value . compact . map { |v | _to_hash ( v ) }
67
68
else
68
- unless ( _tmp_value = _to_hash self . send ( key ) ) . nil?
69
- hash [ value ] = _tmp_value
70
- end
69
+ hash [ param ] = _to_hash ( value )
71
70
end
72
71
end
73
72
hash
Original file line number Diff line number Diff line change 102
102
end
103
103
end
104
104
105
+ describe "#object_to_hash" do
106
+ it "ignores nils and includes empty arrays" do
107
+ api_client = Petstore ::ApiClient . new
108
+ pet = Petstore ::Pet . new
109
+ pet . id = 1
110
+ pet . name = ''
111
+ pet . status = nil
112
+ pet . photo_urls = nil
113
+ pet . tags = [ ]
114
+ expected = { id : 1 , name : '' , tags : [ ] }
115
+ api_client . object_to_hash ( pet ) . should == expected
116
+ end
117
+ end
118
+
105
119
end
You can’t perform that action at this time.
0 commit comments