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;
}
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);
}
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/"));
}
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;
}
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);
}
}
Aggregations