Search in sources :

Example 41 with Response

use of io.swagger.models.Response in project java-chassis by ServiceComb.

the class TestApiResponse method checkResponseHeader.

@Test
public void checkResponseHeader() {
    SwaggerOperation swaggerOperation = swaggerOperations.findOperation("testResponseHeader");
    Assert.assertEquals("/testResponseHeader", swaggerOperation.getPath());
    Response response = swaggerOperation.getOperation().getResponses().get("200");
    Property property = response.getHeaders().get("k1");
    Assert.assertEquals("integer", property.getType());
    Assert.assertEquals("int32", property.getFormat());
}
Also used : Response(io.swagger.models.Response) ApiResponse(io.swagger.annotations.ApiResponse) SwaggerOperation(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation) Property(io.swagger.models.properties.Property) Test(org.junit.Test)

Example 42 with Response

use of io.swagger.models.Response in project java-chassis by ServiceComb.

the class TestApiResponse method checkResponseDesc.

@Test
public void checkResponseDesc() {
    SwaggerOperation swaggerOperation = swaggerOperations.findOperation("testMulti");
    Assert.assertEquals("/testMulti", swaggerOperation.getPath());
    Response response1 = swaggerOperation.getOperation().getResponses().get("200");
    Response response2 = swaggerOperation.getOperation().getResponses().get("301");
    Assert.assertEquals("msg1", response1.getDescription());
    Assert.assertEquals("msg2", response2.getDescription());
}
Also used : Response(io.swagger.models.Response) ApiResponse(io.swagger.annotations.ApiResponse) SwaggerOperation(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation) Test(org.junit.Test)

Example 43 with Response

use of io.swagger.models.Response in project java-chassis by ServiceComb.

the class TestOperationGenerator method apiOperationThenResponse.

@Test
public void apiOperationThenResponse() throws NoSuchMethodException {
    SwaggerOperation swaggerOperation = swaggerOperations.findOperation("apiOperationThenResponse");
    List<String> tags = swaggerOperation.getOperation().getTags();
    assertThat(tags, contains("tag1", "tag2"));
    Response response = swaggerOperation.getOperation().getResponses().get("200");
    assertEquals("200 is ok............", response.getDescription());
    Assert.assertNull(response.getHeaders().get("x-user-domain"));
    Assert.assertNotNull(response.getHeaders().get("x-user-name"));
    Assert.assertNotNull(swaggerOperation.getOperation().getVendorExtensions().get("x-class-name"));
}
Also used : Response(io.swagger.models.Response) ApiResponse(io.swagger.annotations.ApiResponse) SwaggerOperation(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation) Test(org.junit.Test)

Example 44 with Response

use of io.swagger.models.Response in project java-chassis by ServiceComb.

the class AbstractOperationGenerator method scanResponse.

public void scanResponse() {
    if (swaggerOperation.getResponses() != null) {
        Response successResponse = swaggerOperation.getResponses().get(SwaggerConst.SUCCESS_KEY);
        if (successResponse != null) {
            if (successResponse.getResponseSchema() == null) {
                // 标注已经定义了response,但是是void,这可能是在标注上未定义
                // 根据函数原型来处理response
                Model model = createResponseModel();
                successResponse.setResponseSchema(model);
            }
            return;
        }
    }
    Model model = createResponseModel();
    Response response = new Response();
    response.setResponseSchema(model);
    swaggerOperation.addResponse(SwaggerConst.SUCCESS_KEY, response);
}
Also used : Response(io.swagger.models.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Model(io.swagger.models.Model)

Example 45 with Response

use of io.swagger.models.Response in project java-chassis by ServiceComb.

the class ResponsesMeta method init.

public void init(Swagger swagger, Operation operation) {
    if (responseMap.isEmpty()) {
        responseMap.put(Status.OK.getStatusCode(), OBJECT_JAVA_TYPE);
        initGlobalDefaultMapper();
    }
    for (Entry<String, Response> entry : operation.getResponses().entrySet()) {
        JavaType javaType = ConverterMgr.findJavaType(swagger, entry.getValue().getResponseSchema());
        if ("default".equals(entry.getKey())) {
            defaultResponse = javaType;
            continue;
        }
        Integer statusCode = Integer.parseInt(entry.getKey());
        JavaType existing = responseMap.get(statusCode);
        if (existing == null || !isVoid(javaType)) {
            responseMap.put(statusCode, javaType);
        }
    }
    responseMap.putIfAbsent(ExceptionFactory.CONSUMER_INNER_STATUS_CODE, COMMON_EXCEPTION_JAVA_TYPE);
    responseMap.putIfAbsent(ExceptionFactory.PRODUCER_INNER_STATUS_CODE, COMMON_EXCEPTION_JAVA_TYPE);
    responseMap.putIfAbsent(Status.TOO_MANY_REQUESTS.getStatusCode(), COMMON_EXCEPTION_JAVA_TYPE);
    responseMap.putIfAbsent(Status.REQUEST_TIMEOUT.getStatusCode(), COMMON_EXCEPTION_JAVA_TYPE);
    responseMap.putIfAbsent(Status.SERVICE_UNAVAILABLE.getStatusCode(), COMMON_EXCEPTION_JAVA_TYPE);
    if (defaultResponse == null) {
        // swagger中没有定义default,加上default专用于处理exception
        defaultResponse = OBJECT_JAVA_TYPE;
    }
}
Also used : Response(io.swagger.models.Response) JavaType(com.fasterxml.jackson.databind.JavaType)

Aggregations

Response (io.swagger.models.Response)97 Operation (io.swagger.models.Operation)53 Property (io.swagger.models.properties.Property)31 Path (io.swagger.models.Path)30 Swagger (io.swagger.models.Swagger)30 ApiResponse (io.swagger.annotations.ApiResponse)24 Test (org.testng.annotations.Test)23 RefProperty (io.swagger.models.properties.RefProperty)22 ArrayProperty (io.swagger.models.properties.ArrayProperty)21 Test (org.junit.Test)17 BodyParameter (io.swagger.models.parameters.BodyParameter)15 Parameter (io.swagger.models.parameters.Parameter)14 MapProperty (io.swagger.models.properties.MapProperty)14 HashMap (java.util.HashMap)13 Model (io.swagger.models.Model)12 StringProperty (io.swagger.models.properties.StringProperty)12 RefModel (io.swagger.models.RefModel)11 PathParameter (io.swagger.models.parameters.PathParameter)11 QueryParameter (io.swagger.models.parameters.QueryParameter)10 IntegerProperty (io.swagger.models.properties.IntegerProperty)10