Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 1f9f940

Browse files
authored
[Ruby] Change constants lookup methods in enum class (#13285)
* [Ruby] Add missing block ends * [Ruby] Change constants lookup methods in enum class * Update ruby samples
1 parent d75b7b6 commit 1f9f940

20 files changed

+115
-57
lines changed

modules/openapi-generator/src/main/resources/ruby-client/partial_model_enum_class.mustache

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
{{{name}}} = {{{value}}}.freeze{{/enumVars}}
33

44
{{/allowableValues}}
5+
def self.all_vars
6+
@all_vars ||= [{{#allowableValues}}{{#enumVars}}{{{name}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}].freeze
7+
end
8+
59
# Builds the enum from string
610
# @param [String] The enum value in the form of the string
711
# @return [String] The enum value
@@ -13,8 +17,7 @@
1317
# @param [String] The enum value in the form of the string
1418
# @return [String] The enum value
1519
def build_from_hash(value)
16-
constantValues = {{classname}}.constants.select { |c| {{classname}}::const_get(c) == value }
17-
raise "Invalid ENUM value #{value} for class #{{{classname}}}" if constantValues.empty?
18-
value
20+
return value if {{classname}}.all_vars.include?(value)
21+
raise "Invalid ENUM value #{value} for class #{{{classname}}}"
1922
end
2023
end

modules/openapi-generator/src/main/resources/ruby-client/partial_oneof_module_doc.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ require '{{{gemName}}}'
6060
# }
6161
{{/-last}}
6262
{{/mappedModels}}
63+
```
6364
{{/discriminator}}
6465

6566
### build

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class EnumClass
1919
EFG = "-efg".freeze
2020
XYZ = "(xyz)".freeze
2121

22+
def self.all_vars
23+
@all_vars ||= [ABC, EFG, XYZ].freeze
24+
end
25+
2226
# Builds the enum from string
2327
# @param [String] The enum value in the form of the string
2428
# @return [String] The enum value
@@ -30,9 +34,8 @@ def self.build_from_hash(value)
3034
# @param [String] The enum value in the form of the string
3135
# @return [String] The enum value
3236
def build_from_hash(value)
33-
constantValues = EnumClass.constants.select { |c| EnumClass::const_get(c) == value }
34-
raise "Invalid ENUM value #{value} for class #EnumClass" if constantValues.empty?
35-
value
37+
return value if EnumClass.all_vars.include?(value)
38+
raise "Invalid ENUM value #{value} for class #EnumClass"
3639
end
3740
end
3841
end

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class OuterEnum
1919
APPROVED = "approved".freeze
2020
DELIVERED = "delivered".freeze
2121

22+
def self.all_vars
23+
@all_vars ||= [PLACED, APPROVED, DELIVERED].freeze
24+
end
25+
2226
# Builds the enum from string
2327
# @param [String] The enum value in the form of the string
2428
# @return [String] The enum value
@@ -30,9 +34,8 @@ def self.build_from_hash(value)
3034
# @param [String] The enum value in the form of the string
3135
# @return [String] The enum value
3236
def build_from_hash(value)
33-
constantValues = OuterEnum.constants.select { |c| OuterEnum::const_get(c) == value }
34-
raise "Invalid ENUM value #{value} for class #OuterEnum" if constantValues.empty?
35-
value
37+
return value if OuterEnum.all_vars.include?(value)
38+
raise "Invalid ENUM value #{value} for class #OuterEnum"
3639
end
3740
end
3841
end

samples/client/petstore/ruby-autoload/lib/petstore/models/outer_enum_default_value.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class OuterEnumDefaultValue
1919
APPROVED = "approved".freeze
2020
DELIVERED = "delivered".freeze
2121

22+
def self.all_vars
23+
@all_vars ||= [PLACED, APPROVED, DELIVERED].freeze
24+
end
25+
2226
# Builds the enum from string
2327
# @param [String] The enum value in the form of the string
2428
# @return [String] The enum value
@@ -30,9 +34,8 @@ def self.build_from_hash(value)
3034
# @param [String] The enum value in the form of the string
3135
# @return [String] The enum value
3236
def build_from_hash(value)
33-
constantValues = OuterEnumDefaultValue.constants.select { |c| OuterEnumDefaultValue::const_get(c) == value }
34-
raise "Invalid ENUM value #{value} for class #OuterEnumDefaultValue" if constantValues.empty?
35-
value
37+
return value if OuterEnumDefaultValue.all_vars.include?(value)
38+
raise "Invalid ENUM value #{value} for class #OuterEnumDefaultValue"
3639
end
3740
end
3841
end

samples/client/petstore/ruby-autoload/lib/petstore/models/outer_enum_integer.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class OuterEnumInteger
1919
N1 = 1.freeze
2020
N2 = 2.freeze
2121

22+
def self.all_vars
23+
@all_vars ||= [N0, N1, N2].freeze
24+
end
25+
2226
# Builds the enum from string
2327
# @param [String] The enum value in the form of the string
2428
# @return [String] The enum value
@@ -30,9 +34,8 @@ def self.build_from_hash(value)
3034
# @param [String] The enum value in the form of the string
3135
# @return [String] The enum value
3236
def build_from_hash(value)
33-
constantValues = OuterEnumInteger.constants.select { |c| OuterEnumInteger::const_get(c) == value }
34-
raise "Invalid ENUM value #{value} for class #OuterEnumInteger" if constantValues.empty?
35-
value
37+
return value if OuterEnumInteger.all_vars.include?(value)
38+
raise "Invalid ENUM value #{value} for class #OuterEnumInteger"
3639
end
3740
end
3841
end

samples/client/petstore/ruby-autoload/lib/petstore/models/outer_enum_integer_default_value.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class OuterEnumIntegerDefaultValue
1919
N1 = 1.freeze
2020
N2 = 2.freeze
2121

22+
def self.all_vars
23+
@all_vars ||= [N0, N1, N2].freeze
24+
end
25+
2226
# Builds the enum from string
2327
# @param [String] The enum value in the form of the string
2428
# @return [String] The enum value
@@ -30,9 +34,8 @@ def self.build_from_hash(value)
3034
# @param [String] The enum value in the form of the string
3135
# @return [String] The enum value
3236
def build_from_hash(value)
33-
constantValues = OuterEnumIntegerDefaultValue.constants.select { |c| OuterEnumIntegerDefaultValue::const_get(c) == value }
34-
raise "Invalid ENUM value #{value} for class #OuterEnumIntegerDefaultValue" if constantValues.empty?
35-
value
37+
return value if OuterEnumIntegerDefaultValue.all_vars.include?(value)
38+
raise "Invalid ENUM value #{value} for class #OuterEnumIntegerDefaultValue"
3639
end
3740
end
3841
end

samples/client/petstore/ruby-autoload/lib/petstore/models/single_ref_type.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class SingleRefType
1818
ADMIN = "admin".freeze
1919
USER = "user".freeze
2020

21+
def self.all_vars
22+
@all_vars ||= [ADMIN, USER].freeze
23+
end
24+
2125
# Builds the enum from string
2226
# @param [String] The enum value in the form of the string
2327
# @return [String] The enum value
@@ -29,9 +33,8 @@ def self.build_from_hash(value)
2933
# @param [String] The enum value in the form of the string
3034
# @return [String] The enum value
3135
def build_from_hash(value)
32-
constantValues = SingleRefType.constants.select { |c| SingleRefType::const_get(c) == value }
33-
raise "Invalid ENUM value #{value} for class #SingleRefType" if constantValues.empty?
34-
value
36+
return value if SingleRefType.all_vars.include?(value)
37+
raise "Invalid ENUM value #{value} for class #SingleRefType"
3538
end
3639
end
3740
end

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class EnumClass
1919
EFG = "-efg".freeze
2020
XYZ = "(xyz)".freeze
2121

22+
def self.all_vars
23+
@all_vars ||= [ABC, EFG, XYZ].freeze
24+
end
25+
2226
# Builds the enum from string
2327
# @param [String] The enum value in the form of the string
2428
# @return [String] The enum value
@@ -30,9 +34,8 @@ def self.build_from_hash(value)
3034
# @param [String] The enum value in the form of the string
3135
# @return [String] The enum value
3236
def build_from_hash(value)
33-
constantValues = EnumClass.constants.select { |c| EnumClass::const_get(c) == value }
34-
raise "Invalid ENUM value #{value} for class #EnumClass" if constantValues.empty?
35-
value
37+
return value if EnumClass.all_vars.include?(value)
38+
raise "Invalid ENUM value #{value} for class #EnumClass"
3639
end
3740
end
3841
end

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class OuterEnum
1919
APPROVED = "approved".freeze
2020
DELIVERED = "delivered".freeze
2121

22+
def self.all_vars
23+
@all_vars ||= [PLACED, APPROVED, DELIVERED].freeze
24+
end
25+
2226
# Builds the enum from string
2327
# @param [String] The enum value in the form of the string
2428
# @return [String] The enum value
@@ -30,9 +34,8 @@ def self.build_from_hash(value)
3034
# @param [String] The enum value in the form of the string
3135
# @return [String] The enum value
3236
def build_from_hash(value)
33-
constantValues = OuterEnum.constants.select { |c| OuterEnum::const_get(c) == value }
34-
raise "Invalid ENUM value #{value} for class #OuterEnum" if constantValues.empty?
35-
value
37+
return value if OuterEnum.all_vars.include?(value)
38+
raise "Invalid ENUM value #{value} for class #OuterEnum"
3639
end
3740
end
3841
end

0 commit comments

Comments
 (0)