Validate parameter against allowed values (enum)

This commit is contained in:
xhh 2015-07-03 16:46:23 +08:00
parent 846d282ba0
commit 66d16cfeaf
6 changed files with 41 additions and 34 deletions

View File

@ -1,10 +1,18 @@
package io.swagger.codegen;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
public class CodegenParameter {
public Boolean isFormParam, isQueryParam, isPathParam, isHeaderParam,
isCookieParam, isBodyParam, isFile, notFile, hasMore, isContainer, secondaryParam;
public String baseName, paramName, dataType, collectionFormat, description, baseType, defaultValue;
public String jsonSchema;
public boolean isEnum;
public List<String> _enum;
public Map<String, Object> allowableValues;
/**
* Determines whether this parameter is mandatory. If the parameter is in "path",
@ -35,6 +43,13 @@ public class CodegenParameter {
output.required = this.required;
output.jsonSchema = this.jsonSchema;
output.defaultValue = this.defaultValue;
output.isEnum = this.isEnum;
if (this._enum != null) {
output._enum = new ArrayList<String>(this._enum);
}
if (this.allowableValues != null) {
output.allowableValues = new HashMap<String, Object>(this.allowableValues);
}
return output;
}

View File

@ -36,6 +36,7 @@ import io.swagger.models.properties.LongProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.PropertyBuilder;
import io.swagger.models.properties.PropertyBuilder.PropertyId;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import io.swagger.util.Json;
@ -964,7 +965,9 @@ public class DefaultCodegen {
p.baseType = pr.datatype;
imports.add(pr.baseType);
} else {
property = PropertyBuilder.build(qp.getType(), qp.getFormat(), null);
Map<PropertyId, Object> args = new HashMap<PropertyId, Object>();
args.put(PropertyId.ENUM, qp.getEnum());
property = PropertyBuilder.build(qp.getType(), qp.getFormat(), args);
}
if (property == null) {
LOGGER.warn("warning! Property type \"" + qp.getType() + "\" not found for parameter \"" + param.getName() + "\", using String");
@ -972,8 +975,11 @@ public class DefaultCodegen {
}
property.setRequired(param.getRequired());
CodegenProperty model = fromProperty(qp.getName(), property);
p.collectionFormat = collectionFormat;
p.dataType = model.datatype;
p.isEnum = model.isEnum;
p._enum = model._enum;
p.allowableValues = model.allowableValues;
p.collectionFormat = collectionFormat;
p.paramName = toParamName(qp.getName());
if (model.complexType != null) {

View File

@ -17,9 +17,15 @@ module {{moduleName}}
end
{{#allParams}}{{#required}}
# verify the required parameter '{{paramName}}' is set
raise "Missing the required parameter '{{paramName}}' when calling {{nickname}}" if {{{paramName}}}.nil?
{{/required}}{{/allParams}}
fail "Missing the required parameter '{{paramName}}' when calling {{nickname}}" if {{{paramName}}}.nil?{{#isEnum}}
unless [{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?({{{paramName}}})
fail "invalid value for '{{{paramName}}}', must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}"
end{{/isEnum}}
{{/required}}{{^required}}{{#isEnum}}
if opts[:'{{{paramName}}}'] && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?(opts[:'{{{paramName}}}'])
fail 'invalid value for "{{{paramName}}}", must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}'
end
{{/isEnum}}{{/required}}{{/allParams}}
# resource path
path = "{{path}}".sub('{format}','json'){{#pathParams}}.sub('{' + '{{baseName}}' + '}', {{paramName}}.to_s){{/pathParams}}

View File

@ -13,7 +13,6 @@ module Petstore
Swagger.logger.debug "Calling API: PetApi#update_pet ..."
end
# resource path
path = "/pet".sub('{format}','json')
@ -56,7 +55,6 @@ module Petstore
Swagger.logger.debug "Calling API: PetApi#add_pet ..."
end
# resource path
path = "/pet".sub('{format}','json')
@ -99,7 +97,6 @@ module Petstore
Swagger.logger.debug "Calling API: PetApi#find_pets_by_status ..."
end
# resource path
path = "/pet/findByStatus".sub('{format}','json')
@ -144,7 +141,6 @@ module Petstore
Swagger.logger.debug "Calling API: PetApi#find_pets_by_tags ..."
end
# resource path
path = "/pet/findByTags".sub('{format}','json')
@ -190,8 +186,7 @@ module Petstore
end
# verify the required parameter 'pet_id' is set
raise "Missing the required parameter 'pet_id' when calling get_pet_by_id" if pet_id.nil?
fail "Missing the required parameter 'pet_id' when calling get_pet_by_id" if pet_id.nil?
# resource path
path = "/pet/{petId}".sub('{format}','json').sub('{' + 'petId' + '}', pet_id.to_s)
@ -239,8 +234,7 @@ module Petstore
end
# verify the required parameter 'pet_id' is set
raise "Missing the required parameter 'pet_id' when calling update_pet_with_form" if pet_id.nil?
fail "Missing the required parameter 'pet_id' when calling update_pet_with_form" if pet_id.nil?
# resource path
path = "/pet/{petId}".sub('{format}','json').sub('{' + 'petId' + '}', pet_id.to_s)
@ -288,8 +282,7 @@ module Petstore
end
# verify the required parameter 'pet_id' is set
raise "Missing the required parameter 'pet_id' when calling delete_pet" if pet_id.nil?
fail "Missing the required parameter 'pet_id' when calling delete_pet" if pet_id.nil?
# resource path
path = "/pet/{petId}".sub('{format}','json').sub('{' + 'petId' + '}', pet_id.to_s)
@ -337,8 +330,7 @@ module Petstore
end
# verify the required parameter 'pet_id' is set
raise "Missing the required parameter 'pet_id' when calling upload_file" if pet_id.nil?
fail "Missing the required parameter 'pet_id' when calling upload_file" if pet_id.nil?
# resource path
path = "/pet/{petId}/uploadImage".sub('{format}','json').sub('{' + 'petId' + '}', pet_id.to_s)

View File

@ -12,7 +12,6 @@ module Petstore
Swagger.logger.debug "Calling API: StoreApi#get_inventory ..."
end
# resource path
path = "/store/inventory".sub('{format}','json')
@ -56,7 +55,6 @@ module Petstore
Swagger.logger.debug "Calling API: StoreApi#place_order ..."
end
# resource path
path = "/store/order".sub('{format}','json')
@ -101,8 +99,7 @@ module Petstore
end
# verify the required parameter 'order_id' is set
raise "Missing the required parameter 'order_id' when calling get_order_by_id" if order_id.nil?
fail "Missing the required parameter 'order_id' when calling get_order_by_id" if order_id.nil?
# resource path
path = "/store/order/{orderId}".sub('{format}','json').sub('{' + 'orderId' + '}', order_id.to_s)
@ -148,8 +145,7 @@ module Petstore
end
# verify the required parameter 'order_id' is set
raise "Missing the required parameter 'order_id' when calling delete_order" if order_id.nil?
fail "Missing the required parameter 'order_id' when calling delete_order" if order_id.nil?
# resource path
path = "/store/order/{orderId}".sub('{format}','json').sub('{' + 'orderId' + '}', order_id.to_s)

View File

@ -13,7 +13,6 @@ module Petstore
Swagger.logger.debug "Calling API: UserApi#create_user ..."
end
# resource path
path = "/user".sub('{format}','json')
@ -56,7 +55,6 @@ module Petstore
Swagger.logger.debug "Calling API: UserApi#create_users_with_array_input ..."
end
# resource path
path = "/user/createWithArray".sub('{format}','json')
@ -99,7 +97,6 @@ module Petstore
Swagger.logger.debug "Calling API: UserApi#create_users_with_list_input ..."
end
# resource path
path = "/user/createWithList".sub('{format}','json')
@ -143,7 +140,6 @@ module Petstore
Swagger.logger.debug "Calling API: UserApi#login_user ..."
end
# resource path
path = "/user/login".sub('{format}','json')
@ -188,7 +184,6 @@ module Petstore
Swagger.logger.debug "Calling API: UserApi#logout_user ..."
end
# resource path
path = "/user/logout".sub('{format}','json')
@ -232,8 +227,7 @@ module Petstore
end
# verify the required parameter 'username' is set
raise "Missing the required parameter 'username' when calling get_user_by_name" if username.nil?
fail "Missing the required parameter 'username' when calling get_user_by_name" if username.nil?
# resource path
path = "/user/{username}".sub('{format}','json').sub('{' + 'username' + '}', username.to_s)
@ -280,8 +274,7 @@ module Petstore
end
# verify the required parameter 'username' is set
raise "Missing the required parameter 'username' when calling update_user" if username.nil?
fail "Missing the required parameter 'username' when calling update_user" if username.nil?
# resource path
path = "/user/{username}".sub('{format}','json').sub('{' + 'username' + '}', username.to_s)
@ -326,8 +319,7 @@ module Petstore
end
# verify the required parameter 'username' is set
raise "Missing the required parameter 'username' when calling delete_user" if username.nil?
fail "Missing the required parameter 'username' when calling delete_user" if username.nil?
# resource path
path = "/user/{username}".sub('{format}','json').sub('{' + 'username' + '}', username.to_s)