From 8710fbfde7a77cd10ab6289786065ee78c506b4c Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sat, 16 May 2015 03:51:21 +0800 Subject: [PATCH] add base object --- .../codegen/languages/RubyClientCodegen.java | 1 + .../main/resources/ruby/base_object.mustache | 83 ++++++++ .../src/main/resources/ruby/model.mustache | 120 ++++++++++-- .../petstore/ruby/lib/model/base_object.rb | 83 ++++++++ samples/client/petstore/ruby/lib/pet_api.rb | 24 ++- .../petstore/ruby/models/base_object.rb | 86 +++++++++ .../client/petstore/ruby/models/category.rb | 123 ++++++++++-- samples/client/petstore/ruby/models/order.rb | 159 +++++++++++++--- samples/client/petstore/ruby/models/pet.rb | 162 +++++++++++++--- samples/client/petstore/ruby/models/tag.rb | 123 ++++++++++-- samples/client/petstore/ruby/models/user.rb | 177 ++++++++++++++---- samples/client/petstore/ruby/spec/pet_spec.rb | 43 +++++ .../client/petstore/ruby/spec/spec_helper.rb | 5 +- 13 files changed, 1033 insertions(+), 156 deletions(-) create mode 100644 modules/swagger-codegen/src/main/resources/ruby/base_object.mustache create mode 100644 samples/client/petstore/ruby/lib/model/base_object.rb create mode 100644 samples/client/petstore/ruby/models/base_object.rb diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/RubyClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/RubyClientCodegen.java index 0c472a2b75d9..962f7ea692c1 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/RubyClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/RubyClientCodegen.java @@ -72,6 +72,7 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig { supportingFiles.add(new SupportingFile("swagger/response.mustache", "", "lib/swagger/response.rb")); supportingFiles.add(new SupportingFile("swagger/version.mustache", "", "lib/swagger/version.rb")); supportingFiles.add(new SupportingFile("swagger/configuration.mustache", "", "lib/swagger/configuration.rb")); + supportingFiles.add(new SupportingFile("base_object.mustache", "", "lib/model/base_object.rb")); } @Override diff --git a/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache b/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache new file mode 100644 index 000000000000..b8ff6dc3a355 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache @@ -0,0 +1,83 @@ +# base class containing fundamental method such as to_hash, build_from_hash and more +class BaseObject + + # return the object in the form of hash + def to_body + body = {} + self.class.attribute_map.each_pair do |key, value| + body[value] = self.send(key) unless self.send(key).nil? + end + body + 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 + when :DateTime + DateTime.parse(value) + when :string + value.to_s + when :int + value.to_i + when :double + value.to_f + when :boolean + if value =~ /^(true|t|yes|y|1)$/i + true + else + false + end + else # model + _model = Object.const_get(type).new + _model.build_from_hash(value) + end + end + + + + # to_body is an alias to to_body (backward compatibility) + def to_hash + hash = {} + self.class.attribute_map.each_pair do |key, value| + if self.send(key).is_a?(Array) + next if self.send(key).empty? + hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? + else + unless (_tmp_value = _to_hash self.send(key)).nil? + hash[value] = _tmp_value + end + end + 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.respond_to? :to_hash + value.to_hash + else + value + end + end + +end diff --git a/modules/swagger-codegen/src/main/resources/ruby/model.mustache b/modules/swagger-codegen/src/main/resources/ruby/model.mustache index 397a64f6471f..f3c1b85f2997 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/model.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/model.mustache @@ -1,35 +1,119 @@ -{{#models}} +require_relative 'base_object' -{{#model}} -class {{classname}} +{{#models}}#{{description}} +{{#model}}class {{classname}} < BaseObject attr_accessor {{#vars}}:{{{name}}}{{#hasMore}}, {{/hasMore}}{{/vars}}{{newline}} - # :internal => :external + # attribute mapping from ruby-style variable name to JSON key def self.attribute_map { - {{#vars}}:{{{name}}} => :'{{{baseName}}}'{{#hasMore}},{{/hasMore}} + {{#vars}} + # {{description}} + :'{{{name}}}' => :'{{{baseName}}}'{{#hasMore}},{{/hasMore}} + {{/vars}} + } + end + + # attribute type + def self.swagger_types + { + {{#vars}}:'{{{name}}}' => :'{{{datatype}}}'{{#hasMore}},{{/hasMore}} {{/vars}} } end def initialize(attributes = {}) - return if attributes.empty? - # Morph attribute keys into undescored rubyish style + return if !attributes.is_a?(Hash) || attributes.empty? + + # convert string to symbol for hash key + attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} + {{#vars}} - if self.class.attribute_map[:"{{{name}}}"] - {{#isContainer}}if (value = attributes["{{{baseName}}}"]).is_a?(Array) - @{{{name}}} = value{{#complexType}}.map{ |v| {{complexType}}.new(v) }{{/complexType}} - end{{/isContainer}}{{^isContainer}}@{{{name}}} = attributes["{{{baseName}}}"]{{/isContainer}} + if attributes[:'{{{baseName}}}'] + {{#isContainer}}if (value = attributes[:'{{{baseName}}}']).is_a?(Array) + @{{{name}}} = value + end{{/isContainer}}{{^isContainer}}@{{{name}}} = attributes[:'{{{baseName}}}']{{/isContainer}} end {{/vars}} end - def to_body - body = {} - self.class.attribute_map.each_pair do |key, value| - body[value] = self.send(key) unless self.send(key).nil? - end - body - end +# # return the object in the form of hash +# def to_body +# body = {} +# self.class.attribute_map.each_pair do |key, value| +# body[value] = self.send(key) unless self.send(key).nil? +# end +# body +# end +# +# # build the object from hash +# def build_from_hash(attributes) +# 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 +# when :DateTime +# DateTime.parse(value) +# when :string +# value.to_s +# when :int +# value.to_i +# when :double +# value.to_f +# when :boolean +# if value =~ /^(true|t|yes|y|1)$/i +# true +# else +# false +# end +# else # model +# _model = Object.const_get(type).new +# _model.build_from_hash(value) +# end +# end +# +# +# +# # to_body is an alias to to_body (backward compatibility) +# def to_hash +# hash = {} +# self.class.attribute_map.each_pair do |key, value| +# if self.send(key).is_a?(Array) +# next if self.send(key).empty? +# hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? +# else +# unless (_tmp_value = _to_hash self.send(key)).nil? +# hash[value] = _tmp_value +# end +# end +# 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.respond_to? :to_hash +# value.to_hash +# else +# value +# end +# end + end {{/model}} {{/models}} diff --git a/samples/client/petstore/ruby/lib/model/base_object.rb b/samples/client/petstore/ruby/lib/model/base_object.rb new file mode 100644 index 000000000000..b8ff6dc3a355 --- /dev/null +++ b/samples/client/petstore/ruby/lib/model/base_object.rb @@ -0,0 +1,83 @@ +# base class containing fundamental method such as to_hash, build_from_hash and more +class BaseObject + + # return the object in the form of hash + def to_body + body = {} + self.class.attribute_map.each_pair do |key, value| + body[value] = self.send(key) unless self.send(key).nil? + end + body + 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 + when :DateTime + DateTime.parse(value) + when :string + value.to_s + when :int + value.to_i + when :double + value.to_f + when :boolean + if value =~ /^(true|t|yes|y|1)$/i + true + else + false + end + else # model + _model = Object.const_get(type).new + _model.build_from_hash(value) + end + end + + + + # to_body is an alias to to_body (backward compatibility) + def to_hash + hash = {} + self.class.attribute_map.each_pair do |key, value| + if self.send(key).is_a?(Array) + next if self.send(key).empty? + hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? + else + unless (_tmp_value = _to_hash self.send(key)).nil? + hash[value] = _tmp_value + end + end + 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.respond_to? :to_hash + value.to_hash + else + value + end + end + +end diff --git a/samples/client/petstore/ruby/lib/pet_api.rb b/samples/client/petstore/ruby/lib/pet_api.rb index 96ddb14bd949..c00b605e6074 100644 --- a/samples/client/petstore/ruby/lib/pet_api.rb +++ b/samples/client/petstore/ruby/lib/pet_api.rb @@ -37,18 +37,24 @@ class PetApi _body_param = opts[:'body'] if _body_param != nil if _body_param.is_a?(Array) - _array = Array.new - _body_param.each do |item| - if item.respond_to?(:to_body) - _array.push item.to_body + #_array = Array.new + #_body_param.each do |item| + # if item.respond_to?(:to_body) + # _array.push item.to_body + # else + # _array.push item + # end + #end + post_body = _array.map{ |v| + if v.respond_to?(:to_hash) + v.to_hash else - _array.push item + v end - end - post_body = _array + } else - if _body_param.respond_to?(:to_body) - post_body = _body_param.to_body + if _body_param.respond_to?(:to_hash) + post_body = _body_param.to_hash else post_body = _body_param end diff --git a/samples/client/petstore/ruby/models/base_object.rb b/samples/client/petstore/ruby/models/base_object.rb new file mode 100644 index 000000000000..efa21abdfe4f --- /dev/null +++ b/samples/client/petstore/ruby/models/base_object.rb @@ -0,0 +1,86 @@ +require 'json' +# +class BaseObject + + # return the object in the form of hash + def to_body + body = {} + self.class.attribute_map.each_pair do |key, value| + body[value] = self.send(key) unless self.send(key).nil? + end + body + end + + # build the object from hash + def build_from_hash(attributes) + 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 + + # deserialize value based on type + def _deserialize(type, value) + case type + when :DateTime + DateTime.parse(value) + when :string + value.to_s + when :int + value.to_i + when :double + value.to_f + when :boolean + if value =~ /^(true|t|yes|y|1)$/i + true + else + false + end + else # model + _model = Object.const_get(type).new + _model.build_from_hash(value) + end + end + + #def to_json + # self.to_hash.to_json + #end + + # to_body is an alias to to_body (backward compatibility) + def to_hash + hash = {} + self.class.attribute_map.each_pair do |key, value| + if self.send(key).is_a?(Array) + next if self.send(key).empty? + hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? + else + unless (_tmp_value = _to_hash self.send(key)).nil? + hash[value] = _tmp_value + end + end + 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.respond_to? :to_hash + value.to_hash + else + value + end + end + +end diff --git a/samples/client/petstore/ruby/models/category.rb b/samples/client/petstore/ruby/models/category.rb index 64ca857e17e6..b8ef8b6106a3 100644 --- a/samples/client/petstore/ruby/models/category.rb +++ b/samples/client/petstore/ruby/models/category.rb @@ -1,34 +1,123 @@ +require_relative 'base_object' -class Category +# +class Category < BaseObject attr_accessor :id, :name - # :internal => :external + # attribute mapping from ruby-style variable name to JSON key def self.attribute_map { - :id => :'id', - :name => :'name' + + # + :'id' => :'id', + + # + :'name' => :'name' + + } + end + + # attribute type + def self.swagger_types + { + :'id' => :'int', + :'name' => :'string' } end def initialize(attributes = {}) - return if attributes.empty? - # Morph attribute keys into undescored rubyish style + return if !attributes.is_a?(Hash) || attributes.empty? + + # convert string to symbol for hash key + attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} + - if self.class.attribute_map[:"id"] - @id = attributes["id"] + if attributes[:'id'] + @id = attributes[:'id'] end - if self.class.attribute_map[:"name"] - @name = attributes["name"] + if attributes[:'name'] + @name = attributes[:'name'] end end - def to_body - body = {} - self.class.attribute_map.each_pair do |key, value| - body[value] = self.send(key) unless self.send(key).nil? - end - body - end +# # return the object in the form of hash +# def to_body +# body = {} +# self.class.attribute_map.each_pair do |key, value| +# body[value] = self.send(key) unless self.send(key).nil? +# end +# body +# end +# +# # build the object from hash +# def build_from_hash(attributes) +# 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 +# when :DateTime +# DateTime.parse(value) +# when :string +# value.to_s +# when :int +# value.to_i +# when :double +# value.to_f +# when :boolean +# if value =~ /^(true|t|yes|y|1)$/i +# true +# else +# false +# end +# else # model +# _model = Object.const_get(type).new +# _model.build_from_hash(value) +# end +# end +# +# +# +# # to_body is an alias to to_body (backward compatibility) +# def to_hash +# hash = {} +# self.class.attribute_map.each_pair do |key, value| +# if self.send(key).is_a?(Array) +# next if self.send(key).empty? +# hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? +# else +# unless (_tmp_value = _to_hash self.send(key)).nil? +# hash[value] = _tmp_value +# end +# end +# 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.respond_to? :to_hash +# value.to_hash +# else +# value +# end +# end + end diff --git a/samples/client/petstore/ruby/models/order.rb b/samples/client/petstore/ruby/models/order.rb index 5434fbd8e2d8..ade8fea251f7 100644 --- a/samples/client/petstore/ruby/models/order.rb +++ b/samples/client/petstore/ruby/models/order.rb @@ -1,54 +1,155 @@ +require_relative 'base_object' -class Order +# +class Order < BaseObject attr_accessor :id, :pet_id, :quantity, :ship_date, :status, :complete - # :internal => :external + # attribute mapping from ruby-style variable name to JSON key def self.attribute_map { - :id => :'id', - :pet_id => :'petId', - :quantity => :'quantity', - :ship_date => :'shipDate', - :status => :'status', - :complete => :'complete' + + # + :'id' => :'id', + + # + :'pet_id' => :'petId', + + # + :'quantity' => :'quantity', + + # + :'ship_date' => :'shipDate', + + # Order Status + :'status' => :'status', + + # + :'complete' => :'complete' + + } + end + + # attribute type + def self.swagger_types + { + :'id' => :'int', + :'pet_id' => :'int', + :'quantity' => :'int', + :'ship_date' => :'DateTime', + :'status' => :'string', + :'complete' => :'boolean' } end def initialize(attributes = {}) - return if attributes.empty? - # Morph attribute keys into undescored rubyish style + return if !attributes.is_a?(Hash) || attributes.empty? + + # convert string to symbol for hash key + attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} + - if self.class.attribute_map[:"id"] - @id = attributes["id"] + if attributes[:'id'] + @id = attributes[:'id'] end - if self.class.attribute_map[:"pet_id"] - @pet_id = attributes["petId"] + if attributes[:'petId'] + @pet_id = attributes[:'petId'] end - if self.class.attribute_map[:"quantity"] - @quantity = attributes["quantity"] + if attributes[:'quantity'] + @quantity = attributes[:'quantity'] end - if self.class.attribute_map[:"ship_date"] - @ship_date = attributes["shipDate"] + if attributes[:'shipDate'] + @ship_date = attributes[:'shipDate'] end - if self.class.attribute_map[:"status"] - @status = attributes["status"] + if attributes[:'status'] + @status = attributes[:'status'] end - if self.class.attribute_map[:"complete"] - @complete = attributes["complete"] + if attributes[:'complete'] + @complete = attributes[:'complete'] end end - def to_body - body = {} - self.class.attribute_map.each_pair do |key, value| - body[value] = self.send(key) unless self.send(key).nil? - end - body - end +# # return the object in the form of hash +# def to_body +# body = {} +# self.class.attribute_map.each_pair do |key, value| +# body[value] = self.send(key) unless self.send(key).nil? +# end +# body +# end +# +# # build the object from hash +# def build_from_hash(attributes) +# 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 +# when :DateTime +# DateTime.parse(value) +# when :string +# value.to_s +# when :int +# value.to_i +# when :double +# value.to_f +# when :boolean +# if value =~ /^(true|t|yes|y|1)$/i +# true +# else +# false +# end +# else # model +# _model = Object.const_get(type).new +# _model.build_from_hash(value) +# end +# end +# +# +# +# # to_body is an alias to to_body (backward compatibility) +# def to_hash +# hash = {} +# self.class.attribute_map.each_pair do |key, value| +# if self.send(key).is_a?(Array) +# next if self.send(key).empty? +# hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? +# else +# unless (_tmp_value = _to_hash self.send(key)).nil? +# hash[value] = _tmp_value +# end +# end +# 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.respond_to? :to_hash +# value.to_hash +# else +# value +# end +# end + end diff --git a/samples/client/petstore/ruby/models/pet.rb b/samples/client/petstore/ruby/models/pet.rb index 38767b46dd7a..472302daff8f 100644 --- a/samples/client/petstore/ruby/models/pet.rb +++ b/samples/client/petstore/ruby/models/pet.rb @@ -1,58 +1,160 @@ +require_relative 'base_object' -class Pet +# +class Pet < BaseObject attr_accessor :id, :category, :name, :photo_urls, :tags, :status - # :internal => :external + # attribute mapping from ruby-style variable name to JSON key def self.attribute_map { - :id => :'id', - :category => :'category', - :name => :'name', - :photo_urls => :'photoUrls', - :tags => :'tags', - :status => :'status' + + # + :'id' => :'id', + + # + :'category' => :'category', + + # + :'name' => :'name', + + # + :'photo_urls' => :'photoUrls', + + # + :'tags' => :'tags', + + # pet status in the store + :'status' => :'status' + + } + end + + # attribute type + def self.swagger_types + { + :'id' => :'int', + :'category' => :'Category', + :'name' => :'string', + :'photo_urls' => :'array[string]', + :'tags' => :'array[Tag]', + :'status' => :'string' } end def initialize(attributes = {}) - return if attributes.empty? - # Morph attribute keys into undescored rubyish style + return if !attributes.is_a?(Hash) || attributes.empty? + + # convert string to symbol for hash key + attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} + - if self.class.attribute_map[:"id"] - @id = attributes["id"] + if attributes[:'id'] + @id = attributes[:'id'] end - if self.class.attribute_map[:"category"] - @category = attributes["category"] + if attributes[:'category'] + @category = attributes[:'category'] end - if self.class.attribute_map[:"name"] - @name = attributes["name"] + if attributes[:'name'] + @name = attributes[:'name'] end - if self.class.attribute_map[:"photo_urls"] - if (value = attributes["photoUrls"]).is_a?(Array) + if attributes[:'photoUrls'] + if (value = attributes[:'photoUrls']).is_a?(Array) @photo_urls = value end end - if self.class.attribute_map[:"tags"] - if (value = attributes["tags"]).is_a?(Array) - @tags = value.map{ |v| Tag.new(v) } + if attributes[:'tags'] + if (value = attributes[:'tags']).is_a?(Array) + #@tags = value.map{ |v| Tag.new(v) } + @tags = value end end - if self.class.attribute_map[:"status"] - @status = attributes["status"] + if attributes[:'status'] + @status = attributes[:'status'] end end - def to_body - body = {} - self.class.attribute_map.each_pair do |key, value| - body[value] = self.send(key) unless self.send(key).nil? - end - body - end +# # return the object in the form of hash +# def to_body +# body = {} +# self.class.attribute_map.each_pair do |key, value| +# body[value] = self.send(key) unless self.send(key).nil? +# end +# body +# end +# +# # build the object from hash +# def build_from_hash(attributes) +# 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 +# when :DateTime +# DateTime.parse(value) +# when :string +# value.to_s +# when :int +# value.to_i +# when :double +# value.to_f +# when :boolean +# if value =~ /^(true|t|yes|y|1)$/i +# true +# else +# false +# end +# else # model +# _model = Object.const_get(type).new +# _model.build_from_hash(value) +# end +# end +# +# +# +# # to_body is an alias to to_body (backward compatibility) +# def to_hash +# hash = {} +# self.class.attribute_map.each_pair do |key, value| +# if self.send(key).is_a?(Array) +# next if self.send(key).empty? +# hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? +# else +# unless (_tmp_value = _to_hash self.send(key)).nil? +# hash[value] = _tmp_value +# end +# end +# 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.respond_to? :to_hash +# value.to_hash +# else +# value +# end +# end + end diff --git a/samples/client/petstore/ruby/models/tag.rb b/samples/client/petstore/ruby/models/tag.rb index 24e3d1d0706e..dc4611953e65 100644 --- a/samples/client/petstore/ruby/models/tag.rb +++ b/samples/client/petstore/ruby/models/tag.rb @@ -1,34 +1,123 @@ +require_relative 'base_object' -class Tag +# +class Tag < BaseObject attr_accessor :id, :name - # :internal => :external + # attribute mapping from ruby-style variable name to JSON key def self.attribute_map { - :id => :'id', - :name => :'name' + + # + :'id' => :'id', + + # + :'name' => :'name' + + } + end + + # attribute type + def self.swagger_types + { + :'id' => :'int', + :'name' => :'string' } end def initialize(attributes = {}) - return if attributes.empty? - # Morph attribute keys into undescored rubyish style + return if !attributes.is_a?(Hash) || attributes.empty? + + # convert string to symbol for hash key + attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} + - if self.class.attribute_map[:"id"] - @id = attributes["id"] + if attributes[:'id'] + @id = attributes[:'id'] end - if self.class.attribute_map[:"name"] - @name = attributes["name"] + if attributes[:'name'] + @name = attributes[:'name'] end end - def to_body - body = {} - self.class.attribute_map.each_pair do |key, value| - body[value] = self.send(key) unless self.send(key).nil? - end - body - end +# # return the object in the form of hash +# def to_body +# body = {} +# self.class.attribute_map.each_pair do |key, value| +# body[value] = self.send(key) unless self.send(key).nil? +# end +# body +# end +# +# # build the object from hash +# def build_from_hash(attributes) +# 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 +# when :DateTime +# DateTime.parse(value) +# when :string +# value.to_s +# when :int +# value.to_i +# when :double +# value.to_f +# when :boolean +# if value =~ /^(true|t|yes|y|1)$/i +# true +# else +# false +# end +# else # model +# _model = Object.const_get(type).new +# _model.build_from_hash(value) +# end +# end +# +# +# +# # to_body is an alias to to_body (backward compatibility) +# def to_hash +# hash = {} +# self.class.attribute_map.each_pair do |key, value| +# if self.send(key).is_a?(Array) +# next if self.send(key).empty? +# hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? +# else +# unless (_tmp_value = _to_hash self.send(key)).nil? +# hash[value] = _tmp_value +# end +# end +# 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.respond_to? :to_hash +# value.to_hash +# else +# value +# end +# end + end diff --git a/samples/client/petstore/ruby/models/user.rb b/samples/client/petstore/ruby/models/user.rb index e270392a3b89..376c5a20f0a0 100644 --- a/samples/client/petstore/ruby/models/user.rb +++ b/samples/client/petstore/ruby/models/user.rb @@ -1,64 +1,171 @@ +require_relative 'base_object' -class User +# +class User < BaseObject attr_accessor :id, :username, :first_name, :last_name, :email, :password, :phone, :user_status - # :internal => :external + # attribute mapping from ruby-style variable name to JSON key def self.attribute_map { - :id => :'id', - :username => :'username', - :first_name => :'firstName', - :last_name => :'lastName', - :email => :'email', - :password => :'password', - :phone => :'phone', - :user_status => :'userStatus' + + # + :'id' => :'id', + + # + :'username' => :'username', + + # + :'first_name' => :'firstName', + + # + :'last_name' => :'lastName', + + # + :'email' => :'email', + + # + :'password' => :'password', + + # + :'phone' => :'phone', + + # User Status + :'user_status' => :'userStatus' + + } + end + + # attribute type + def self.swagger_types + { + :'id' => :'int', + :'username' => :'string', + :'first_name' => :'string', + :'last_name' => :'string', + :'email' => :'string', + :'password' => :'string', + :'phone' => :'string', + :'user_status' => :'int' } end def initialize(attributes = {}) - return if attributes.empty? - # Morph attribute keys into undescored rubyish style + return if !attributes.is_a?(Hash) || attributes.empty? + + # convert string to symbol for hash key + attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} + - if self.class.attribute_map[:"id"] - @id = attributes["id"] + if attributes[:'id'] + @id = attributes[:'id'] end - if self.class.attribute_map[:"username"] - @username = attributes["username"] + if attributes[:'username'] + @username = attributes[:'username'] end - if self.class.attribute_map[:"first_name"] - @first_name = attributes["firstName"] + if attributes[:'firstName'] + @first_name = attributes[:'firstName'] end - if self.class.attribute_map[:"last_name"] - @last_name = attributes["lastName"] + if attributes[:'lastName'] + @last_name = attributes[:'lastName'] end - if self.class.attribute_map[:"email"] - @email = attributes["email"] + if attributes[:'email'] + @email = attributes[:'email'] end - if self.class.attribute_map[:"password"] - @password = attributes["password"] + if attributes[:'password'] + @password = attributes[:'password'] end - if self.class.attribute_map[:"phone"] - @phone = attributes["phone"] + if attributes[:'phone'] + @phone = attributes[:'phone'] end - if self.class.attribute_map[:"user_status"] - @user_status = attributes["userStatus"] + if attributes[:'userStatus'] + @user_status = attributes[:'userStatus'] end end - def to_body - body = {} - self.class.attribute_map.each_pair do |key, value| - body[value] = self.send(key) unless self.send(key).nil? - end - body - end +# # return the object in the form of hash +# def to_body +# body = {} +# self.class.attribute_map.each_pair do |key, value| +# body[value] = self.send(key) unless self.send(key).nil? +# end +# body +# end +# +# # build the object from hash +# def build_from_hash(attributes) +# 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 +# when :DateTime +# DateTime.parse(value) +# when :string +# value.to_s +# when :int +# value.to_i +# when :double +# value.to_f +# when :boolean +# if value =~ /^(true|t|yes|y|1)$/i +# true +# else +# false +# end +# else # model +# _model = Object.const_get(type).new +# _model.build_from_hash(value) +# end +# end +# +# +# +# # to_body is an alias to to_body (backward compatibility) +# def to_hash +# hash = {} +# self.class.attribute_map.each_pair do |key, value| +# if self.send(key).is_a?(Array) +# next if self.send(key).empty? +# hash[value] = self.send(key).select{|v| !v.nil?}.map{ |v| _to_hash v} unless self.send(key).nil? +# else +# unless (_tmp_value = _to_hash self.send(key)).nil? +# hash[value] = _tmp_value +# end +# end +# 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.respond_to? :to_hash +# value.to_hash +# else +# value +# end +# end + end diff --git a/samples/client/petstore/ruby/spec/pet_spec.rb b/samples/client/petstore/ruby/spec/pet_spec.rb index 67cee70c1ee5..91750e770eb6 100644 --- a/samples/client/petstore/ruby/spec/pet_spec.rb +++ b/samples/client/petstore/ruby/spec/pet_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'json' describe "Pet" do before do @@ -7,11 +8,53 @@ describe "Pet" do end describe "pet methods" do + it "should construct a new pet object" do + tag1 = Tag.new({'id' => 1, 'name'=> 'tag1'}) + tag2 = Tag.new({'id' => 2, 'name'=> 'tag2'}) + category1 = Category.new({:id => 1, :name => 'category unknown'}) + # initalize using both string and symbol key + pet_hash = {:'id' => 10002, :'name' => "RUBY UNIT TESTING", :'status' => "pending", + :'photo_urls' => ["url1", "url2"], :'category' => category1, + :'tags' => [tag1, tag2]} + pet = Pet.new(pet_hash) + # test new + pet.name.should == "RUBY UNIT TESTING" + pet.status.should == "pending" + pet.id.should == 10002 + pet.tags[0].id.should == 1 + pet.tags[1].name.should == 'tag2' + pet.category.name.should == 'category unknown' + + # test build_from_hash + pet2 = Pet.new + pet2.build_from_hash(pet.to_hash) + pet.to_hash.should == pet2.to_hash + + # make sure sub-object has different object id + pet.tags[0].object_id.should_not == pet2.tags[0].object_id + pet.tags[1].object_id.should_not == pet2.tags[1].object_id + pet.category.object_id.should_not == pet2.category.object_id + + puts pet.to_json + pet_array = [pet, pet2] + puts pet_array.map{ |v| + if v.respond_to?(:to_hash) + v.to_hash + else + v + end + }.to_json + + end + it "should fetch a pet object" do pet = PetApi.get_pet_by_id(10002) + print pet.inspect pet.should be_a(Pet) pet.id.should == 10002 pet.name.should == "RUBY UNIT TESTING" + pet.tags[0].name.should == "RUBY UNIT TESTING" + pet.category.name.should == "RUBY UNIT TESTING" end it "should find pets by status" do diff --git a/samples/client/petstore/ruby/spec/spec_helper.rb b/samples/client/petstore/ruby/spec/spec_helper.rb index 71725a45a6c9..dd24eeca3800 100644 --- a/samples/client/petstore/ruby/spec/spec_helper.rb +++ b/samples/client/petstore/ruby/spec/spec_helper.rb @@ -49,7 +49,10 @@ def prepare_pet # remove the pet PetApi.delete_pet(10002) # recreate the pet - pet = Pet.new('id' => 10002, 'name' => "RUBY UNIT TESTING") + category = Category.new('id' => 20002, 'name' => 'category test') + tag = Tag.new('id' => 30002, 'name' => 'tag test') + pet = Pet.new('id' => 10002, 'name' => "RUBY UNIT TESTING", 'photo_urls' => 'photo url', + 'category' => category, 'tags' => [tag], 'status' => 'pending') PetApi.add_pet(:body => pet) end