Search in sources :

Example 1 with SwaggerGenerator

use of org.apache.servicecomb.swagger.generator.core.SwaggerGenerator in project incubator-servicecomb-java-chassis by apache.

the class UnitTestSwaggerUtils method generateSwagger.

public static SwaggerGenerator generateSwagger(Class<?> cls) {
    SwaggerGeneratorContext context = compositeContext.selectContext(cls);
    SwaggerGenerator generator = new SwaggerGenerator(context, cls);
    generator.generate();
    return generator;
}
Also used : SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) SwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.core.SwaggerGeneratorContext) CompositeSwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext)

Example 2 with SwaggerGenerator

use of org.apache.servicecomb.swagger.generator.core.SwaggerGenerator in project incubator-servicecomb-java-chassis by apache.

the class ClassUtils method getOrCreateBodyClass.

// 将一系列body parameter包装成一个class
public static Class<?> getOrCreateBodyClass(OperationGenerator operationGenerator, List<BodyParameter> bodyParameters) {
    SwaggerGenerator swaggerGenerator = operationGenerator.getSwaggerGenerator();
    Method method = operationGenerator.getProviderMethod();
    String clsName = swaggerGenerator.ensureGetPackageName() + "." + method.getName() + "Body";
    Class<?> cls = getClassByName(swaggerGenerator.getClassLoader(), clsName);
    if (cls != null) {
        return cls;
    }
    ClassConfig classConfig = new ClassConfig();
    classConfig.setClassName(clsName);
    // 2.预备body与明确body混合
    for (BodyParameter bp : bodyParameters) {
        JavaType javaType = ConverterMgr.findJavaType(swaggerGenerator.getClassLoader(), swaggerGenerator.ensureGetPackageName(), swaggerGenerator.getSwagger(), bp);
        classConfig.addField(bp.getName(), javaType);
    }
    return JavassistUtils.createClass(swaggerGenerator.getClassLoader(), classConfig);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) ClassConfig(org.apache.servicecomb.common.javassist.ClassConfig) Method(java.lang.reflect.Method) BodyParameter(io.swagger.models.parameters.BodyParameter)

Example 3 with SwaggerGenerator

use of org.apache.servicecomb.swagger.generator.core.SwaggerGenerator in project incubator-servicecomb-java-chassis by apache.

the class ApiOperationProcessorTest method testConvertTags.

@Test
public void testConvertTags() throws NoSuchMethodException {
    ApiOperationProcessor apiOperationProcessor = new ApiOperationProcessor();
    Method function = TestClass.class.getMethod("function");
    SwaggerGenerator swaggerGenerator = new SwaggerGenerator(new PojoSwaggerGeneratorContext(), TestClass.class);
    OperationGenerator operationGenerator = new OperationGenerator(swaggerGenerator, function);
    apiOperationProcessor.process(function.getAnnotation(ApiOperation.class), operationGenerator);
    assertThat(operationGenerator.getOperation().getTags(), containsInAnyOrder("tag1", "tag2"));
}
Also used : SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) ApiOperation(io.swagger.annotations.ApiOperation) Method(java.lang.reflect.Method) PojoSwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.pojo.PojoSwaggerGeneratorContext) OperationGenerator(org.apache.servicecomb.swagger.generator.core.OperationGenerator) Test(org.junit.Test)

Example 4 with SwaggerGenerator

use of org.apache.servicecomb.swagger.generator.core.SwaggerGenerator in project incubator-servicecomb-java-chassis by apache.

the class ApiOperationProcessorTest method testConvertTagsOnMethodWithNoTag.

@Test
public void testConvertTagsOnMethodWithNoTag() throws NoSuchMethodException {
    ApiOperationProcessor apiOperationProcessor = new ApiOperationProcessor();
    Method function = TestClass.class.getMethod("functionWithNoTag");
    SwaggerGenerator swaggerGenerator = new SwaggerGenerator(new PojoSwaggerGeneratorContext(), TestClass.class);
    OperationGenerator operationGenerator = new OperationGenerator(swaggerGenerator, function);
    apiOperationProcessor.process(function.getAnnotation(ApiOperation.class), operationGenerator);
    List<String> tagList = operationGenerator.getOperation().getTags();
    assertNull(tagList);
}
Also used : SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) ApiOperation(io.swagger.annotations.ApiOperation) Method(java.lang.reflect.Method) PojoSwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.pojo.PojoSwaggerGeneratorContext) OperationGenerator(org.apache.servicecomb.swagger.generator.core.OperationGenerator) Test(org.junit.Test)

Example 5 with SwaggerGenerator

use of org.apache.servicecomb.swagger.generator.core.SwaggerGenerator in project incubator-servicecomb-java-chassis by apache.

the class ApiProcessorTest method process.

@Test
public void process() {
    SwaggerGenerator swaggerGenerator = new SwaggerGenerator(Mockito.mock(SwaggerGeneratorContext.class), null);
    apiProcessor.process(SwaggerTestTarget.class.getAnnotation(Api.class), swaggerGenerator);
    assertThat(swaggerGenerator.getDefaultTags(), contains("tag1", "tag2"));
}
Also used : SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) SwaggerGeneratorContext(org.apache.servicecomb.swagger.generator.core.SwaggerGeneratorContext) Api(io.swagger.annotations.Api) Test(org.junit.Test)

Aggregations

SwaggerGenerator (org.apache.servicecomb.swagger.generator.core.SwaggerGenerator)13 Test (org.junit.Test)7 Swagger (io.swagger.models.Swagger)5 SwaggerGeneratorContext (org.apache.servicecomb.swagger.generator.core.SwaggerGeneratorContext)5 Method (java.lang.reflect.Method)3 Api (io.swagger.annotations.Api)2 ApiOperation (io.swagger.annotations.ApiOperation)2 CompositeSwaggerGeneratorContext (org.apache.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext)2 OperationGenerator (org.apache.servicecomb.swagger.generator.core.OperationGenerator)2 PojoSwaggerGeneratorContext (org.apache.servicecomb.swagger.generator.pojo.PojoSwaggerGeneratorContext)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 SwaggerDefinition (io.swagger.annotations.SwaggerDefinition)1 Operation (io.swagger.models.Operation)1 BodyParameter (io.swagger.models.parameters.BodyParameter)1 ArrayList (java.util.ArrayList)1 ClassConfig (org.apache.servicecomb.common.javassist.ClassConfig)1 ServicePathManager (org.apache.servicecomb.common.rest.locator.ServicePathManager)1 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)1 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)1 Microservice (org.apache.servicecomb.serviceregistry.api.registry.Microservice)1