Skip to content

Commit b576bc3

Browse files
committed
Merge pull request #2092 from sugi/fix-ruby-boolean-handling
ruby: Fix boolean convertion on json to model attribute.
2 parents 3ba4cd1 + 0ea7ae1 commit b576bc3

File tree

7 files changed

+30
-11
lines changed

7 files changed

+30
-11
lines changed

modules/swagger-codegen/src/main/resources/ruby/base_object.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
when :Float
3232
value.to_f
3333
when :BOOLEAN
34-
if value =~ /^(true|t|yes|y|1)$/i
34+
if value.to_s =~ /^(true|t|yes|y|1)$/i
3535
true
3636
else
3737
false

samples/client/petstore/ruby/lib/petstore/models/category.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def _deserialize(type, value)
110110
when :Float
111111
value.to_f
112112
when :BOOLEAN
113-
if value =~ /^(true|t|yes|y|1)$/i
113+
if value.to_s =~ /^(true|t|yes|y|1)$/i
114114
true
115115
else
116116
false

samples/client/petstore/ruby/lib/petstore/models/order.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def _deserialize(type, value)
160160
when :Float
161161
value.to_f
162162
when :BOOLEAN
163-
if value =~ /^(true|t|yes|y|1)$/i
163+
if value.to_s =~ /^(true|t|yes|y|1)$/i
164164
true
165165
else
166166
false

samples/client/petstore/ruby/lib/petstore/models/pet.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _deserialize(type, value)
164164
when :Float
165165
value.to_f
166166
when :BOOLEAN
167-
if value =~ /^(true|t|yes|y|1)$/i
167+
if value.to_s =~ /^(true|t|yes|y|1)$/i
168168
true
169169
else
170170
false

samples/client/petstore/ruby/lib/petstore/models/tag.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def _deserialize(type, value)
110110
when :Float
111111
value.to_f
112112
when :BOOLEAN
113-
if value =~ /^(true|t|yes|y|1)$/i
113+
if value.to_s =~ /^(true|t|yes|y|1)$/i
114114
true
115115
else
116116
false

samples/client/petstore/ruby/lib/petstore/models/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def _deserialize(type, value)
171171
when :Float
172172
value.to_f
173173
when :BOOLEAN
174-
if value =~ /^(true|t|yes|y|1)$/i
174+
if value.to_s =~ /^(true|t|yes|y|1)$/i
175175
true
176176
else
177177
false

samples/client/petstore/ruby/spec/base_object_spec.rb

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22

33
class ArrayMapObject < Petstore::Category
4-
attr_accessor :int_arr, :pet_arr, :int_map, :pet_map, :int_arr_map, :pet_arr_map
4+
attr_accessor :int_arr, :pet_arr, :int_map, :pet_map, :int_arr_map, :pet_arr_map, :boolean_true_arr, :boolean_false_arr
55

66
def self.attribute_map
77
{
@@ -10,7 +10,9 @@ def self.attribute_map
1010
:int_map => :int_map,
1111
:pet_map => :pet_map,
1212
:int_arr_map => :int_arr_map,
13-
:pet_arr_map => :pet_arr_map
13+
:pet_arr_map => :pet_arr_map,
14+
:boolean_true_arr => :boolean_true_arr,
15+
:boolean_false_arr => :boolean_false_arr,
1416
}
1517
end
1618

@@ -21,7 +23,9 @@ def self.swagger_types
2123
:int_map => :'Hash<String, Integer>',
2224
:pet_map => :'Hash<String, Pet>',
2325
:int_arr_map => :'Hash<String, Array<Integer>>',
24-
:pet_arr_map => :'Hash<String, Array<Pet>>'
26+
:pet_arr_map => :'Hash<String, Array<Pet>>',
27+
:boolean_true_arr => :'Array<BOOLEAN>',
28+
:boolean_false_arr => :'Array<BOOLEAN>',
2529
}
2630
end
2731
end
@@ -37,7 +41,9 @@ def self.swagger_types
3741
int_map: {'int' => 123},
3842
pet_map: {'pet' => {name: 'Kitty'}},
3943
int_arr_map: {'int_arr' => [123, 456]},
40-
pet_arr_map: {'pet_arr' => [{name: 'Kitty'}]}
44+
pet_arr_map: {'pet_arr' => [{name: 'Kitty'}]},
45+
boolean_true_arr: [true, "true", "TruE", 1, "y", "yes", "1", "t", "T"],
46+
boolean_false_arr: [false, "", 0, "0", "f", nil, "null"],
4147
}
4248
end
4349

@@ -71,11 +77,24 @@ def self.swagger_types
7177
pet = arr.first
7278
pet.should be_a(Petstore::Pet)
7379
pet.name.should == 'Kitty'
80+
81+
obj.boolean_true_arr.should be_a(Array)
82+
obj.boolean_true_arr.each do |b|
83+
b.should eq true
84+
end
85+
86+
obj.boolean_false_arr.should be_a(Array)
87+
obj.boolean_false_arr.each do |b|
88+
b.should eq false
89+
end
7490
end
7591

7692
it 'works for #to_hash' do
7793
obj.build_from_hash(data)
78-
obj.to_hash.should == data
94+
expect_data = data.dup
95+
expect_data[:boolean_true_arr].map! {true}
96+
expect_data[:boolean_false_arr].map! {false}
97+
obj.to_hash.should == expect_data
7998
end
8099
end
81100
end

0 commit comments

Comments
 (0)