use of io.swagger.models.parameters.PathParameter in project endpoints-java by cloudendpoints.
the class SwaggerGenerator method writeApiMethod.
private void writeApiMethod(ApiMethodConfig methodConfig, ApiConfig apiConfig, Swagger swagger, GenerationContext genCtx) throws ApiConfigException {
Path path = getOrCreatePath(swagger, methodConfig);
Operation operation = new Operation();
operation.setOperationId(getOperationId(apiConfig, methodConfig));
operation.setDescription(methodConfig.getDescription());
Collection<String> pathParameters = methodConfig.getPathParameters();
for (ApiParameterConfig parameterConfig : methodConfig.getParameterConfigs()) {
switch(parameterConfig.getClassification()) {
case API_PARAMETER:
boolean isPathParameter = pathParameters.contains(parameterConfig.getName());
SerializableParameter parameter = isPathParameter ? new PathParameter() : new QueryParameter();
parameter.setName(parameterConfig.getName());
parameter.setDescription(parameterConfig.getDescription());
boolean required = isPathParameter || (!parameterConfig.getNullable() && parameterConfig.getDefaultValue() == null);
if (parameterConfig.isRepeated()) {
TypeToken<?> t = parameterConfig.getRepeatedItemSerializedType();
parameter.setType("array");
Property p = getSwaggerArrayProperty(t);
if (parameterConfig.isEnum()) {
// TODO: Not sure if this is the right check
((StringProperty) p).setEnum(getEnumValues(t));
}
parameter.setItems(p);
} else if (parameterConfig.isEnum()) {
parameter.setType("string");
parameter.setEnum(getEnumValues(parameterConfig.getType()));
parameter.setRequired(required);
} else {
parameter.setType(TYPE_TO_STRING_MAP.get(parameterConfig.getSchemaBaseType().getType()));
parameter.setFormat(TYPE_TO_FORMAT_MAP.get(parameterConfig.getSchemaBaseType().getType()));
parameter.setRequired(required);
}
operation.parameter(parameter);
break;
case RESOURCE:
TypeToken<?> requestType = parameterConfig.getSchemaBaseType();
Schema schema = genCtx.schemata.getOrAdd(requestType, apiConfig);
BodyParameter bodyParameter = new BodyParameter();
bodyParameter.setName("body");
bodyParameter.setSchema(new RefModel(schema.name()));
operation.addParameter(bodyParameter);
break;
case UNKNOWN:
throw new IllegalArgumentException("Unclassifiable parameter type found.");
case INJECTED:
// Do nothing, these are synthetic and created by the framework.
break;
}
}
Response response = new Response().description("A successful response");
if (methodConfig.hasResourceInResponse()) {
TypeToken<?> returnType = ApiAnnotationIntrospector.getSchemaType(methodConfig.getReturnType(), apiConfig);
Schema schema = genCtx.schemata.getOrAdd(returnType, apiConfig);
response.setSchema(new RefProperty(schema.name()));
}
operation.response(200, response);
writeAuthConfig(swagger, methodConfig, operation);
if (methodConfig.isApiKeyRequired()) {
List<Map<String, List<String>>> security = operation.getSecurity();
// security requirements, add a new one for just the API key.
if (security != null) {
for (Map<String, List<String>> securityEntry : security) {
securityEntry.put(API_KEY, ImmutableList.<String>of());
}
} else {
operation.addSecurity(API_KEY, ImmutableList.<String>of());
}
Map<String, SecuritySchemeDefinition> definitions = swagger.getSecurityDefinitions();
if (definitions == null || !definitions.containsKey(API_KEY)) {
swagger.securityDefinition(API_KEY, new ApiKeyAuthDefinition(API_KEY_PARAM, In.QUERY));
}
}
path.set(methodConfig.getHttpMethod().toLowerCase(), operation);
addDefinedMetricCosts(genCtx.limitMetrics, operation, methodConfig.getMetricCosts());
}
Aggregations