Search in sources :

Example 56 with Response

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());
}
Also used : Path(io.swagger.models.Path) Response(io.swagger.models.Response) ApiResponse(io.swagger.annotations.ApiResponse) Swagger(io.swagger.models.Swagger) Operation(io.swagger.models.Operation) Property(io.swagger.models.properties.Property)

Example 57 with Response

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);
}
Also used : Response(io.swagger.models.Response) ApiResponse(io.swagger.annotations.ApiResponse) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty)

Example 58 with 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());
        }
    }
}
Also used : HashMap(java.util.HashMap) ApiResponse(io.swagger.annotations.ApiResponse) RefProperty(io.swagger.models.properties.RefProperty) Response(io.swagger.models.Response) ApiResponse(io.swagger.annotations.ApiResponse) Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) ApiOperation(io.swagger.annotations.ApiOperation) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) RefProperty(io.swagger.models.properties.RefProperty) Map(java.util.Map) HashMap(java.util.HashMap) ApiResponses(io.swagger.annotations.ApiResponses)

Example 59 with Response

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);
        }
    }
}
Also used : Path(io.swagger.models.Path) Method(java.lang.reflect.Method) Operation(io.swagger.models.Operation) Response(io.swagger.models.Response) Type(java.lang.reflect.Type) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition)

Example 60 with Response

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());
}
Also used : Response(io.swagger.models.Response) ApiOperation(io.swagger.annotations.ApiOperation) Operation(io.swagger.models.Operation)

Aggregations

Response (io.swagger.models.Response)93 Operation (io.swagger.models.Operation)51 Property (io.swagger.models.properties.Property)30 Path (io.swagger.models.Path)29 Swagger (io.swagger.models.Swagger)29 ApiResponse (io.swagger.annotations.ApiResponse)24 Test (org.testng.annotations.Test)23 RefProperty (io.swagger.models.properties.RefProperty)21 ArrayProperty (io.swagger.models.properties.ArrayProperty)20 Test (org.junit.Test)17 MapProperty (io.swagger.models.properties.MapProperty)14 Parameter (io.swagger.models.parameters.Parameter)13 Model (io.swagger.models.Model)12 BodyParameter (io.swagger.models.parameters.BodyParameter)12 StringProperty (io.swagger.models.properties.StringProperty)12 HashMap (java.util.HashMap)12 RefModel (io.swagger.models.RefModel)10 PathParameter (io.swagger.models.parameters.PathParameter)10 IntegerProperty (io.swagger.models.properties.IntegerProperty)10 QueryParameter (io.swagger.models.parameters.QueryParameter)9