use of io.swagger.models.Response in project java-chassis by ServiceComb.
the class TestApiResponse method checkResponseHeader.
private void checkResponseHeader(SwaggerGenerator generator) {
Swagger swagger = generator.getSwagger();
Path path = swagger.getPaths().get("/testResponseHeader");
Operation operation = path.getOperations().get(0);
Assert.assertEquals("testResponseHeader", operation.getOperationId());
Response response = operation.getResponses().get("200");
Property property = response.getHeaders().get("k1");
Assert.assertEquals(Integer.class, ConverterMgr.findJavaType(generator, property).getRawClass());
}
use of io.swagger.models.Response in project java-chassis by ServiceComb.
the class AnnotationUtils method generateResponse.
private static void generateResponse(Swagger swagger, ResponseConfig responseConfig) {
Response response = new Response();
Property property = generateResponseProperty(swagger, responseConfig);
response.setSchema(property);
if (responseConfig.getResponseHeaders() != null) {
Map<String, Property> headers = generateResponseHeader(swagger, responseConfig.getResponseHeaders());
response.setHeaders(headers);
}
responseConfig.setResponse(response);
}
use of io.swagger.models.Response in project vertx-swagger by bobxwang.
the class RouteReaderExtension method applyResponses.
@Override
public void applyResponses(ReaderContext context, Operation operation, Method method) {
final Map<Integer, Response> result = new HashMap<>();
final ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
if (apiOperation != null && StringUtils.isNotBlank(apiOperation.responseReference())) {
final Response response = new Response().description(SUCCESSFUL_OPERATION);
response.schema(new RefProperty(apiOperation.responseReference()));
result.put(apiOperation.code(), response);
}
final Type responseType = getResponseType(method);
if (isValidResponse(responseType)) {
final Property property = ModelConverters.getInstance().readAsProperty(responseType);
if (property != null) {
final Property responseProperty = ContainerWrapper.wrapContainer(getResponseContainer(apiOperation), property);
final int responseCode = apiOperation == null ? 200 : apiOperation.code();
final Map<String, Property> defaultResponseHeaders = apiOperation == null ? Collections.emptyMap() : parseResponseHeaders(context, apiOperation.responseHeaders());
final Response response = new Response().description(SUCCESSFUL_OPERATION).schema(responseProperty).headers(defaultResponseHeaders);
result.put(responseCode, response);
appendModels(context.getSwagger(), responseType);
}
}
final ApiResponses responseAnnotation = ReflectionUtils.getAnnotation(method, ApiResponses.class);
if (responseAnnotation != null) {
for (ApiResponse apiResponse : responseAnnotation.value()) {
final Map<String, Property> responseHeaders = parseResponseHeaders(context, apiResponse.responseHeaders());
final Response response = new Response().description(apiResponse.message()).headers(responseHeaders);
if (StringUtils.isNotEmpty(apiResponse.reference())) {
response.schema(new RefProperty(apiResponse.reference()));
} else if (!ReflectionUtils.isVoid(apiResponse.response())) {
final Type type = apiResponse.response();
final Property property = ModelConverters.getInstance().readAsProperty(type);
if (property != null) {
response.schema(ContainerWrapper.wrapContainer(apiResponse.responseContainer(), property));
appendModels(context.getSwagger(), type);
}
}
result.put(apiResponse.code(), response);
}
}
for (Map.Entry<Integer, Response> responseEntry : result.entrySet()) {
if (responseEntry.getKey() == 0) {
operation.defaultResponse(responseEntry.getValue());
} else {
operation.response(responseEntry.getKey(), responseEntry.getValue());
}
}
}
use of io.swagger.models.Response in project vertx-swagger by bobxwang.
the class Reader method read.
private void read(ReaderContext context) {
Class<?> clasz = context.getCls();
final SwaggerDefinition swaggerDefinition = clasz.getAnnotation(SwaggerDefinition.class);
if (swaggerDefinition != null) {
readSwaggerConfig(swaggerDefinition);
}
for (Method method : clasz.getDeclaredMethods()) {
if (!method.isAnnotationPresent(BBRouter.class)) {
continue;
}
final Operation operation = new Operation();
String operationPath = null;
String httpMethod = null;
final Type[] genericParameterTypes = method.getGenericParameterTypes();
final Annotation[][] paramAnnotations = method.getParameterAnnotations();
for (ReaderExtension extension : ReaderExtensions.getExtensions()) {
if (operationPath == null) {
operationPath = extension.getPath(context, method);
}
if (httpMethod == null) {
httpMethod = extension.getHttpMethod(context, method);
}
if (operationPath == null || httpMethod == null) {
continue;
}
if (extension.isReadable(context)) {
extension.setDeprecated(operation, method);
extension.applyConsumes(context, operation, method);
extension.applyProduces(context, operation, method);
extension.applyOperationId(operation, method);
extension.applySummary(operation, method);
extension.applyDescription(operation, method);
extension.applySchemes(context, operation, method);
extension.applySecurityRequirements(context, operation, method);
extension.applyTags(context, operation, method);
extension.applyResponses(context, operation, method);
extension.applyImplicitParameters(context, operation, method);
extension.applyExtensions(context, operation, method);
for (int i = 0; i < genericParameterTypes.length; i++) {
extension.applyParameters(context, operation, genericParameterTypes[i], paramAnnotations[i]);
}
}
}
if (httpMethod != null) {
if (operation.getResponses() == null) {
operation.defaultResponse(new Response().description("successful operation"));
}
final Map<String, String> regexMap = new HashMap<>();
final String parsedPath = PathUtils.parsePath(operationPath, regexMap);
final String swaggerPath = getSwaggerPath(parsedPath);
Path path = swagger.getPath(swaggerPath);
if (path == null) {
path = new Path();
swagger.path(swaggerPath, path);
}
path.set(httpMethod.toLowerCase(), operation);
}
}
}
use of io.swagger.models.Response in project incubator-servicecomb-java-chassis by apache.
the class TestApiOperation method testBase.
private void testBase(Path path) {
Assert.assertEquals(1, path.getOperations().size());
Operation operation = path.getGet();
Assert.assertEquals("summary", operation.getSummary());
Assert.assertEquals("notes", operation.getDescription());
Assert.assertEquals(Arrays.asList("tag1", "tag2"), operation.getTags());
Assert.assertEquals(Arrays.asList("application/json"), operation.getProduces());
Assert.assertEquals(Arrays.asList("application/json"), operation.getConsumes());
Assert.assertEquals(Arrays.asList(Scheme.HTTP, Scheme.HTTPS), operation.getSchemes());
Map<String, Response> responseMap = operation.getResponses();
Assert.assertEquals(2, responseMap.size());
Response response = responseMap.get(SwaggerConst.SUCCESS_KEY);
Assert.assertNotNull(response);
Assert.assertEquals(null, response.getSchema());
response = responseMap.get("202");
Assert.assertNotNull(response);
Assert.assertEquals(null, response.getSchema());
Assert.assertEquals(1, response.getHeaders().size());
Assert.assertEquals("integer", response.getHeaders().get("h1").getType());
}
Aggregations