Merge pull request #2912 from zlx/feature/ruby_enum_support

Add Ruby enum support
This commit is contained in:
wing328 2016-05-21 17:39:56 +08:00
commit bc44f74786
5 changed files with 294 additions and 397 deletions

View File

@ -5,6 +5,7 @@ import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.CodegenParameter;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile;
@ -527,6 +528,57 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
return camelize(name) + "Api";
}
@Override
public String toEnumValue(String value, String datatype) {
if ("Integer".equals(datatype) || "Float".equals(datatype)) {
return value;
} else {
return "\"" + escapeText(value) + "\"";
}
}
@Override
public String toEnumVarName(String name, String datatype) {
// number
if ("Integer".equals(datatype) || "Float".equals(datatype)) {
String varName = new String(name);
varName = varName.replaceAll("-", "MINUS_");
varName = varName.replaceAll("\\+", "PLUS_");
varName = varName.replaceAll("\\.", "_DOT_");
return varName;
}
// string
String enumName = sanitizeName(underscore(name).toUpperCase());
enumName = enumName.replaceFirst("^_", "");
enumName = enumName.replaceFirst("_$", "");
if (enumName.matches("\\d.*")) { // starts with number
return "N" + enumName;
} else {
return enumName;
}
}
@Override
public String toEnumName(CodegenProperty property) {
String enumName = underscore(toModelName(property.name)).toUpperCase();
enumName = enumName.replaceFirst("^_", "");
enumName = enumName.replaceFirst("_$", "");
if (enumName.matches("\\d.*")) { // starts with number
return "N" + enumName;
} else {
return enumName;
}
}
@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
// process enum in models
return postProcessModelsEnum(objs);
}
@Override
public String toOperationId(String operationId) {
// rename to empty_method_name_1 (e.g.) if method name is empty

View File

@ -4,239 +4,6 @@
require 'date'
module {{moduleName}}{{#models}}{{#model}}{{#description}}
# {{{description}}}{{/description}}
class {{classname}}{{#vars}}{{#description}}
# {{{description}}}{{/description}}
attr_accessor :{{{name}}}
{{/vars}}
# Attribute mapping from ruby-style variable name to JSON key.
def self.attribute_map
{
{{#vars}}
:'{{{name}}}' => :'{{{baseName}}}'{{#hasMore}},{{/hasMore}}
{{/vars}}
}
end
# Attribute type mapping.
def self.swagger_types
{
{{#vars}}
:'{{{name}}}' => :'{{{datatype}}}'{{#hasMore}},{{/hasMore}}
{{/vars}}
}
end
# Initializes the object
# @param [Hash] attributes Model attributes in the form of hash
def initialize(attributes = {})
return unless attributes.is_a?(Hash)
# convert string to symbol for hash key
attributes = attributes.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
{{#vars}}
if attributes.has_key?(:'{{{baseName}}}')
{{#isContainer}}
if (value = attributes[:'{{{baseName}}}']).is_a?(Array)
self.{{{name}}} = value
end
{{/isContainer}}
{{^isContainer}}
self.{{{name}}} = attributes[:'{{{baseName}}}']
{{/isContainer}}
{{#defaultValue}}
else
self.{{{name}}} = {{{defaultValue}}}
{{/defaultValue}}
end
{{/vars}}
end
# Show invalid properties with the reasons. Usually used together with valid?
# @return Array for valid properies with the reasons
def list_invalid_properties
invalid_properties = Array.new
{{#isEnum}}
allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}]
if @{{{name}}} && !allowed_values.include?({{{name}}})
invalid_properties.push("invalid value for '{{{name}}}', must be one of #{allowed_values}.")
end
{{/isEnum}}
{{#hasValidation}}
if @{{{name}}}.nil?
fail ArgumentError, "{{{name}}} cannot be nil"
end
{{#minLength}}
if @{{{name}}}.to_s.length > {{{maxLength}}}
invalid_properties.push("invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}.")
end
{{/minLength}}
{{#maxLength}}
if @{{{name}}}.to_s.length < {{{minLength}}}
invalid_properties.push("invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}.")
end
{{/maxLength}}
{{#maximum}}
if @{{{name}}} > {{{maximum}}}
invalid_properties.push("invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}.")
end
{{/maximum}}
{{#minimum}}
if @{{{name}}} < {{{minimum}}}
invalid_properties.push("invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}.")
end
{{/minimum}}
{{#pattern}}
if @{{{name}}} !~ Regexp.new({{{pattern}}})
invalid_properties.push("invalid value for '{{{name}}}', must conform to the pattern {{{pattern}}}.")
end
{{/pattern}}
{{/hasValidation}}
return invalid_properties
end
# Check to see if the all the properties in the model are valid
# @return true if the model is valid
def valid?
{{#vars}}
{{#required}}
if @{{{name}}}.nil?
return false
end
{{/required}}
{{#isEnum}}
allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}]
if @{{{name}}} && !allowed_values.include?(@{{{name}}})
return false
end
{{/isEnum}}
{{#hasValidation}}
{{#minLength}}
if @{{{name}}}.to_s.length > {{{maxLength}}}
return false
end
{{/minLength}}
{{#maxLength}}
if @{{{name}}}.to_s.length < {{{minLength}}}
return false
end
{{/maxLength}}
{{#maximum}}
if @{{{name}}} > {{{maximum}}}
return false
end
{{/maximum}}
{{#minimum}}
if @{{{name}}} < {{{minimum}}}
return false
end
{{/minimum}}
{{#pattern}}
if @{{{name}}} !~ Regexp.new({{{pattern}}})
return false
end
{{/pattern}}
{{/hasValidation}}
{{/vars}}
end
{{#vars}}
{{#isEnum}}
# Custom attribute writer method checking allowed values (enum).
# @param [Object] {{{name}}} Object to be assigned
def {{{name}}}=({{{name}}})
allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}]
if {{{name}}} && !allowed_values.include?({{{name}}})
fail ArgumentError, "invalid value for '{{{name}}}', must be one of #{allowed_values}."
end
@{{{name}}} = {{{name}}}
end
{{/isEnum}}
{{^isEnum}}
{{#hasValidation}}
# Custom attribute writer method with validation
# @param [Object] {{{name}}} Value to be assigned
def {{{name}}}=({{{name}}})
if {{{name}}}.nil?
fail ArgumentError, "{{{name}}} cannot be nil"
end
{{#minLength}}
if {{{name}}}.to_s.length > {{{maxLength}}}
fail ArgumentError, "invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}."
end
{{/minLength}}
{{#maxLength}}
if {{{name}}}.to_s.length < {{{minLength}}}
fail ArgumentError, "invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}."
end
{{/maxLength}}
{{#maximum}}
if {{{name}}} > {{{maximum}}}
fail ArgumentError, "invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}."
end
{{/maximum}}
{{#minimum}}
if {{{name}}} < {{{minimum}}}
fail ArgumentError, "invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}."
end
{{/minimum}}
{{#pattern}}
if @{{{name}}} !~ Regexp.new({{{pattern}}})
fail ArgumentError, "invalid value for '{{{name}}}', must conform to the pattern {{{pattern}}}."
end
{{/pattern}}
@{{{name}}} = {{{name}}}
end
{{/hasValidation}}
{{/isEnum}}
{{/vars}}
# Checks equality by comparing each attribute.
# @param [Object] Object to be compared
def ==(o)
return true if self.equal?(o)
self.class == o.class{{#vars}} &&
{{name}} == o.{{name}}{{/vars}}
end
# @see the `==` method
# @param [Object] Object to be compared
def eql?(o)
self == o
end
# Calculates hash code according to all attributes.
# @return [Fixnum] Hash code
def hash
[{{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}}].hash
end
{{> base_object}}
end
{{/model}}
{{/models}}
module {{moduleName}}
{{#models}}{{#model}}{{#isEnum}}{{>partial_model_enum_class}}{{/isEnum}}{{^isEnum}}{{>partial_model_generic}}{{/isEnum}}{{/model}}{{/models}}
end

View File

@ -0,0 +1,4 @@
class {{classname}}
{{#allowableValues}}{{#enumVars}}
{{{name}}} = {{{value}}}.freeze{{/enumVars}}{{/allowableValues}}
end

View File

@ -0,0 +1,232 @@
{{#description}} # {{{description}}}{{/description}}
class {{classname}}{{#vars}}{{#description}}
# {{{description}}}{{/description}}
attr_accessor :{{{name}}}
{{/vars}}
# Attribute mapping from ruby-style variable name to JSON key.
def self.attribute_map
{
{{#vars}}
:'{{{name}}}' => :'{{{baseName}}}'{{#hasMore}},{{/hasMore}}
{{/vars}}
}
end
# Attribute type mapping.
def self.swagger_types
{
{{#vars}}
:'{{{name}}}' => :'{{{datatype}}}'{{#hasMore}},{{/hasMore}}
{{/vars}}
}
end
# Initializes the object
# @param [Hash] attributes Model attributes in the form of hash
def initialize(attributes = {})
return unless attributes.is_a?(Hash)
# convert string to symbol for hash key
attributes = attributes.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
{{#vars}}
if attributes.has_key?(:'{{{baseName}}}')
{{#isContainer}}
if (value = attributes[:'{{{baseName}}}']).is_a?(Array)
self.{{{name}}} = value
end
{{/isContainer}}
{{^isContainer}}
self.{{{name}}} = attributes[:'{{{baseName}}}']
{{/isContainer}}
{{#defaultValue}}
else
self.{{{name}}} = {{{defaultValue}}}
{{/defaultValue}}
end
{{/vars}}
end
# Show invalid properties with the reasons. Usually used together with valid?
# @return Array for valid properies with the reasons
def list_invalid_properties
invalid_properties = Array.new
{{#isEnum}}
allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}]
if @{{{name}}} && !allowed_values.include?({{{name}}})
invalid_properties.push("invalid value for '{{{name}}}', must be one of #{allowed_values}.")
end
{{/isEnum}}
{{#hasValidation}}
if @{{{name}}}.nil?
fail ArgumentError, "{{{name}}} cannot be nil"
end
{{#minLength}}
if @{{{name}}}.to_s.length > {{{maxLength}}}
invalid_properties.push("invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}.")
end
{{/minLength}}
{{#maxLength}}
if @{{{name}}}.to_s.length < {{{minLength}}}
invalid_properties.push("invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}.")
end
{{/maxLength}}
{{#maximum}}
if @{{{name}}} > {{{maximum}}}
invalid_properties.push("invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}.")
end
{{/maximum}}
{{#minimum}}
if @{{{name}}} < {{{minimum}}}
invalid_properties.push("invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}.")
end
{{/minimum}}
{{#pattern}}
if @{{{name}}} !~ Regexp.new({{{pattern}}})
invalid_properties.push("invalid value for '{{{name}}}', must conform to the pattern {{{pattern}}}.")
end
{{/pattern}}
{{/hasValidation}}
return invalid_properties
end
# Check to see if the all the properties in the model are valid
# @return true if the model is valid
def valid?
{{#vars}}
{{#required}}
if @{{{name}}}.nil?
return false
end
{{/required}}
{{#isEnum}}
allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}]
if @{{{name}}} && !allowed_values.include?(@{{{name}}})
return false
end
{{/isEnum}}
{{#hasValidation}}
{{#minLength}}
if @{{{name}}}.to_s.length > {{{maxLength}}}
return false
end
{{/minLength}}
{{#maxLength}}
if @{{{name}}}.to_s.length < {{{minLength}}}
return false
end
{{/maxLength}}
{{#maximum}}
if @{{{name}}} > {{{maximum}}}
return false
end
{{/maximum}}
{{#minimum}}
if @{{{name}}} < {{{minimum}}}
return false
end
{{/minimum}}
{{#pattern}}
if @{{{name}}} !~ Regexp.new({{{pattern}}})
return false
end
{{/pattern}}
{{/hasValidation}}
{{/vars}}
end
{{#vars}}
{{#isEnum}}
# Custom attribute writer method checking allowed values (enum).
# @param [Object] {{{name}}} Object to be assigned
def {{{name}}}=({{{name}}})
allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}]
if {{{name}}} && !allowed_values.include?({{{name}}})
fail ArgumentError, "invalid value for '{{{name}}}', must be one of #{allowed_values}."
end
@{{{name}}} = {{{name}}}
end
{{/isEnum}}
{{^isEnum}}
{{#hasValidation}}
# Custom attribute writer method with validation
# @param [Object] {{{name}}} Value to be assigned
def {{{name}}}=({{{name}}})
if {{{name}}}.nil?
fail ArgumentError, "{{{name}}} cannot be nil"
end
{{#minLength}}
if {{{name}}}.to_s.length > {{{maxLength}}}
fail ArgumentError, "invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}."
end
{{/minLength}}
{{#maxLength}}
if {{{name}}}.to_s.length < {{{minLength}}}
fail ArgumentError, "invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}."
end
{{/maxLength}}
{{#maximum}}
if {{{name}}} > {{{maximum}}}
fail ArgumentError, "invalid value for '{{{name}}}', must be smaller than or equal to {{{maximum}}}."
end
{{/maximum}}
{{#minimum}}
if {{{name}}} < {{{minimum}}}
fail ArgumentError, "invalid value for '{{{name}}}', must be greater than or equal to {{{minimum}}}."
end
{{/minimum}}
{{#pattern}}
if @{{{name}}} !~ Regexp.new({{{pattern}}})
fail ArgumentError, "invalid value for '{{{name}}}', must conform to the pattern {{{pattern}}}."
end
{{/pattern}}
@{{{name}}} = {{{name}}}
end
{{/hasValidation}}
{{/isEnum}}
{{/vars}}
# Checks equality by comparing each attribute.
# @param [Object] Object to be compared
def ==(o)
return true if self.equal?(o)
self.class == o.class{{#vars}} &&
{{name}} == o.{{name}}{{/vars}}
end
# @see the `==` method
# @param [Object] Object to be compared
def eql?(o)
self == o
end
# Calculates hash code according to all attributes.
# @return [Fixnum] Hash code
def hash
[{{#vars}}{{name}}{{#hasMore}}, {{/hasMore}}{{/vars}}].hash
end
{{> base_object}}
end

View File

@ -1,7 +1,7 @@
=begin
Swagger Petstore
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
OpenAPI spec version: 1.0.0
Contact: apiteam@swagger.io
@ -18,167 +18,9 @@ require 'date'
module Petstore
class EnumClass
# Attribute mapping from ruby-style variable name to JSON key.
def self.attribute_map
{
}
end
# Attribute type mapping.
def self.swagger_types
{
}
end
# Initializes the object
# @param [Hash] attributes Model attributes in the form of hash
def initialize(attributes = {})
return unless attributes.is_a?(Hash)
# convert string to symbol for hash key
attributes = attributes.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
end
# Show invalid properties with the reasons. Usually used together with valid?
# @return Array for valid properies with the reasons
def list_invalid_properties
invalid_properties = Array.new
allowed_values = ["_abc", "-efg", "(xyz)"]
if @EnumClass && !allowed_values.include?(EnumClass)
invalid_properties.push("invalid value for 'EnumClass', must be one of #{allowed_values}.")
end
return invalid_properties
end
# Check to see if the all the properties in the model are valid
# @return true if the model is valid
def valid?
end
# Checks equality by comparing each attribute.
# @param [Object] Object to be compared
def ==(o)
return true if self.equal?(o)
self.class == o.class
end
# @see the `==` method
# @param [Object] Object to be compared
def eql?(o)
self == o
end
# Calculates hash code according to all attributes.
# @return [Fixnum] Hash code
def hash
[].hash
end
# Builds the object from hash
# @param [Hash] attributes Model attributes in the form of hash
# @return [Object] Returns the model itself
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
# check to ensure the input is an array given that the the attribute
# is documented as an array but the input is not
if attributes[self.class.attribute_map[key]].is_a?(Array)
self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
end
elsif !attributes[self.class.attribute_map[key]].nil?
self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
end # or else data not found in attributes(hash), not an issue as the data can be optional
end
self
end
# Deserializes the data based on type
# @param string type Data type
# @param string value Value to be deserialized
# @return [Object] Deserialized data
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
temp_model = Petstore.const_get(type).new
temp_model.build_from_hash(value)
end
end
# Returns the string representation of the object
# @return [String] String presentation of the object
def to_s
to_hash.to_s
end
# to_body is an alias to to_hash (backward compatibility)
# @return [Hash] Returns the object in the form of hash
def to_body
to_hash
end
# Returns the object in the form of hash
# @return [Hash] Returns 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
# Outputs non-array value in the form of hash
# For object, use to_hash. Otherwise, just return the value
# @param [Object] value Any valid value
# @return [Hash] Returns the value in the form of hash
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
ABC = "_ab\"c".freeze
EFG = "-efg".freeze
XYZ = "(xy'z)".freeze
end
end