diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java index f30b2ef8b85a..2fd6d8d8bc09 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java @@ -6,9 +6,7 @@ import io.swagger.codegen.CodegenConstants; import io.swagger.codegen.CodegenType; import io.swagger.codegen.DefaultCodegen; import io.swagger.codegen.SupportingFile; -import io.swagger.models.properties.ArrayProperty; -import io.swagger.models.properties.MapProperty; -import io.swagger.models.properties.Property; +import io.swagger.models.properties.*; import java.io.File; import java.util.Arrays; @@ -186,6 +184,43 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig { return super.getTypeDeclaration(p); } + @Override + public String toDefaultValue(Property p) { + if (p instanceof IntegerProperty) { + IntegerProperty dp = (IntegerProperty) p; + if (dp.getDefault() != null) { + return dp.getDefault().toString(); + } + } else if (p instanceof LongProperty) { + LongProperty dp = (LongProperty) p; + if (dp.getDefault() != null) { + return dp.getDefault().toString(); + } + } else if (p instanceof DoubleProperty) { + DoubleProperty dp = (DoubleProperty) p; + if (dp.getDefault() != null) { + return dp.getDefault().toString(); + } + } else if (p instanceof FloatProperty) { + FloatProperty dp = (FloatProperty) p; + if (dp.getDefault() != null) { + return dp.getDefault().toString(); + } + } else if (p instanceof BooleanProperty) { + BooleanProperty bp = (BooleanProperty) p; + if (bp.getDefault() != null) { + return bp.getDefault().toString(); + } + } else if (p instanceof StringProperty) { + StringProperty sp = (StringProperty) p; + if (sp.getDefault() != null) { + return "\"" + escapeText(sp.getDefault()) + "\""; + } + } + + return null; + } + @Override public String getSwaggerType(Property p) { String swaggerType = super.getSwaggerType(p); @@ -204,10 +239,6 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig { return type; } - public String toDefaultValue(Property p) { - return "null"; - } - @Override public String toVarName(String name) { // sanitize name diff --git a/modules/swagger-codegen/src/main/resources/ruby/api_client.mustache b/modules/swagger-codegen/src/main/resources/ruby/api_client.mustache index 3873d4765777..fa14e2f88ca4 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/api_client.mustache @@ -86,6 +86,15 @@ module {{moduleName}} Typhoeus::Request.new(url, req_opts) end + # Check if the given MIME is a JSON MIME. + # JSON MIME examples: + # application/json + # application/json; charset=UTF8 + # APPLICATION/JSON + def json_mime?(mime) + !!(mime =~ /\Aapplication\/json(;.*)?\z/i) + end + # Deserialize the response to the given return type. # # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]" @@ -99,9 +108,7 @@ module {{moduleName}} # ensuring a default content type content_type = response.headers['Content-Type'] || 'application/json' - unless content_type.start_with?('application/json') - fail "Content-Type is not supported: #{content_type}" - end + fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type) begin data = JSON.parse("[#{body}]", :symbolize_names => true)[0] @@ -228,26 +235,21 @@ module {{moduleName}} # @param [Array] accepts array for Accept # @return [String] the Accept header (e.g. application/json) def select_header_accept(accepts) - if accepts.empty? - return - elsif accepts.any?{ |s| s.casecmp('application/json') == 0 } - 'application/json' # look for json data by default - else - accepts.join(',') - end + return nil if accepts.nil? || accepts.empty? + # use JSON when present, otherwise use all of the provided + json_accept = accepts.find { |s| json_mime?(s) } + return json_accept || accepts.join(',') end # Return Content-Type header based on an array of content types provided. # @param [Array] content_types array for Content-Type # @return [String] the Content-Type header (e.g. application/json) def select_header_content_type(content_types) - if content_types.empty? - 'application/json' # use application/json by default - elsif content_types.any?{ |s| s.casecmp('application/json')==0 } - 'application/json' # use application/json if it's included - else - content_types[0] # otherwise, use the first one - end + # use application/json by default + return 'application/json' if content_types.nil? || content_types.empty? + # use JSON when present, otherwise use the first one + json_content_type = content_types.find { |s| json_mime?(s) } + return json_content_type || content_types.first end # Convert object (array, hash, object, etc) to JSON string. diff --git a/modules/swagger-codegen/src/main/resources/ruby/model.mustache b/modules/swagger-codegen/src/main/resources/ruby/model.mustache index 35742384c260..5d4eb954767a 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/model.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/model.mustache @@ -1,18 +1,19 @@ -module {{moduleName}} -{{#models}} # {{description}} -{{#model}} class {{classname}} < BaseObject +module {{moduleName}}{{#models}}{{#model}}{{#description}} + # {{{description}}}{{/description}} + class {{classname}} < BaseObject attr_accessor {{#vars}}:{{{name}}}{{#hasMore}}, {{/hasMore}}{{/vars}}{{newline}} - # attribute mapping from ruby-style variable name to JSON key + + # Attribute mapping from ruby-style variable name to JSON key. def self.attribute_map { - {{#vars}} - # {{description}} + {{#vars}}{{#description}} + # {{{description}}}{{/description}} :'{{{name}}}' => :'{{{baseName}}}'{{#hasMore}},{{/hasMore}} {{/vars}} } end - # attribute type + # Attribute type mapping. def self.swagger_types { {{#vars}}:'{{{name}}}' => :'{{{datatype}}}'{{#hasMore}},{{/hasMore}} @@ -21,7 +22,7 @@ module {{moduleName}} end def initialize(attributes = {}) - return if !attributes.is_a?(Hash) || attributes.empty? + 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} @@ -30,11 +31,14 @@ module {{moduleName}} if attributes[:'{{{baseName}}}'] {{#isContainer}}if (value = attributes[:'{{{baseName}}}']).is_a?(Array) self.{{{name}}} = value - end{{/isContainer}}{{^isContainer}}self.{{{name}}} = attributes[:'{{{baseName}}}']{{/isContainer}} + end{{/isContainer}}{{^isContainer}}self.{{{name}}} = attributes[:'{{{baseName}}}']{{/isContainer}}{{#defaultValue}} + else + self.{{{name}}} = {{{defaultValue}}}{{/defaultValue}} end {{/vars}} end {{#vars}}{{#isEnum}} + # Custom attribute writer method checking allowed values (enum). def {{{name}}}=({{{name}}}) allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] if {{{name}}} && !allowed_values.include?({{{name}}}) @@ -43,16 +47,19 @@ module {{moduleName}} @{{{name}}} = {{{name}}} end {{/isEnum}}{{/vars}} + # Check equality by comparing each attribute. def ==(o) return true if self.equal?(o) self.class == o.class{{#vars}} && {{name}} == o.{{name}}{{/vars}} end + # @see the `==` method def eql?(o) self == o end + # Calculate hash code according to all attributes. def hash [{{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}}].hash end diff --git a/samples/client/petstore/ruby/lib/petstore/api_client.rb b/samples/client/petstore/ruby/lib/petstore/api_client.rb index 412fb7946ad4..0648ee54cf35 100644 --- a/samples/client/petstore/ruby/lib/petstore/api_client.rb +++ b/samples/client/petstore/ruby/lib/petstore/api_client.rb @@ -86,6 +86,15 @@ module Petstore Typhoeus::Request.new(url, req_opts) end + # Check if the given MIME is a JSON MIME. + # JSON MIME examples: + # application/json + # application/json; charset=UTF8 + # APPLICATION/JSON + def json_mime?(mime) + !!(mime =~ /\Aapplication\/json(;.*)?\z/i) + end + # Deserialize the response to the given return type. # # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]" @@ -99,9 +108,7 @@ module Petstore # ensuring a default content type content_type = response.headers['Content-Type'] || 'application/json' - unless content_type.start_with?('application/json') - fail "Content-Type is not supported: #{content_type}" - end + fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type) begin data = JSON.parse("[#{body}]", :symbolize_names => true)[0] @@ -228,26 +235,21 @@ module Petstore # @param [Array] accepts array for Accept # @return [String] the Accept header (e.g. application/json) def select_header_accept(accepts) - if accepts.empty? - return - elsif accepts.any?{ |s| s.casecmp('application/json') == 0 } - 'application/json' # look for json data by default - else - accepts.join(',') - end + return nil if accepts.nil? || accepts.empty? + # use JSON when present, otherwise use all of the provided + json_accept = accepts.find { |s| json_mime?(s) } + return json_accept || accepts.join(',') end # Return Content-Type header based on an array of content types provided. # @param [Array] content_types array for Content-Type # @return [String] the Content-Type header (e.g. application/json) def select_header_content_type(content_types) - if content_types.empty? - 'application/json' # use application/json by default - elsif content_types.any?{ |s| s.casecmp('application/json')==0 } - 'application/json' # use application/json if it's included - else - content_types[0] # otherwise, use the first one - end + # use application/json by default + return 'application/json' if content_types.nil? || content_types.empty? + # use JSON when present, otherwise use the first one + json_content_type = content_types.find { |s| json_mime?(s) } + return json_content_type || content_types.first end # Convert object (array, hash, object, etc) to JSON string. diff --git a/samples/client/petstore/ruby/lib/petstore/models/category.rb b/samples/client/petstore/ruby/lib/petstore/models/category.rb index ab5b9cabbaaa..8864ad10f753 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/category.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/category.rb @@ -1,21 +1,19 @@ module Petstore - # class Category < BaseObject attr_accessor :id, :name - # attribute mapping from ruby-style variable name to JSON key + + # Attribute mapping from ruby-style variable name to JSON key. def self.attribute_map { - # :'id' => :'id', - # :'name' => :'name' } end - # attribute type + # Attribute type mapping. def self.swagger_types { :'id' => :'Integer', @@ -25,7 +23,7 @@ module Petstore end def initialize(attributes = {}) - return if !attributes.is_a?(Hash) || attributes.empty? + 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} @@ -41,6 +39,7 @@ module Petstore end + # Check equality by comparing each attribute. def ==(o) return true if self.equal?(o) self.class == o.class && @@ -48,10 +47,12 @@ module Petstore name == o.name end + # @see the `==` method def eql?(o) self == o end + # Calculate hash code according to all attributes. def hash [id, name].hash end diff --git a/samples/client/petstore/ruby/lib/petstore/models/order.rb b/samples/client/petstore/ruby/lib/petstore/models/order.rb index 74eab3436e3e..59c84f656660 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/order.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/order.rb @@ -1,33 +1,28 @@ module Petstore - # class Order < BaseObject attr_accessor :id, :pet_id, :quantity, :ship_date, :status, :complete - # attribute mapping from ruby-style variable name to JSON key + + # Attribute mapping from ruby-style variable name to JSON key. def self.attribute_map { - # :'id' => :'id', - # :'pet_id' => :'petId', - # :'quantity' => :'quantity', - # :'ship_date' => :'shipDate', # Order Status :'status' => :'status', - # :'complete' => :'complete' } end - # attribute type + # Attribute type mapping. def self.swagger_types { :'id' => :'Integer', @@ -41,7 +36,7 @@ module Petstore end def initialize(attributes = {}) - return if !attributes.is_a?(Hash) || attributes.empty? + 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} @@ -73,6 +68,7 @@ module Petstore end + # Custom attribute writer method checking allowed values (enum). def status=(status) allowed_values = ["placed", "approved", "delivered"] if status && !allowed_values.include?(status) @@ -81,6 +77,7 @@ module Petstore @status = status end + # Check equality by comparing each attribute. def ==(o) return true if self.equal?(o) self.class == o.class && @@ -92,10 +89,12 @@ module Petstore complete == o.complete end + # @see the `==` method def eql?(o) self == o end + # Calculate hash code according to all attributes. def hash [id, pet_id, quantity, ship_date, status, complete].hash end diff --git a/samples/client/petstore/ruby/lib/petstore/models/pet.rb b/samples/client/petstore/ruby/lib/petstore/models/pet.rb index 8c5fe6e17e7f..303a12d702cd 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/pet.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/pet.rb @@ -1,24 +1,19 @@ module Petstore - # class Pet < BaseObject attr_accessor :id, :category, :name, :photo_urls, :tags, :status - # attribute mapping from ruby-style variable name to JSON key + + # 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', # pet status in the store @@ -27,7 +22,7 @@ module Petstore } end - # attribute type + # Attribute type mapping. def self.swagger_types { :'id' => :'Integer', @@ -41,7 +36,7 @@ module Petstore end def initialize(attributes = {}) - return if !attributes.is_a?(Hash) || attributes.empty? + 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} @@ -77,6 +72,7 @@ module Petstore end + # Custom attribute writer method checking allowed values (enum). def status=(status) allowed_values = ["available", "pending", "sold"] if status && !allowed_values.include?(status) @@ -85,6 +81,7 @@ module Petstore @status = status end + # Check equality by comparing each attribute. def ==(o) return true if self.equal?(o) self.class == o.class && @@ -96,10 +93,12 @@ module Petstore status == o.status end + # @see the `==` method def eql?(o) self == o end + # Calculate hash code according to all attributes. def hash [id, category, name, photo_urls, tags, status].hash end diff --git a/samples/client/petstore/ruby/lib/petstore/models/tag.rb b/samples/client/petstore/ruby/lib/petstore/models/tag.rb index 51439de58466..06d779c2c547 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/tag.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/tag.rb @@ -1,21 +1,19 @@ module Petstore - # class Tag < BaseObject attr_accessor :id, :name - # attribute mapping from ruby-style variable name to JSON key + + # Attribute mapping from ruby-style variable name to JSON key. def self.attribute_map { - # :'id' => :'id', - # :'name' => :'name' } end - # attribute type + # Attribute type mapping. def self.swagger_types { :'id' => :'Integer', @@ -25,7 +23,7 @@ module Petstore end def initialize(attributes = {}) - return if !attributes.is_a?(Hash) || attributes.empty? + 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} @@ -41,6 +39,7 @@ module Petstore end + # Check equality by comparing each attribute. def ==(o) return true if self.equal?(o) self.class == o.class && @@ -48,10 +47,12 @@ module Petstore name == o.name end + # @see the `==` method def eql?(o) self == o end + # Calculate hash code according to all attributes. def hash [id, name].hash end diff --git a/samples/client/petstore/ruby/lib/petstore/models/user.rb b/samples/client/petstore/ruby/lib/petstore/models/user.rb index 3d20ab95c4f4..1b49db1c96be 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/user.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/user.rb @@ -1,30 +1,23 @@ module Petstore - # class User < BaseObject attr_accessor :id, :username, :first_name, :last_name, :email, :password, :phone, :user_status - # attribute mapping from ruby-style variable name to JSON key + + # 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 @@ -33,7 +26,7 @@ module Petstore } end - # attribute type + # Attribute type mapping. def self.swagger_types { :'id' => :'Integer', @@ -49,7 +42,7 @@ module Petstore end def initialize(attributes = {}) - return if !attributes.is_a?(Hash) || attributes.empty? + 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} @@ -89,6 +82,7 @@ module Petstore end + # Check equality by comparing each attribute. def ==(o) return true if self.equal?(o) self.class == o.class && @@ -102,10 +96,12 @@ module Petstore user_status == o.user_status end + # @see the `==` method def eql?(o) self == o end + # Calculate hash code according to all attributes. def hash [id, username, first_name, last_name, email, password, phone, user_status].hash end diff --git a/samples/client/petstore/ruby/spec/api_client_spec.rb b/samples/client/petstore/ruby/spec/api_client_spec.rb index eeb27300bf37..7b2478ea67c3 100644 --- a/samples/client/petstore/ruby/spec/api_client_spec.rb +++ b/samples/client/petstore/ruby/spec/api_client_spec.rb @@ -145,4 +145,52 @@ describe Petstore::ApiClient do end end + describe "#json_mime?" do + let(:api_client) { Petstore::ApiClient.new } + + it "works" do + api_client.json_mime?(nil).should == false + api_client.json_mime?('').should == false + + api_client.json_mime?('application/json').should == true + api_client.json_mime?('application/json; charset=UTF8').should == true + api_client.json_mime?('APPLICATION/JSON').should == true + + api_client.json_mime?('application/xml').should == false + api_client.json_mime?('text/plain').should == false + api_client.json_mime?('application/jsonp').should == false + end + end + + describe "#select_header_accept" do + let(:api_client) { Petstore::ApiClient.new } + + it "works" do + api_client.select_header_accept(nil).should == nil + api_client.select_header_accept([]).should == nil + + api_client.select_header_accept(['application/json']).should == 'application/json' + api_client.select_header_accept(['application/xml', 'application/json; charset=UTF8']).should == 'application/json; charset=UTF8' + api_client.select_header_accept(['APPLICATION/JSON', 'text/html']).should == 'APPLICATION/JSON' + + api_client.select_header_accept(['application/xml']).should == 'application/xml' + api_client.select_header_accept(['text/html', 'application/xml']).should == 'text/html,application/xml' + end + end + + describe "#select_header_content_type" do + let(:api_client) { Petstore::ApiClient.new } + + it "works" do + api_client.select_header_content_type(nil).should == 'application/json' + api_client.select_header_content_type([]).should == 'application/json' + + api_client.select_header_content_type(['application/json']).should == 'application/json' + api_client.select_header_content_type(['application/xml', 'application/json; charset=UTF8']).should == 'application/json; charset=UTF8' + api_client.select_header_content_type(['APPLICATION/JSON', 'text/html']).should == 'APPLICATION/JSON' + api_client.select_header_content_type(['application/xml']).should == 'application/xml' + api_client.select_header_content_type(['text/plain', 'application/xml']).should == 'text/plain' + end + end + end