use of io.swagger.models.Response in project herd by FINRAOS.
the class RestControllerProcessor method processRestMethodReturnValue.
/**
* Processes the return value of a RequestMapping annotated method.
*
* @param returnType the return type.
* @param operation the operation.
* @param returnDescription the description of the return value.
*
* @throws MojoExecutionException if the return type isn't an XmlType.
*/
private void processRestMethodReturnValue(Class<?> returnType, Operation operation, String returnDescription) throws MojoExecutionException {
log.debug("Processing REST method return value \"" + returnType.getName() + "\".");
// Add the class name to the list of classes which we will create an example for.
exampleClassNames.add(returnType.getSimpleName());
// Add the success response
operation.response(200, new Response().description(returnDescription == null ? "Success" : returnDescription).schema(new RefProperty(getXmlType(returnType).name().trim())));
// If we have an error class, add that as the default response.
if (modelErrorClass != null) {
operation.defaultResponse(new Response().description("General Error").schema(new RefProperty(getXmlType(modelErrorClass).name().trim())));
}
}
use of io.swagger.models.Response in project sql-boot by sql-boot.
the class ApiController method getSwaggerDescription.
private Swagger getSwaggerDescription(HttpServletRequest request, String connectionName) {
FsResourceTypes fsResourceTypes = new FsResourceTypes(dbConnectionList.getConnectionByName(connectionName));
final List<ResourceType> resourceTypes = fsResourceTypes.resourceTypes();
Swagger swagger = new Swagger();
swagger.consumes("application/json");
swagger.host(request.getServerName() + ":" + request.getServerPort());
swagger.setInfo(new Info().version("v1").title("API specification"));
swagger.setSchemes(asList(Scheme.HTTP, Scheme.HTTPS));
swagger.path("/connections", new Path().get(new Operation().tag("connections").response(200, new Response().description("Ok").schema(new ArrayProperty(new RefProperty("connection")))).produces("application/json")));
swagger.model("connection", new ModelImpl().property("name", new StringProperty().description("name")).property("url", new StringProperty().description("url")).property("user", new StringProperty().description("user")).property("driverClassName", new StringProperty().description("driverClassName")).property("configurationFolder", new StringProperty().description("configurationFolder")));
// paths
for (ResourceType resourceType : resourceTypes) {
PathParameter parameter = new PathParameter().required(true).type("string").name("connection_name");
parameter.setDefaultValue(connectionName);
swagger.path("/api/{connection_name}/headers/" + resourceType.name(), new Path().get(new Operation().description(resourceType.name()).tag("db_objects").parameter(parameter).parameter(new QueryParameter().name("select").type("string")).parameter(new QueryParameter().name("distinct").type("boolean")).parameter(new QueryParameter().name("where").type("string")).parameter(new QueryParameter().name("page").type("string").description("get page by mask [page_count:page_size]")).parameter(new QueryParameter().name("limit").type("integer")).parameter(new QueryParameter().name("orderby").type("string")).parameter(new QueryParameter().name("cache").type("boolean")).response(200, new Response().description("Ok").schema(new ArrayProperty(new RefProperty(resourceType.name())))).produces("application/json")));
final List<String> path = resourceType.path();
final List<String> newPath = new ArrayList<>();
for (String s : path) {
newPath.add(s + "_name");
List<Parameter> parameterList = new ArrayList<>();
PathParameter parameterConnection = new PathParameter().required(true).type("string").name("connection_name");
parameterConnection.setDefaultValue(connectionName);
parameterList.add(parameterConnection);
for (String s1 : newPath) {
final PathParameter pathParameter = new PathParameter().required(true).type("string").name(s1);
pathParameter.setDefaultValue("*");
parameterList.add(pathParameter);
}
Operation operation = new Operation();
operation.setParameters(parameterList);
operation.parameter(new QueryParameter().name("select").type("string"));
operation.parameter(new QueryParameter().name("distinct").type("boolean"));
operation.parameter(new QueryParameter().name("where").type("string"));
operation.parameter(new QueryParameter().name("page").type("string").description("get page by mask [page_count:page_size]"));
operation.parameter(new QueryParameter().name("limit").type("integer"));
operation.parameter(new QueryParameter().name("orderby").type("string"));
operation.parameter(new QueryParameter().name("cache").type("boolean"));
swagger.path("/api/{connection_name}/headers/" + resourceType.name() + "/" + newPath.stream().map(v -> "{" + v + "}").collect(joining(".")), new Path().get(operation.description(resourceType.name()).tag("db_objects").response(200, new Response().description("Ok").schema(new ArrayProperty(new RefProperty(resourceType.name())))).produces("application/json")));
}
}
// definitions
for (ResourceType resourceType : resourceTypes) {
ModelImpl model = new ModelImpl();
Map<String, String> stringStringMap = resourceType.metaData();
if (stringStringMap != null) {
Set<Entry<String, String>> entries = stringStringMap.entrySet();
for (Entry<String, String> stringStringEntry : entries) {
model.property(stringStringEntry.getKey(), new StringProperty().description(stringStringEntry.getValue()));
}
}
swagger.model(resourceType.name(), model);
}
return swagger;
}
use of io.swagger.models.Response 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());
}
use of io.swagger.models.Response in project swagger-parser by swagger-api.
the class OperationProcessor method processOperation.
public void processOperation(Operation operation) {
final List<Parameter> processedOperationParameters = parameterProcessor.processParameters(operation.getParameters());
operation.setParameters(processedOperationParameters);
final Map<String, Response> responses = operation.getResponses();
if (responses != null) {
for (String responseCode : responses.keySet()) {
Response response = responses.get(responseCode);
if (response != null) {
if (response instanceof RefResponse) {
RefResponse refResponse = (RefResponse) response;
Response resolvedResponse = cache.loadRef(refResponse.get$ref(), refResponse.getRefFormat(), Response.class);
if (resolvedResponse != null) {
response = resolvedResponse;
responses.put(responseCode, resolvedResponse);
}
}
responseProcessor.processResponse(response);
}
}
}
}
use of io.swagger.models.Response in project swagger-parser by swagger-api.
the class ResolverCacheTest method testLoadInternalResponseRefWithSpaces.
@Test
public void testLoadInternalResponseRefWithSpaces(@Injectable Response mockedResponse) throws Exception {
Swagger swagger = new Swagger();
Map<String, Response> responses = new HashMap<>();
responses.put("foo bar", mockedResponse);
swagger.setResponses(responses);
ResolverCache cache = new ResolverCache(swagger, auths, null);
Response actualResult = cache.loadRef("#/responses/foo bar", RefFormat.INTERNAL, Response.class);
assertEquals(actualResult, mockedResponse);
}
Aggregations