Search in sources :

Example 1 with SwaggerGenerator

use of io.servicecomb.swagger.generator.core.SwaggerGenerator in project java-chassis by ServiceComb.

the class ProducerSchemaFactory method generateSwagger.

@Override
protected SwaggerGenerator generateSwagger(ProducerSchemaContext context) {
    SwaggerGenerator generator = super.generateSwagger(context);
    context.setGenerator(generator);
    return generator;
}
Also used : SwaggerGenerator(io.servicecomb.swagger.generator.core.SwaggerGenerator)

Example 2 with SwaggerGenerator

use of io.servicecomb.swagger.generator.core.SwaggerGenerator in project java-chassis by ServiceComb.

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(classConfig);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) SwaggerGenerator(io.servicecomb.swagger.generator.core.SwaggerGenerator) ClassConfig(io.servicecomb.common.javassist.ClassConfig) Method(java.lang.reflect.Method) BodyParameter(io.swagger.models.parameters.BodyParameter)

Example 3 with SwaggerGenerator

use of io.servicecomb.swagger.generator.core.SwaggerGenerator in project java-chassis by ServiceComb.

the class TestRestEngineSchemaListener method test.

@Test
public void test() {
    BeanUtils.setContext(Mockito.mock(ApplicationContext.class));
    MicroserviceMeta mm = new MicroserviceMeta("app:ms");
    List<SchemaMeta> smList = new ArrayList<>();
    SwaggerGenerator generator = new SwaggerGenerator(context, Impl.class);
    Swagger swagger = generator.generate();
    SchemaMeta sm1 = new SchemaMeta(swagger, mm, "sid1");
    smList.add(sm1);
    RestEngineSchemaListener listener = new RestEngineSchemaListener();
    SchemaMeta[] smArr = smList.toArray(new SchemaMeta[smList.size()]);
    listener.onSchemaLoaded(smArr);
    // 重复调用,不应该出异常
    listener.onSchemaLoaded(smArr);
    ServicePathManager spm = ServicePathManager.getServicePathManager(mm);
    Assert.assertEquals(mm, spm.getMicroserviceMeta());
    Assert.assertNotNull(spm.getStaticPathOperationMap().get("Impl/add/"));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) SwaggerGenerator(io.servicecomb.swagger.generator.core.SwaggerGenerator) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) Swagger(io.swagger.models.Swagger) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) ArrayList(java.util.ArrayList) ServicePathManager(io.servicecomb.common.rest.locator.ServicePathManager) Test(org.junit.Test)

Example 4 with SwaggerGenerator

use of io.servicecomb.swagger.generator.core.SwaggerGenerator in project java-chassis by ServiceComb.

the class AbstractSchemaFactory method generateSwagger.

protected SwaggerGenerator generateSwagger(CONTEXT context) {
    SwaggerGeneratorContext generatorContext = compositeSwaggerGeneratorContext.selectContext(context.getProviderClass());
    SwaggerGenerator generator = new SwaggerGenerator(generatorContext, context.getProviderClass());
    generator.setPackageName(SchemaUtils.generatePackageName(context.getMicroserviceMeta(), context.getSchemaId()));
    generator.generate();
    // 确保接口是存在的
    ClassUtils.getOrCreateInterface(generator);
    return generator;
}
Also used : SwaggerGenerator(io.servicecomb.swagger.generator.core.SwaggerGenerator) CompositeSwaggerGeneratorContext(io.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext) SwaggerGeneratorContext(io.servicecomb.swagger.generator.core.SwaggerGeneratorContext)

Example 5 with SwaggerGenerator

use of io.servicecomb.swagger.generator.core.SwaggerGenerator in project java-chassis by ServiceComb.

the class ConsumerSchemaFactory method mapDiffIntfParameters.

protected void mapDiffIntfParameters(ConsumerSchemaContext context) {
    // 建立契约与consumer之间的参数映射关系
    Class<?> swaggerIntf = ClassUtils.getJavaInterface(context.getSchemaMeta().getSwagger());
    SwaggerGenerator generator = generateSwagger(context);
    for (OperationMeta operationMeta : context.getSchemaMeta().getOperations()) {
        OperationGenerator operationGenerator = generator.getOperationGeneratorMap().get(operationMeta.getOperationId());
        // swagger集合可能大于consumer集合
        if (operationGenerator == null) {
            continue;
        }
        Method swaggerMethod = ReflectUtils.findMethod(swaggerIntf, operationMeta.getOperationId());
        List<Parameter> swaggerParameters = operationMeta.getSwaggerOperation().getParameters();
        Method consumerMethod = operationGenerator.getProviderMethod();
        List<Parameter> consumerParameters = operationGenerator.getProviderParameters();
        ConsumerArgumentsMapper argsMapper = consumerArgsMapperFactory.createArgumentsMapper(context.getSchemaMeta().getSwagger(), swaggerMethod, swaggerParameters, consumerMethod, consumerParameters);
        createOperation(context, operationMeta, argsMapper);
    }
}
Also used : ConsumerArgumentsMapper(io.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapper) SwaggerGenerator(io.servicecomb.swagger.generator.core.SwaggerGenerator) Parameter(io.swagger.models.parameters.Parameter) ConsumerOperationMeta(io.servicecomb.core.provider.consumer.ConsumerOperationMeta) OperationMeta(io.servicecomb.core.definition.OperationMeta) Method(java.lang.reflect.Method) OperationGenerator(io.servicecomb.swagger.generator.core.OperationGenerator)

Aggregations

SwaggerGenerator (io.servicecomb.swagger.generator.core.SwaggerGenerator)8 Swagger (io.swagger.models.Swagger)3 CompositeSwaggerGeneratorContext (io.servicecomb.swagger.generator.core.CompositeSwaggerGeneratorContext)2 SwaggerGeneratorContext (io.servicecomb.swagger.generator.core.SwaggerGeneratorContext)2 Method (java.lang.reflect.Method)2 Test (org.junit.Test)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 ClassConfig (io.servicecomb.common.javassist.ClassConfig)1 ServicePathManager (io.servicecomb.common.rest.locator.ServicePathManager)1 MicroserviceMeta (io.servicecomb.core.definition.MicroserviceMeta)1 OperationMeta (io.servicecomb.core.definition.OperationMeta)1 SchemaMeta (io.servicecomb.core.definition.SchemaMeta)1 ConsumerOperationMeta (io.servicecomb.core.provider.consumer.ConsumerOperationMeta)1 OperationGenerator (io.servicecomb.swagger.generator.core.OperationGenerator)1 ConsumerArgumentsMapper (io.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapper)1 Operation (io.swagger.models.Operation)1 BodyParameter (io.swagger.models.parameters.BodyParameter)1 Parameter (io.swagger.models.parameters.Parameter)1 ArrayList (java.util.ArrayList)1 ApplicationContext (org.springframework.context.ApplicationContext)1