Search in sources :

Example 1 with Operation

use of io.swagger.models.Operation 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 Operation

use of io.swagger.models.Operation 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 3 with Operation

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

the class RestOperationMeta method init.

public void init(OperationMeta operationMeta) {
    this.operationMeta = operationMeta;
    Swagger swagger = operationMeta.getSchemaMeta().getSwagger();
    Operation operation = operationMeta.getSwaggerOperation();
    this.produces = operation.getProduces();
    if (produces == null) {
        this.produces = swagger.getProduces();
    }
    setAbsolutePath(concatPath(swagger.getBasePath(), operationMeta.getOperationPath()));
    this.createProduceProcessors();
    Method method = operationMeta.getMethod();
    Type[] genericParamTypes = method.getGenericParameterTypes();
    if (genericParamTypes.length != operation.getParameters().size()) {
        throw new Error("Param count is not equal between swagger and method,  path=" + absolutePath);
    }
    // 初始化所有rest param
    for (int idx = 0; idx < genericParamTypes.length; idx++) {
        Parameter parameter = operation.getParameters().get(idx);
        Type genericParamType = genericParamTypes[idx];
        RestParam param = new RestParam(idx, parameter, genericParamType);
        addParam(param);
    }
    this.pathBuilder = new URLPathBuilder(absolutePath, paramMap);
}
Also used : MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) Swagger(io.swagger.models.Swagger) Parameter(io.swagger.models.parameters.Parameter) URLPathBuilder(io.servicecomb.common.rest.definition.path.URLPathBuilder) Operation(io.swagger.models.Operation) Method(java.lang.reflect.Method)

Example 4 with Operation

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

the class SimpleReaderTest method scanResourceWithCustomHttpMethodAnnotations.

@Test(description = "scan a resource with custom http method annotations")
public void scanResourceWithCustomHttpMethodAnnotations() {
    Swagger swagger = getSwagger(ResourceWithCustomHTTPMethodAnnotations.class);
    Operation get = getGet(swagger, "/");
    assertNotNull(get);
    Operation post = getPost(swagger, "/");
    assertNotNull(post);
    Operation patch = getPatch(swagger, "/");
    assertNotNull(patch);
    Operation put = getPut(swagger, "/");
    assertNotNull(put);
    Operation delete = getDelete(swagger, "/");
    assertNotNull(delete);
}
Also used : Swagger(io.swagger.models.Swagger) Operation(io.swagger.models.Operation) NicknamedOperation(io.swagger.resources.NicknamedOperation) Test(org.testng.annotations.Test)

Example 5 with Operation

use of io.swagger.models.Operation 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)

Aggregations

Operation (io.swagger.models.Operation)214 Path (io.swagger.models.Path)87 Test (org.testng.annotations.Test)85 Swagger (io.swagger.models.Swagger)82 Response (io.swagger.models.Response)57 BodyParameter (io.swagger.models.parameters.BodyParameter)44 Property (io.swagger.models.properties.Property)40 HashMap (java.util.HashMap)37 Parameter (io.swagger.models.parameters.Parameter)34 Model (io.swagger.models.Model)32 Map (java.util.Map)32 HttpMethod (io.swagger.models.HttpMethod)28 ArrayProperty (io.swagger.models.properties.ArrayProperty)28 ArrayList (java.util.ArrayList)28 Test (org.junit.Test)27 RefProperty (io.swagger.models.properties.RefProperty)24 ArrayModel (io.swagger.models.ArrayModel)22 ApiOperation (io.swagger.annotations.ApiOperation)21 StringProperty (io.swagger.models.properties.StringProperty)21 RefModel (io.swagger.models.RefModel)20