Skip to content

Commit 9272b3e

Browse files
Renaud Amarwing328
authored andcommitted
[Ruby] Fix "build_from_hash" function for Ruby enums (#6812)
* Modify build_from_hash to compare value against enum's value rather than const name * Updated Petstore sample
1 parent 16e7bc0 commit 9272b3e

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
# @param [String] The enum value in the form of the string
77
# @return [String] The enum value
88
def build_from_hash(value)
9-
consantValues = {{classname}}.constants.select{|c| c.to_s == value}
10-
raise "Invalid ENUM value #{value} for class #{{{classname}}}" if consantValues.empty?
9+
constantValues = {{classname}}.constants.select{|c| {{classname}}::const_get(c) == value}
10+
raise "Invalid ENUM value #{value} for class #{{{classname}}}" if constantValues.empty?
1111
value
1212
end
1313
end

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class EnumClass
2323
# @param [String] The enum value in the form of the string
2424
# @return [String] The enum value
2525
def build_from_hash(value)
26-
consantValues = EnumClass.constants.select{|c| c.to_s == value}
27-
raise "Invalid ENUM value #{value} for class #EnumClass" if consantValues.empty?
26+
constantValues = EnumClass.constants.select{|c| EnumClass::const_get(c) == value}
27+
raise "Invalid ENUM value #{value} for class #EnumClass" if constantValues.empty?
2828
value
2929
end
3030
end

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class OuterEnum
2323
# @param [String] The enum value in the form of the string
2424
# @return [String] The enum value
2525
def build_from_hash(value)
26-
consantValues = OuterEnum.constants.select{|c| c.to_s == value}
27-
raise "Invalid ENUM value #{value} for class #OuterEnum" if consantValues.empty?
26+
constantValues = OuterEnum.constants.select{|c| OuterEnum::const_get(c) == value}
27+
raise "Invalid ENUM value #{value} for class #OuterEnum" if constantValues.empty?
2828
value
2929
end
3030
end

0 commit comments

Comments
 (0)