mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-21 14:17:07 +00:00
Improve model class of ruby-client (#1670)
* Add Model.build_from_hash * Use Model.build_from_hash instead of Model.new.build_from_hash * Update sample for ruby-client * Update sample for openapi3 ruby-client
This commit is contained in:
committed by
William Cheng
parent
cacce1f707
commit
82fcde1d91
@@ -204,9 +204,7 @@ module {{moduleName}}
|
||||
end
|
||||
else
|
||||
# models, e.g. Pet
|
||||
{{moduleName}}.const_get(return_type).new.tap do |model|
|
||||
model.build_from_hash data
|
||||
end
|
||||
{{moduleName}}.const_get(return_type).build_from_hash(data)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,3 +1,10 @@
|
||||
# Builds the object from hash
|
||||
# @param [Hash] attributes Model attributes in the form of hash
|
||||
# @return [Object] Returns the model itself
|
||||
def self.build_from_hash(attributes)
|
||||
new.build_from_hash(attributes)
|
||||
end
|
||||
|
||||
# Builds the object from hash
|
||||
# @param [Hash] attributes Model attributes in the form of hash
|
||||
# @return [Object] Returns the model itself
|
||||
@@ -58,8 +65,7 @@
|
||||
end
|
||||
end
|
||||
else # model
|
||||
temp_model = {{moduleName}}.const_get(type).new
|
||||
temp_model.build_from_hash(value)
|
||||
{{moduleName}}.const_get(type).build_from_hash(value)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -2,6 +2,13 @@
|
||||
{{#allowableValues}}{{#enumVars}}
|
||||
{{{name}}} = {{{value}}}.freeze{{/enumVars}}{{/allowableValues}}
|
||||
|
||||
# Builds the enum from string
|
||||
# @param [String] The enum value in the form of the string
|
||||
# @return [String] The enum value
|
||||
def self.build_from_hash(value)
|
||||
new.build_from_hash(value)
|
||||
end
|
||||
|
||||
# Builds the enum from string
|
||||
# @param [String] The enum value in the form of the string
|
||||
# @return [String] The enum value
|
||||
@@ -10,4 +17,4 @@
|
||||
raise "Invalid ENUM value #{value} for class #{{{classname}}}" if constantValues.empty?
|
||||
value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -239,8 +239,7 @@
|
||||
{{#-first}}
|
||||
_any_of_found = false
|
||||
openapi_any_of.each do |_class|
|
||||
_any_of = {{moduleName}}.const_get(_class).new
|
||||
_any_of.build_from_hash(self.to_hash)
|
||||
_any_of = {{moduleName}}.const_get(_class).build_from_hash(self.to_hash)
|
||||
if _any_of.valid?
|
||||
_any_of_found = true
|
||||
end
|
||||
@@ -256,8 +255,7 @@
|
||||
{{#-first}}
|
||||
_one_of_found = false
|
||||
openapi_one_of.each do |_class|
|
||||
_one_of = {{moduleName}}.const_get(_class).new
|
||||
_one_of.build_from_hash(self.to_hash)
|
||||
_one_of = {{moduleName}}.const_get(_class).build_from_hash(self.to_hash)
|
||||
if _one_of.valid?
|
||||
if _one_of_found?
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user