Search in sources :

Example 1 with Response

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

the class ClassUtils method createInterface.

private static Class<?> createInterface(Swagger swagger, ClassLoader classLoader, String packageName, String intfName) {
    ClassConfig classConfig = new ClassConfig();
    classConfig.setClassName(intfName);
    classConfig.setIntf(true);
    StringBuilder sbMethod = new StringBuilder();
    StringBuilder sbMethodGenericSignature = new StringBuilder();
    for (Path path : swagger.getPaths().values()) {
        for (Operation operation : path.getOperations()) {
            boolean hasGenericSignature = false;
            sbMethod.setLength(0);
            sbMethodGenericSignature.setLength(0);
            Response result = operation.getResponses().get(SwaggerConst.SUCCESS_KEY);
            JavaType resultJavaType = ConverterMgr.findJavaType(classLoader, packageName, swagger, result.getSchema());
            hasGenericSignature = hasGenericSignature || resultJavaType.hasGenericTypes();
            sbMethod.append(JavassistUtils.getNameForGenerateCode(resultJavaType)).append(" ").append(operation.getOperationId()).append("(");
            sbMethodGenericSignature.append("(");
            for (Parameter parameter : operation.getParameters()) {
                String paramName = parameter.getName();
                paramName = correctMethodParameterName(paramName);
                JavaType paramJavaType = ConverterMgr.findJavaType(classLoader, packageName, swagger, parameter);
                hasGenericSignature = hasGenericSignature || paramJavaType.hasGenericTypes();
                String code = String.format("%s %s,", paramJavaType.getRawClass().getName(), paramName);
                sbMethod.append(code);
                sbMethodGenericSignature.append(paramJavaType.getGenericSignature());
            }
            if (!operation.getParameters().isEmpty()) {
                sbMethod.setLength(sbMethod.length() - 1);
            }
            sbMethod.append(");");
            sbMethodGenericSignature.append(")");
            sbMethodGenericSignature.append(resultJavaType.getGenericSignature());
            if (hasGenericSignature) {
                classConfig.addMethod(sbMethod.toString(), sbMethodGenericSignature.toString());
            } else {
                classConfig.addMethod(sbMethod.toString(), null);
            }
        }
    }
    return JavassistUtils.createClass(classLoader, classConfig);
}
Also used : Path(io.swagger.models.Path) Response(io.swagger.models.Response) JavaType(com.fasterxml.jackson.databind.JavaType) ClassConfig(io.servicecomb.common.javassist.ClassConfig) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) Operation(io.swagger.models.Operation)

Example 2 with Response

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

the class OperationGenerator method scanResponse.

public void scanResponse() {
    if (operation.getResponses() != null) {
        Response successResponse = operation.getResponses().get(SwaggerConst.SUCCESS_KEY);
        if (successResponse != null) {
            if (successResponse.getSchema() == null) {
                // 标注已经定义了response,但是是void,这可能是在标注上未定义
                // 根据函数原型来处理response
                Property property = createResponseProperty();
                successResponse.setSchema(property);
            }
            return;
        }
    }
    Property property = createResponseProperty();
    Response response = new Response();
    response.setSchema(property);
    operation.addResponse(SwaggerConst.SUCCESS_KEY, response);
}
Also used : Response(io.swagger.models.Response) Property(io.swagger.models.properties.Property)

Example 3 with Response

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

the class TestApiResponse method checkApiResponseHeader.

private void checkApiResponseHeader(SwaggerGenerator generator) {
    Swagger swagger = generator.getSwagger();
    Path path = swagger.getPaths().get("/testApiResponseHeader");
    Operation operation = path.getOperations().get(0);
    Assert.assertEquals("testApiResponseHeader", operation.getOperationId());
    Response response = operation.getResponses().get("200");
    Property property = response.getHeaders().get("k1");
    Assert.assertEquals(Integer.class, ConverterMgr.findJavaType(generator, property).getRawClass());
    property = response.getHeaders().get("k2");
    Assert.assertEquals(String.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 4 with Response

use of io.swagger.models.Response in project swagger-core by swagger-api.

the class SimpleScannerWithDecoratorExtensionTest method scanSimpleResourceWithDecorator.

/**
     * Test for method annotated with vendor annotation which could be used for swagger documentation.
     */
@Test(description = "scan a simple resource with custom decorator")
public void scanSimpleResourceWithDecorator() {
    final Swagger swagger = getSwagger(SimpleResourceWithVendorAnnotation.class);
    assertEquals(swagger.getPaths().size(), 2);
    final Operation get = getGet(swagger, "/{id}");
    assertNotNull(get);
    assertEquals(get.getParameters().size(), 2);
    final Response response = get.getResponses().get(RESPONSE_STATUS_401);
    assertNotNull(response);
    assertEquals(response.getDescription(), RESPONSE_DESCRIPTION);
}
Also used : Response(io.swagger.models.Response) Swagger(io.swagger.models.Swagger) Operation(io.swagger.models.Operation) Test(org.testng.annotations.Test)

Example 5 with Response

use of io.swagger.models.Response in project swagger-core by swagger-api.

the class SimpleReaderTest method scanResourceWithMapReturnType.

@Test(description = "scan a resource with map return type")
public void scanResourceWithMapReturnType() {
    Swagger swagger = getSwagger(ResourceWithMapReturnValue.class);
    Operation get = getGet(swagger, "/{id}");
    assertNotNull(get);
    assertNotNull(get.getResponses());
    Response response = get.getResponses().get("200");
    assertNotNull(response);
    Property schema = response.getSchema();
    assertEquals(schema.getClass(), MapProperty.class);
}
Also used : ResourceWithResponse(io.swagger.resources.ResourceWithResponse) Response(io.swagger.models.Response) Swagger(io.swagger.models.Swagger) Operation(io.swagger.models.Operation) NicknamedOperation(io.swagger.resources.NicknamedOperation) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) RefProperty(io.swagger.models.properties.RefProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) Test(org.testng.annotations.Test)

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