Skip to content

Commit

Permalink
Merge pull request #2092 from sugi/fix-ruby-boolean-handling
Browse files Browse the repository at this point in the history
ruby: Fix boolean convertion on json to model attribute.
  • Loading branch information
wing328 committed Feb 10, 2016
2 parents 3ba4cd1 + 0ea7ae1 commit b576bc3
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
when :Float
value.to_f
when :BOOLEAN
if value =~ /^(true|t|yes|y|1)$/i
if value.to_s =~ /^(true|t|yes|y|1)$/i
true
else
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def _deserialize(type, value)
when :Float
value.to_f
when :BOOLEAN
if value =~ /^(true|t|yes|y|1)$/i
if value.to_s =~ /^(true|t|yes|y|1)$/i
true
else
false
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/ruby/lib/petstore/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def _deserialize(type, value)
when :Float
value.to_f
when :BOOLEAN
if value =~ /^(true|t|yes|y|1)$/i
if value.to_s =~ /^(true|t|yes|y|1)$/i
true
else
false
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/ruby/lib/petstore/models/pet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def _deserialize(type, value)
when :Float
value.to_f
when :BOOLEAN
if value =~ /^(true|t|yes|y|1)$/i
if value.to_s =~ /^(true|t|yes|y|1)$/i
true
else
false
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/ruby/lib/petstore/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def _deserialize(type, value)
when :Float
value.to_f
when :BOOLEAN
if value =~ /^(true|t|yes|y|1)$/i
if value.to_s =~ /^(true|t|yes|y|1)$/i
true
else
false
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/ruby/lib/petstore/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def _deserialize(type, value)
when :Float
value.to_f
when :BOOLEAN
if value =~ /^(true|t|yes|y|1)$/i
if value.to_s =~ /^(true|t|yes|y|1)$/i
true
else
false
Expand Down
29 changes: 24 additions & 5 deletions samples/client/petstore/ruby/spec/base_object_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

class ArrayMapObject < Petstore::Category
attr_accessor :int_arr, :pet_arr, :int_map, :pet_map, :int_arr_map, :pet_arr_map
attr_accessor :int_arr, :pet_arr, :int_map, :pet_map, :int_arr_map, :pet_arr_map, :boolean_true_arr, :boolean_false_arr

def self.attribute_map
{
Expand All @@ -10,7 +10,9 @@ def self.attribute_map
:int_map => :int_map,
:pet_map => :pet_map,
:int_arr_map => :int_arr_map,
:pet_arr_map => :pet_arr_map
:pet_arr_map => :pet_arr_map,
:boolean_true_arr => :boolean_true_arr,
:boolean_false_arr => :boolean_false_arr,
}
end

Expand All @@ -21,7 +23,9 @@ def self.swagger_types
:int_map => :'Hash<String, Integer>',
:pet_map => :'Hash<String, Pet>',
:int_arr_map => :'Hash<String, Array<Integer>>',
:pet_arr_map => :'Hash<String, Array<Pet>>'
:pet_arr_map => :'Hash<String, Array<Pet>>',
:boolean_true_arr => :'Array<BOOLEAN>',
:boolean_false_arr => :'Array<BOOLEAN>',
}
end
end
Expand All @@ -37,7 +41,9 @@ def self.swagger_types
int_map: {'int' => 123},
pet_map: {'pet' => {name: 'Kitty'}},
int_arr_map: {'int_arr' => [123, 456]},
pet_arr_map: {'pet_arr' => [{name: 'Kitty'}]}
pet_arr_map: {'pet_arr' => [{name: 'Kitty'}]},
boolean_true_arr: [true, "true", "TruE", 1, "y", "yes", "1", "t", "T"],
boolean_false_arr: [false, "", 0, "0", "f", nil, "null"],
}
end

Expand Down Expand Up @@ -71,11 +77,24 @@ def self.swagger_types
pet = arr.first
pet.should be_a(Petstore::Pet)
pet.name.should == 'Kitty'

obj.boolean_true_arr.should be_a(Array)
obj.boolean_true_arr.each do |b|
b.should eq true
end

obj.boolean_false_arr.should be_a(Array)
obj.boolean_false_arr.each do |b|
b.should eq false
end
end

it 'works for #to_hash' do
obj.build_from_hash(data)
obj.to_hash.should == data
expect_data = data.dup
expect_data[:boolean_true_arr].map! {true}
expect_data[:boolean_false_arr].map! {false}
obj.to_hash.should == expect_data
end
end
end

0 comments on commit b576bc3

Please sign in to comment.