forked from loafle/openapi-generator-original
Merge pull request #1772 from xhh/ruby-inline-base-object
[Ruby] Remove base_object from Ruby model
This commit is contained in:
commit
c5ccb3f8eb
@ -127,7 +127,6 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
supportingFiles.add(new SupportingFile("configuration.mustache", gemFolder, "configuration.rb"));
|
supportingFiles.add(new SupportingFile("configuration.mustache", gemFolder, "configuration.rb"));
|
||||||
supportingFiles.add(new SupportingFile("version.mustache", gemFolder, "version.rb"));
|
supportingFiles.add(new SupportingFile("version.mustache", gemFolder, "version.rb"));
|
||||||
String modelFolder = gemFolder + File.separator + modelPackage.replace("/", File.separator);
|
String modelFolder = gemFolder + File.separator + modelPackage.replace("/", File.separator);
|
||||||
supportingFiles.add(new SupportingFile("base_object.mustache", modelFolder, "base_object.rb"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,9 +1,3 @@
|
|||||||
require 'date'
|
|
||||||
|
|
||||||
module {{moduleName}}
|
|
||||||
# base class containing fundamental method such as to_hash, build_from_hash and more
|
|
||||||
class BaseObject
|
|
||||||
|
|
||||||
# build the object from hash
|
# build the object from hash
|
||||||
def build_from_hash(attributes)
|
def build_from_hash(attributes)
|
||||||
return nil unless attributes.is_a?(Hash)
|
return nil unless attributes.is_a?(Hash)
|
||||||
@ -94,6 +88,3 @@ module {{moduleName}}
|
|||||||
value
|
value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
@ -5,7 +5,6 @@ require '{{gemName}}/version'
|
|||||||
require '{{gemName}}/configuration'
|
require '{{gemName}}/configuration'
|
||||||
|
|
||||||
# Models
|
# Models
|
||||||
require '{{gemName}}/{{modelPackage}}/base_object'
|
|
||||||
{{#models}}
|
{{#models}}
|
||||||
require '{{importPath}}'
|
require '{{importPath}}'
|
||||||
{{/models}}
|
{{/models}}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
|
require 'date'
|
||||||
|
|
||||||
module {{moduleName}}{{#models}}{{#model}}{{#description}}
|
module {{moduleName}}{{#models}}{{#model}}{{#description}}
|
||||||
# {{{description}}}{{/description}}
|
# {{{description}}}{{/description}}
|
||||||
class {{classname}} < BaseObject{{#vars}}{{#description}}
|
class {{classname}}{{#vars}}{{#description}}
|
||||||
# {{{description}}}{{/description}}
|
# {{{description}}}{{/description}}
|
||||||
attr_accessor :{{{name}}}
|
attr_accessor :{{{name}}}
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
@ -64,6 +66,8 @@ module {{moduleName}}{{#models}}{{#model}}{{#description}}
|
|||||||
def hash
|
def hash
|
||||||
[{{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}}].hash
|
[{{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}}].hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
{{> base_object}}
|
||||||
end
|
end
|
||||||
{{/model}}
|
{{/model}}
|
||||||
{{/models}}
|
{{/models}}
|
||||||
|
@ -5,7 +5,6 @@ require 'petstore/version'
|
|||||||
require 'petstore/configuration'
|
require 'petstore/configuration'
|
||||||
|
|
||||||
# Models
|
# Models
|
||||||
require 'petstore/models/base_object'
|
|
||||||
require 'petstore/models/user'
|
require 'petstore/models/user'
|
||||||
require 'petstore/models/category'
|
require 'petstore/models/category'
|
||||||
require 'petstore/models/pet'
|
require 'petstore/models/pet'
|
||||||
|
@ -1,99 +0,0 @@
|
|||||||
require 'date'
|
|
||||||
|
|
||||||
module Petstore
|
|
||||||
# base class containing fundamental method such as to_hash, build_from_hash and more
|
|
||||||
class BaseObject
|
|
||||||
|
|
||||||
# build the object from hash
|
|
||||||
def build_from_hash(attributes)
|
|
||||||
return nil unless attributes.is_a?(Hash)
|
|
||||||
self.class.swagger_types.each_pair do |key, type|
|
|
||||||
if type =~ /^Array<(.*)>/i
|
|
||||||
if attributes[self.class.attribute_map[key]].is_a?(Array)
|
|
||||||
self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
|
|
||||||
else
|
|
||||||
#TODO show warning in debug mode
|
|
||||||
end
|
|
||||||
elsif !attributes[self.class.attribute_map[key]].nil?
|
|
||||||
self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
|
|
||||||
else
|
|
||||||
# data not found in attributes(hash), not an issue as the data can be optional
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
self
|
|
||||||
end
|
|
||||||
|
|
||||||
def _deserialize(type, value)
|
|
||||||
case type.to_sym
|
|
||||||
when :DateTime
|
|
||||||
DateTime.parse(value)
|
|
||||||
when :Date
|
|
||||||
Date.parse(value)
|
|
||||||
when :String
|
|
||||||
value.to_s
|
|
||||||
when :Integer
|
|
||||||
value.to_i
|
|
||||||
when :Float
|
|
||||||
value.to_f
|
|
||||||
when :BOOLEAN
|
|
||||||
if value =~ /^(true|t|yes|y|1)$/i
|
|
||||||
true
|
|
||||||
else
|
|
||||||
false
|
|
||||||
end
|
|
||||||
when /\AArray<(?<inner_type>.+)>\z/
|
|
||||||
inner_type = Regexp.last_match[:inner_type]
|
|
||||||
value.map { |v| _deserialize(inner_type, v) }
|
|
||||||
when /\AHash<(?<k_type>.+), (?<v_type>.+)>\z/
|
|
||||||
k_type = Regexp.last_match[:k_type]
|
|
||||||
v_type = Regexp.last_match[:v_type]
|
|
||||||
{}.tap do |hash|
|
|
||||||
value.each do |k, v|
|
|
||||||
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else # model
|
|
||||||
_model = Petstore.const_get(type).new
|
|
||||||
_model.build_from_hash(value)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_s
|
|
||||||
to_hash.to_s
|
|
||||||
end
|
|
||||||
|
|
||||||
# to_body is an alias to to_body (backward compatibility))
|
|
||||||
def to_body
|
|
||||||
to_hash
|
|
||||||
end
|
|
||||||
|
|
||||||
# return the object in the form of hash
|
|
||||||
def to_hash
|
|
||||||
hash = {}
|
|
||||||
self.class.attribute_map.each_pair do |attr, param|
|
|
||||||
value = self.send(attr)
|
|
||||||
next if value.nil?
|
|
||||||
hash[param] = _to_hash(value)
|
|
||||||
end
|
|
||||||
hash
|
|
||||||
end
|
|
||||||
|
|
||||||
# Method to output non-array value in the form of hash
|
|
||||||
# For object, use to_hash. Otherwise, just return the value
|
|
||||||
def _to_hash(value)
|
|
||||||
if value.is_a?(Array)
|
|
||||||
value.compact.map{ |v| _to_hash(v) }
|
|
||||||
elsif value.is_a?(Hash)
|
|
||||||
{}.tap do |hash|
|
|
||||||
value.each { |k, v| hash[k] = _to_hash(v) }
|
|
||||||
end
|
|
||||||
elsif value.respond_to? :to_hash
|
|
||||||
value.to_hash
|
|
||||||
else
|
|
||||||
value
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
@ -1,5 +1,7 @@
|
|||||||
|
require 'date'
|
||||||
|
|
||||||
module Petstore
|
module Petstore
|
||||||
class Category < BaseObject
|
class Category
|
||||||
attr_accessor :id
|
attr_accessor :id
|
||||||
|
|
||||||
attr_accessor :name
|
attr_accessor :name
|
||||||
@ -58,5 +60,97 @@ module Petstore
|
|||||||
def hash
|
def hash
|
||||||
[id, name].hash
|
[id, name].hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# build the object from hash
|
||||||
|
def build_from_hash(attributes)
|
||||||
|
return nil unless attributes.is_a?(Hash)
|
||||||
|
self.class.swagger_types.each_pair do |key, type|
|
||||||
|
if type =~ /^Array<(.*)>/i
|
||||||
|
if attributes[self.class.attribute_map[key]].is_a?(Array)
|
||||||
|
self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
|
||||||
|
else
|
||||||
|
#TODO show warning in debug mode
|
||||||
|
end
|
||||||
|
elsif !attributes[self.class.attribute_map[key]].nil?
|
||||||
|
self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
|
||||||
|
else
|
||||||
|
# data not found in attributes(hash), not an issue as the data can be optional
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def _deserialize(type, value)
|
||||||
|
case type.to_sym
|
||||||
|
when :DateTime
|
||||||
|
DateTime.parse(value)
|
||||||
|
when :Date
|
||||||
|
Date.parse(value)
|
||||||
|
when :String
|
||||||
|
value.to_s
|
||||||
|
when :Integer
|
||||||
|
value.to_i
|
||||||
|
when :Float
|
||||||
|
value.to_f
|
||||||
|
when :BOOLEAN
|
||||||
|
if value =~ /^(true|t|yes|y|1)$/i
|
||||||
|
true
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
when /\AArray<(?<inner_type>.+)>\z/
|
||||||
|
inner_type = Regexp.last_match[:inner_type]
|
||||||
|
value.map { |v| _deserialize(inner_type, v) }
|
||||||
|
when /\AHash<(?<k_type>.+), (?<v_type>.+)>\z/
|
||||||
|
k_type = Regexp.last_match[:k_type]
|
||||||
|
v_type = Regexp.last_match[:v_type]
|
||||||
|
{}.tap do |hash|
|
||||||
|
value.each do |k, v|
|
||||||
|
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else # model
|
||||||
|
_model = Petstore.const_get(type).new
|
||||||
|
_model.build_from_hash(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
to_hash.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
# to_body is an alias to to_body (backward compatibility))
|
||||||
|
def to_body
|
||||||
|
to_hash
|
||||||
|
end
|
||||||
|
|
||||||
|
# return the object in the form of hash
|
||||||
|
def to_hash
|
||||||
|
hash = {}
|
||||||
|
self.class.attribute_map.each_pair do |attr, param|
|
||||||
|
value = self.send(attr)
|
||||||
|
next if value.nil?
|
||||||
|
hash[param] = _to_hash(value)
|
||||||
|
end
|
||||||
|
hash
|
||||||
|
end
|
||||||
|
|
||||||
|
# Method to output non-array value in the form of hash
|
||||||
|
# For object, use to_hash. Otherwise, just return the value
|
||||||
|
def _to_hash(value)
|
||||||
|
if value.is_a?(Array)
|
||||||
|
value.compact.map{ |v| _to_hash(v) }
|
||||||
|
elsif value.is_a?(Hash)
|
||||||
|
{}.tap do |hash|
|
||||||
|
value.each { |k, v| hash[k] = _to_hash(v) }
|
||||||
|
end
|
||||||
|
elsif value.respond_to? :to_hash
|
||||||
|
value.to_hash
|
||||||
|
else
|
||||||
|
value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
|
require 'date'
|
||||||
|
|
||||||
module Petstore
|
module Petstore
|
||||||
class Order < BaseObject
|
class Order
|
||||||
attr_accessor :id
|
attr_accessor :id
|
||||||
|
|
||||||
attr_accessor :pet_id
|
attr_accessor :pet_id
|
||||||
@ -108,5 +110,97 @@ module Petstore
|
|||||||
def hash
|
def hash
|
||||||
[id, pet_id, quantity, ship_date, status, complete].hash
|
[id, pet_id, quantity, ship_date, status, complete].hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# build the object from hash
|
||||||
|
def build_from_hash(attributes)
|
||||||
|
return nil unless attributes.is_a?(Hash)
|
||||||
|
self.class.swagger_types.each_pair do |key, type|
|
||||||
|
if type =~ /^Array<(.*)>/i
|
||||||
|
if attributes[self.class.attribute_map[key]].is_a?(Array)
|
||||||
|
self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
|
||||||
|
else
|
||||||
|
#TODO show warning in debug mode
|
||||||
|
end
|
||||||
|
elsif !attributes[self.class.attribute_map[key]].nil?
|
||||||
|
self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
|
||||||
|
else
|
||||||
|
# data not found in attributes(hash), not an issue as the data can be optional
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def _deserialize(type, value)
|
||||||
|
case type.to_sym
|
||||||
|
when :DateTime
|
||||||
|
DateTime.parse(value)
|
||||||
|
when :Date
|
||||||
|
Date.parse(value)
|
||||||
|
when :String
|
||||||
|
value.to_s
|
||||||
|
when :Integer
|
||||||
|
value.to_i
|
||||||
|
when :Float
|
||||||
|
value.to_f
|
||||||
|
when :BOOLEAN
|
||||||
|
if value =~ /^(true|t|yes|y|1)$/i
|
||||||
|
true
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
when /\AArray<(?<inner_type>.+)>\z/
|
||||||
|
inner_type = Regexp.last_match[:inner_type]
|
||||||
|
value.map { |v| _deserialize(inner_type, v) }
|
||||||
|
when /\AHash<(?<k_type>.+), (?<v_type>.+)>\z/
|
||||||
|
k_type = Regexp.last_match[:k_type]
|
||||||
|
v_type = Regexp.last_match[:v_type]
|
||||||
|
{}.tap do |hash|
|
||||||
|
value.each do |k, v|
|
||||||
|
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else # model
|
||||||
|
_model = Petstore.const_get(type).new
|
||||||
|
_model.build_from_hash(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
to_hash.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
# to_body is an alias to to_body (backward compatibility))
|
||||||
|
def to_body
|
||||||
|
to_hash
|
||||||
|
end
|
||||||
|
|
||||||
|
# return the object in the form of hash
|
||||||
|
def to_hash
|
||||||
|
hash = {}
|
||||||
|
self.class.attribute_map.each_pair do |attr, param|
|
||||||
|
value = self.send(attr)
|
||||||
|
next if value.nil?
|
||||||
|
hash[param] = _to_hash(value)
|
||||||
|
end
|
||||||
|
hash
|
||||||
|
end
|
||||||
|
|
||||||
|
# Method to output non-array value in the form of hash
|
||||||
|
# For object, use to_hash. Otherwise, just return the value
|
||||||
|
def _to_hash(value)
|
||||||
|
if value.is_a?(Array)
|
||||||
|
value.compact.map{ |v| _to_hash(v) }
|
||||||
|
elsif value.is_a?(Hash)
|
||||||
|
{}.tap do |hash|
|
||||||
|
value.each { |k, v| hash[k] = _to_hash(v) }
|
||||||
|
end
|
||||||
|
elsif value.respond_to? :to_hash
|
||||||
|
value.to_hash
|
||||||
|
else
|
||||||
|
value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
|
require 'date'
|
||||||
|
|
||||||
module Petstore
|
module Petstore
|
||||||
class Pet < BaseObject
|
class Pet
|
||||||
attr_accessor :id
|
attr_accessor :id
|
||||||
|
|
||||||
attr_accessor :category
|
attr_accessor :category
|
||||||
@ -112,5 +114,97 @@ module Petstore
|
|||||||
def hash
|
def hash
|
||||||
[id, category, name, photo_urls, tags, status].hash
|
[id, category, name, photo_urls, tags, status].hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# build the object from hash
|
||||||
|
def build_from_hash(attributes)
|
||||||
|
return nil unless attributes.is_a?(Hash)
|
||||||
|
self.class.swagger_types.each_pair do |key, type|
|
||||||
|
if type =~ /^Array<(.*)>/i
|
||||||
|
if attributes[self.class.attribute_map[key]].is_a?(Array)
|
||||||
|
self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
|
||||||
|
else
|
||||||
|
#TODO show warning in debug mode
|
||||||
|
end
|
||||||
|
elsif !attributes[self.class.attribute_map[key]].nil?
|
||||||
|
self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
|
||||||
|
else
|
||||||
|
# data not found in attributes(hash), not an issue as the data can be optional
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def _deserialize(type, value)
|
||||||
|
case type.to_sym
|
||||||
|
when :DateTime
|
||||||
|
DateTime.parse(value)
|
||||||
|
when :Date
|
||||||
|
Date.parse(value)
|
||||||
|
when :String
|
||||||
|
value.to_s
|
||||||
|
when :Integer
|
||||||
|
value.to_i
|
||||||
|
when :Float
|
||||||
|
value.to_f
|
||||||
|
when :BOOLEAN
|
||||||
|
if value =~ /^(true|t|yes|y|1)$/i
|
||||||
|
true
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
when /\AArray<(?<inner_type>.+)>\z/
|
||||||
|
inner_type = Regexp.last_match[:inner_type]
|
||||||
|
value.map { |v| _deserialize(inner_type, v) }
|
||||||
|
when /\AHash<(?<k_type>.+), (?<v_type>.+)>\z/
|
||||||
|
k_type = Regexp.last_match[:k_type]
|
||||||
|
v_type = Regexp.last_match[:v_type]
|
||||||
|
{}.tap do |hash|
|
||||||
|
value.each do |k, v|
|
||||||
|
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else # model
|
||||||
|
_model = Petstore.const_get(type).new
|
||||||
|
_model.build_from_hash(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
to_hash.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
# to_body is an alias to to_body (backward compatibility))
|
||||||
|
def to_body
|
||||||
|
to_hash
|
||||||
|
end
|
||||||
|
|
||||||
|
# return the object in the form of hash
|
||||||
|
def to_hash
|
||||||
|
hash = {}
|
||||||
|
self.class.attribute_map.each_pair do |attr, param|
|
||||||
|
value = self.send(attr)
|
||||||
|
next if value.nil?
|
||||||
|
hash[param] = _to_hash(value)
|
||||||
|
end
|
||||||
|
hash
|
||||||
|
end
|
||||||
|
|
||||||
|
# Method to output non-array value in the form of hash
|
||||||
|
# For object, use to_hash. Otherwise, just return the value
|
||||||
|
def _to_hash(value)
|
||||||
|
if value.is_a?(Array)
|
||||||
|
value.compact.map{ |v| _to_hash(v) }
|
||||||
|
elsif value.is_a?(Hash)
|
||||||
|
{}.tap do |hash|
|
||||||
|
value.each { |k, v| hash[k] = _to_hash(v) }
|
||||||
|
end
|
||||||
|
elsif value.respond_to? :to_hash
|
||||||
|
value.to_hash
|
||||||
|
else
|
||||||
|
value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
|
require 'date'
|
||||||
|
|
||||||
module Petstore
|
module Petstore
|
||||||
class Tag < BaseObject
|
class Tag
|
||||||
attr_accessor :id
|
attr_accessor :id
|
||||||
|
|
||||||
attr_accessor :name
|
attr_accessor :name
|
||||||
@ -58,5 +60,97 @@ module Petstore
|
|||||||
def hash
|
def hash
|
||||||
[id, name].hash
|
[id, name].hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# build the object from hash
|
||||||
|
def build_from_hash(attributes)
|
||||||
|
return nil unless attributes.is_a?(Hash)
|
||||||
|
self.class.swagger_types.each_pair do |key, type|
|
||||||
|
if type =~ /^Array<(.*)>/i
|
||||||
|
if attributes[self.class.attribute_map[key]].is_a?(Array)
|
||||||
|
self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
|
||||||
|
else
|
||||||
|
#TODO show warning in debug mode
|
||||||
|
end
|
||||||
|
elsif !attributes[self.class.attribute_map[key]].nil?
|
||||||
|
self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
|
||||||
|
else
|
||||||
|
# data not found in attributes(hash), not an issue as the data can be optional
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def _deserialize(type, value)
|
||||||
|
case type.to_sym
|
||||||
|
when :DateTime
|
||||||
|
DateTime.parse(value)
|
||||||
|
when :Date
|
||||||
|
Date.parse(value)
|
||||||
|
when :String
|
||||||
|
value.to_s
|
||||||
|
when :Integer
|
||||||
|
value.to_i
|
||||||
|
when :Float
|
||||||
|
value.to_f
|
||||||
|
when :BOOLEAN
|
||||||
|
if value =~ /^(true|t|yes|y|1)$/i
|
||||||
|
true
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
when /\AArray<(?<inner_type>.+)>\z/
|
||||||
|
inner_type = Regexp.last_match[:inner_type]
|
||||||
|
value.map { |v| _deserialize(inner_type, v) }
|
||||||
|
when /\AHash<(?<k_type>.+), (?<v_type>.+)>\z/
|
||||||
|
k_type = Regexp.last_match[:k_type]
|
||||||
|
v_type = Regexp.last_match[:v_type]
|
||||||
|
{}.tap do |hash|
|
||||||
|
value.each do |k, v|
|
||||||
|
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else # model
|
||||||
|
_model = Petstore.const_get(type).new
|
||||||
|
_model.build_from_hash(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
to_hash.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
# to_body is an alias to to_body (backward compatibility))
|
||||||
|
def to_body
|
||||||
|
to_hash
|
||||||
|
end
|
||||||
|
|
||||||
|
# return the object in the form of hash
|
||||||
|
def to_hash
|
||||||
|
hash = {}
|
||||||
|
self.class.attribute_map.each_pair do |attr, param|
|
||||||
|
value = self.send(attr)
|
||||||
|
next if value.nil?
|
||||||
|
hash[param] = _to_hash(value)
|
||||||
|
end
|
||||||
|
hash
|
||||||
|
end
|
||||||
|
|
||||||
|
# Method to output non-array value in the form of hash
|
||||||
|
# For object, use to_hash. Otherwise, just return the value
|
||||||
|
def _to_hash(value)
|
||||||
|
if value.is_a?(Array)
|
||||||
|
value.compact.map{ |v| _to_hash(v) }
|
||||||
|
elsif value.is_a?(Hash)
|
||||||
|
{}.tap do |hash|
|
||||||
|
value.each { |k, v| hash[k] = _to_hash(v) }
|
||||||
|
end
|
||||||
|
elsif value.respond_to? :to_hash
|
||||||
|
value.to_hash
|
||||||
|
else
|
||||||
|
value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
|
require 'date'
|
||||||
|
|
||||||
module Petstore
|
module Petstore
|
||||||
class User < BaseObject
|
class User
|
||||||
attr_accessor :id
|
attr_accessor :id
|
||||||
|
|
||||||
attr_accessor :username
|
attr_accessor :username
|
||||||
@ -119,5 +121,97 @@ module Petstore
|
|||||||
def hash
|
def hash
|
||||||
[id, username, first_name, last_name, email, password, phone, user_status].hash
|
[id, username, first_name, last_name, email, password, phone, user_status].hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# build the object from hash
|
||||||
|
def build_from_hash(attributes)
|
||||||
|
return nil unless attributes.is_a?(Hash)
|
||||||
|
self.class.swagger_types.each_pair do |key, type|
|
||||||
|
if type =~ /^Array<(.*)>/i
|
||||||
|
if attributes[self.class.attribute_map[key]].is_a?(Array)
|
||||||
|
self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
|
||||||
|
else
|
||||||
|
#TODO show warning in debug mode
|
||||||
|
end
|
||||||
|
elsif !attributes[self.class.attribute_map[key]].nil?
|
||||||
|
self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
|
||||||
|
else
|
||||||
|
# data not found in attributes(hash), not an issue as the data can be optional
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def _deserialize(type, value)
|
||||||
|
case type.to_sym
|
||||||
|
when :DateTime
|
||||||
|
DateTime.parse(value)
|
||||||
|
when :Date
|
||||||
|
Date.parse(value)
|
||||||
|
when :String
|
||||||
|
value.to_s
|
||||||
|
when :Integer
|
||||||
|
value.to_i
|
||||||
|
when :Float
|
||||||
|
value.to_f
|
||||||
|
when :BOOLEAN
|
||||||
|
if value =~ /^(true|t|yes|y|1)$/i
|
||||||
|
true
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
when /\AArray<(?<inner_type>.+)>\z/
|
||||||
|
inner_type = Regexp.last_match[:inner_type]
|
||||||
|
value.map { |v| _deserialize(inner_type, v) }
|
||||||
|
when /\AHash<(?<k_type>.+), (?<v_type>.+)>\z/
|
||||||
|
k_type = Regexp.last_match[:k_type]
|
||||||
|
v_type = Regexp.last_match[:v_type]
|
||||||
|
{}.tap do |hash|
|
||||||
|
value.each do |k, v|
|
||||||
|
hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else # model
|
||||||
|
_model = Petstore.const_get(type).new
|
||||||
|
_model.build_from_hash(value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
to_hash.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
# to_body is an alias to to_body (backward compatibility))
|
||||||
|
def to_body
|
||||||
|
to_hash
|
||||||
|
end
|
||||||
|
|
||||||
|
# return the object in the form of hash
|
||||||
|
def to_hash
|
||||||
|
hash = {}
|
||||||
|
self.class.attribute_map.each_pair do |attr, param|
|
||||||
|
value = self.send(attr)
|
||||||
|
next if value.nil?
|
||||||
|
hash[param] = _to_hash(value)
|
||||||
|
end
|
||||||
|
hash
|
||||||
|
end
|
||||||
|
|
||||||
|
# Method to output non-array value in the form of hash
|
||||||
|
# For object, use to_hash. Otherwise, just return the value
|
||||||
|
def _to_hash(value)
|
||||||
|
if value.is_a?(Array)
|
||||||
|
value.compact.map{ |v| _to_hash(v) }
|
||||||
|
elsif value.is_a?(Hash)
|
||||||
|
{}.tap do |hash|
|
||||||
|
value.each { |k, v| hash[k] = _to_hash(v) }
|
||||||
|
end
|
||||||
|
elsif value.respond_to? :to_hash
|
||||||
|
value.to_hash
|
||||||
|
else
|
||||||
|
value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
class ArrayMapObject < Petstore::BaseObject
|
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
|
||||||
|
|
||||||
def self.attribute_map
|
def self.attribute_map
|
||||||
@ -27,7 +27,7 @@ class ArrayMapObject < Petstore::BaseObject
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
describe Petstore::BaseObject do
|
describe 'BaseObject' do
|
||||||
describe 'array and map properties' do
|
describe 'array and map properties' do
|
||||||
let(:obj) { ArrayMapObject.new }
|
let(:obj) { ArrayMapObject.new }
|
||||||
|
|
||||||
@ -78,4 +78,4 @@ describe Petstore::BaseObject do
|
|||||||
obj.to_hash.should == data
|
obj.to_hash.should == data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user