Search in sources :

Example 11 with SwaggerGenerator

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

the class ProducerSchemaFactory method createSchema.

protected SchemaMeta createSchema(ProducerSchemaContext context) {
    // 尝试从规划的目录加载契约
    Swagger swagger = loadSwagger(context);
    if (swagger == null) {
        Set<String> combinedNames = RegistryUtils.getServiceRegistry().getCombinedMicroserviceNames();
        for (String name : combinedNames) {
            swagger = loadSwagger(name, context.getSchemaId());
            if (swagger != null) {
                break;
            }
        }
    }
    // 根据class动态产生契约
    SwaggerGenerator generator = generateSwagger(context);
    if (swagger == null) {
        swagger = generator.getSwagger();
        String swaggerContent = getSwaggerContent(swagger);
        LOGGER.info("generate swagger for {}/{}/{}, swagger: {}", context.getMicroserviceMeta().getAppId(), context.getMicroserviceName(), context.getSchemaId(), swaggerContent);
    }
    // 注册契约
    return schemaLoader.registerSchema(context.getMicroserviceMeta(), context.getSchemaId(), swagger);
}
Also used : SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) Swagger(io.swagger.models.Swagger)

Example 12 with SwaggerGenerator

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

the class TestResponsesMeta method test.

@Test
public void test() {
    SwaggerGenerator generator = UnitTestSwaggerUtils.generateSwagger(ResponseMetaImpl.class);
    Swagger swagger = generator.getSwagger();
    Operation operation = swagger.getPath("/add").getPost();
    ResponsesMeta meta = new ResponsesMeta();
    meta.init(null, "gen", swagger, operation, int.class);
    ResponseMeta resp = meta.findResponseMeta(200);
    Assert.assertEquals(int.class, resp.getJavaType().getRawClass());
    resp = meta.findResponseMeta(201);
    Assert.assertEquals(int.class, resp.getJavaType().getRawClass());
    resp = meta.findResponseMeta(400);
    Assert.assertEquals(String.class, resp.getJavaType().getRawClass());
    resp = meta.findResponseMeta(401);
    Assert.assertEquals(Long.class, resp.getJavaType().getRawClass());
    Assert.assertEquals(Integer.class, resp.getHeaders().get("h1").getRawClass());
    resp = meta.findResponseMeta(500);
    Assert.assertEquals(CommonExceptionData.class, resp.getJavaType().getRawClass());
}
Also used : SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) Swagger(io.swagger.models.Swagger) Operation(io.swagger.models.Operation) Test(org.junit.Test)

Example 13 with SwaggerGenerator

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

the class SwaggerEnvironment method createProducer.

public SwaggerProducer createProducer(Object producerInstance) {
    Class<?> producerCls = BeanUtils.getImplClassFromBean(producerInstance);
    SwaggerGenerator producerGenerator = UnitTestSwaggerUtils.generateSwagger(producerCls);
    Swagger swagger = producerGenerator.getSwagger();
    return createProducer(producerInstance, swagger);
}
Also used : SwaggerGenerator(org.apache.servicecomb.swagger.generator.core.SwaggerGenerator) Swagger(io.swagger.models.Swagger)

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