Ruby client: update Petstore sample, fix model import

This commit is contained in:
xhh 2016-03-04 15:41:27 +08:00
parent 9b4b0111ad
commit 53d8a31bd3
7 changed files with 435 additions and 6 deletions

View File

@ -86,6 +86,7 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
languageSpecificPrimitives.add("Array"); languageSpecificPrimitives.add("Array");
languageSpecificPrimitives.add("Hash"); languageSpecificPrimitives.add("Hash");
languageSpecificPrimitives.add("File"); languageSpecificPrimitives.add("File");
languageSpecificPrimitives.add("Object");
typeMapping.put("string", "String"); typeMapping.put("string", "String");
typeMapping.put("char", "String"); typeMapping.put("char", "String");
@ -460,11 +461,6 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
return underscore(sanitizeName(operationId)); return underscore(sanitizeName(operationId));
} }
@Override
public String toModelImport(String name) {
return gemName + "/" + modelPackage() + "/" + underscore(name);
}
@Override @Override
public String toApiImport(String name) { public String toApiImport(String name) {
return gemName + "/" + apiPackage() + "/" + toApiFilename(name); return gemName + "/" + apiPackage() + "/" + toApiFilename(name);

View File

@ -10,7 +10,8 @@ require '{{gemName}}/configuration'
# Models # Models
{{#models}} {{#models}}
require '{{importPath}}' {{#model}}
require '{{gemName}}/{{modelPackage}}/{{classFilename}}'{{/model}}
{{/models}} {{/models}}
# APIs # APIs

View File

@ -22,8 +22,10 @@ require 'petstore/configuration'
# Models # Models
require 'petstore/models/order' require 'petstore/models/order'
require 'petstore/models/special_model_name'
require 'petstore/models/user' require 'petstore/models/user'
require 'petstore/models/category' require 'petstore/models/category'
require 'petstore/models/object_return'
require 'petstore/models/inline_response_200' require 'petstore/models/inline_response_200'
require 'petstore/models/tag' require 'petstore/models/tag'
require 'petstore/models/pet' require 'petstore/models/pet'

View File

@ -0,0 +1,165 @@
=begin
Swagger Petstore
This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters
OpenAPI spec version: 1.0.0
Contact: apiteam@swagger.io
Generated by: https://github.com/swagger-api/swagger-codegen.git
License: Apache 2.0
http://www.apache.org/licenses/LICENSE-2.0.html
Terms of Service: http://swagger.io/terms/
=end
require 'date'
module Petstore
class ObjectReturn
attr_accessor :_return
# Attribute mapping from ruby-style variable name to JSON key.
def self.attribute_map
{
:'_return' => :'return'
}
end
# Attribute type mapping.
def self.swagger_types
{
:'_return' => :'Integer'
}
end
def initialize(attributes = {})
return unless attributes.is_a?(Hash)
# convert string to symbol for hash key
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
if attributes[:'return']
self._return = attributes[:'return']
end
end
# Check equality by comparing each attribute.
def ==(o)
return true if self.equal?(o)
self.class == o.class &&
_return == o._return
end
# @see the `==` method
def eql?(o)
self == o
end
# Calculate hash code according to all attributes.
def hash
[_return].hash
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.to_s =~ /^(true|t|yes|y|1)$/i
true
else
false
end
when :Object
# generic object (usually a Hash), return directly
value
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

View File

@ -0,0 +1,165 @@
=begin
Swagger Petstore
This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters
OpenAPI spec version: 1.0.0
Contact: apiteam@swagger.io
Generated by: https://github.com/swagger-api/swagger-codegen.git
License: Apache 2.0
http://www.apache.org/licenses/LICENSE-2.0.html
Terms of Service: http://swagger.io/terms/
=end
require 'date'
module Petstore
class SpecialModelName
attr_accessor :special_property_name
# Attribute mapping from ruby-style variable name to JSON key.
def self.attribute_map
{
:'special_property_name' => :'$special[property.name]'
}
end
# Attribute type mapping.
def self.swagger_types
{
:'special_property_name' => :'Integer'
}
end
def initialize(attributes = {})
return unless attributes.is_a?(Hash)
# convert string to symbol for hash key
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
if attributes[:'$special[property.name]']
self.special_property_name = attributes[:'$special[property.name]']
end
end
# Check equality by comparing each attribute.
def ==(o)
return true if self.equal?(o)
self.class == o.class &&
special_property_name == o.special_property_name
end
# @see the `==` method
def eql?(o)
self == o
end
# Calculate hash code according to all attributes.
def hash
[special_property_name].hash
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.to_s =~ /^(true|t|yes|y|1)$/i
true
else
false
end
when :Object
# generic object (usually a Hash), return directly
value
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

View File

@ -0,0 +1,50 @@
=begin
Swagger Petstore
This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters
OpenAPI spec version: 1.0.0
Contact: apiteam@swagger.io
Generated by: https://github.com/swagger-api/swagger-codegen.git
License: Apache 2.0
http://www.apache.org/licenses/LICENSE-2.0.html
Terms of Service: http://swagger.io/terms/
=end
require 'spec_helper'
require 'json'
require 'date'
# Unit tests for Petstore::
# Automatically generated by swagger-codegen (github.com/swagger-api/swagger-codegen)
# Please update as you see appropriate
describe 'ObjectReturn' do
before do
# run before each test
@instance = Petstore::ObjectReturn.new
end
after do
# run after each test
end
describe 'test an instance of ObjectReturn' do
it 'should create an instact of ObjectReturn' do
@instance.should be_a(Petstore::ObjectReturn)
end
end
describe 'test attribute "_return"' do
it 'should work' do
# assertion here
# should be_a()
# should be_nil
# should ==
# should_not ==
end
end
end

View File

@ -0,0 +1,50 @@
=begin
Swagger Petstore
This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters
OpenAPI spec version: 1.0.0
Contact: apiteam@swagger.io
Generated by: https://github.com/swagger-api/swagger-codegen.git
License: Apache 2.0
http://www.apache.org/licenses/LICENSE-2.0.html
Terms of Service: http://swagger.io/terms/
=end
require 'spec_helper'
require 'json'
require 'date'
# Unit tests for Petstore::
# Automatically generated by swagger-codegen (github.com/swagger-api/swagger-codegen)
# Please update as you see appropriate
describe 'SpecialModelName' do
before do
# run before each test
@instance = Petstore::SpecialModelName.new
end
after do
# run after each test
end
describe 'test an instance of SpecialModelName' do
it 'should create an instact of SpecialModelName' do
@instance.should be_a(Petstore::SpecialModelName)
end
end
describe 'test attribute "special_property_name"' do
it 'should work' do
# assertion here
# should be_a()
# should be_nil
# should ==
# should_not ==
end
end
end