Search in sources :

Example 1 with SwaggerOperations

use of org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations in project java-chassis by ServiceComb.

the class TestArrayType method test.

@Test
public void test() {
    SwaggerOperations swaggerOperations = SwaggerOperations.generate(ArrayType.class);
    SwaggerOperation swaggerOperation = swaggerOperations.findOperation("testBytes");
    BodyParameter bodyParameter = (BodyParameter) swaggerOperation.getOperation().getParameters().get(0);
    ModelImpl model = SwaggerUtils.getModelImpl(swaggerOperations.getSwagger(), bodyParameter);
    Assert.assertEquals(ModelImpl.OBJECT, model.getType());
    Assert.assertEquals(1, model.getProperties().size());
    ByteArrayProperty byteArrayProperty = (ByteArrayProperty) model.getProperties().get("value");
    Assert.assertEquals("string", byteArrayProperty.getType());
    Assert.assertEquals("byte", byteArrayProperty.getFormat());
}
Also used : ByteArrayProperty(io.swagger.models.properties.ByteArrayProperty) SwaggerOperations(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations) SwaggerOperation(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation) BodyParameter(io.swagger.models.parameters.BodyParameter) ModelImpl(io.swagger.models.ModelImpl) Test(org.junit.Test)

Example 2 with SwaggerOperations

use of org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations in project java-chassis by ServiceComb.

the class SwaggerEnvironment method createConsumer.

public SwaggerConsumer createConsumer(Class<?> consumerIntf, Swagger swagger) {
    Map<Class<?>, ContextArgumentMapperFactory> contextFactorys = SPIServiceUtils.getOrLoadSortedService(ConsumerContextArgumentMapperFactory.class).stream().collect(Collectors.toMap(ConsumerContextArgumentMapperFactory::getContextClass, Function.identity()));
    ResponseMapperFactorys<ConsumerResponseMapper> consumerResponseMapperFactorys = new ResponseMapperFactorys<>(ConsumerResponseMapperFactory.class);
    SwaggerOperations swaggerOperations = new SwaggerOperations(swagger);
    SwaggerConsumer consumer = new SwaggerConsumer();
    consumer.setConsumerIntf(consumerIntf);
    for (Method consumerMethod : MethodUtils.findSwaggerMethods(consumerIntf)) {
        String operationId = findOperationId(consumerMethod);
        SwaggerOperation swaggerOperation = swaggerOperations.findOperation(operationId);
        if (swaggerOperation == null) {
            // consumer method set is bigger than contract, it's invalid
            // but we need to support consumer upgrade before producer, so only log and ignore it.
            LOGGER.warn("consumer method {}:{} not exist in contract.", consumerIntf.getName(), consumerMethod.getName());
            continue;
        }
        ConsumerArgumentsMapperCreator creator = new ConsumerArgumentsMapperCreator(Json.mapper().getSerializationConfig(), contextFactorys, consumerIntf, consumerMethod, swaggerOperation);
        ArgumentsMapper argsMapper = creator.createArgumentsMapper();
        ConsumerResponseMapper responseMapper = consumerResponseMapperFactorys.createResponseMapper(consumerMethod.getGenericReturnType());
        SwaggerConsumerOperation op = new SwaggerConsumerOperation();
        op.setConsumerClass(consumerIntf);
        op.setConsumerMethod(consumerMethod);
        op.setSwaggerOperation(swaggerOperation);
        op.setArgumentsMapper(argsMapper);
        op.setResponseMapper(responseMapper);
        consumer.addOperation(op);
    }
    return consumer;
}
Also used : SwaggerOperations(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations) SwaggerOperation(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation) ResponseMapperFactorys(org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys) Method(java.lang.reflect.Method) ConsumerArgumentsMapperCreator(org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapperCreator) ConsumerContextArgumentMapperFactory(org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerContextArgumentMapperFactory) ProducerContextArgumentMapperFactory(org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerContextArgumentMapperFactory) ContextArgumentMapperFactory(org.apache.servicecomb.swagger.invocation.arguments.ContextArgumentMapperFactory) ConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper) ArgumentsMapper(org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper) ProducerArgumentsMapper(org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper)

Example 3 with SwaggerOperations

use of org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations in project java-chassis by ServiceComb.

the class SwaggerEnvironment method createProducer.

public SwaggerProducer createProducer(Object producerInstance, Class<?> schemaInterface, Swagger swagger) {
    Class<?> producerCls = targetSwaggerClass(producerInstance, schemaInterface);
    swagger = checkAndGenerateSwagger(producerCls, swagger);
    Map<Class<?>, ContextArgumentMapperFactory> contextFactories = SPIServiceUtils.getOrLoadSortedService(ProducerContextArgumentMapperFactory.class).stream().collect(Collectors.toMap(ProducerContextArgumentMapperFactory::getContextClass, Function.identity()));
    ResponseMapperFactorys<ProducerResponseMapper> producerResponseMapperFactorys = new ResponseMapperFactorys<>(ProducerResponseMapperFactory.class);
    SwaggerOperations swaggerOperations = new SwaggerOperations(swagger);
    Map<String, Method> visibleProducerMethods = MethodUtils.findSwaggerMethodsMapOfOperationId(producerCls);
    SwaggerProducer producer = new SwaggerProducer();
    producer.setSwagger(swagger);
    producer.setProducerCls(producerCls);
    producer.setProducerInstance(producerInstance);
    for (SwaggerOperation swaggerOperation : swaggerOperations.getOperations().values()) {
        String operationId = swaggerOperation.getOperationId();
        // producer参数不一定等于swagger参数
        Method producerMethod = visibleProducerMethods.getOrDefault(operationId, null);
        if (producerMethod == null) {
            // producer未实现契约,非法
            String msg = String.format("operationId %s not exist in producer %s.", operationId, producerInstance.getClass().getName());
            throw new IllegalStateException(msg);
        }
        ProducerArgumentsMapperCreator creator = new ProducerArgumentsMapperCreator(Json.mapper().getSerializationConfig(), contextFactories, producerCls, producerMethod, swaggerOperation);
        ProducerArgumentsMapper argsMapper = creator.createArgumentsMapper();
        ProducerResponseMapper responseMapper = producerResponseMapperFactorys.createResponseMapper(producerMethod.getGenericReturnType());
        SwaggerProducerOperation op = new SwaggerProducerOperation();
        op.setProducerClass(producerCls);
        op.setProducerInstance(producerInstance);
        op.setProducerMethod(producerMethod);
        op.setSwaggerOperation(swaggerOperation);
        op.setSwaggerParameterTypes(creator.getSwaggerParameterTypes());
        op.setArgumentsMapper(argsMapper);
        op.setResponseMapper(responseMapper);
        producer.addOperation(op);
    }
    return producer;
}
Also used : SwaggerOperations(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations) SwaggerOperation(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation) ResponseMapperFactorys(org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys) ProducerArgumentsMapper(org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper) Method(java.lang.reflect.Method) ProducerArgumentsMapperCreator(org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapperCreator) ProducerResponseMapper(org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper) ConsumerContextArgumentMapperFactory(org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerContextArgumentMapperFactory) ProducerContextArgumentMapperFactory(org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerContextArgumentMapperFactory) ContextArgumentMapperFactory(org.apache.servicecomb.swagger.invocation.arguments.ContextArgumentMapperFactory)

Example 4 with SwaggerOperations

use of org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations in project java-chassis by ServiceComb.

the class ApiProcessorTest method processOnNoTag.

@Test
public void processOnNoTag() {
    SwaggerOperations swaggerOperations = SwaggerOperations.generate(SwaggerTestTargetWithNoTag.class);
    SwaggerOperation swaggerOperation = swaggerOperations.findOperation("op");
    assertNull(swaggerOperation.getOperation().getTags());
    assertThat(swaggerOperation.getSwagger().getConsumes(), Matchers.contains(MediaType.APPLICATION_JSON));
    assertThat(swaggerOperation.getSwagger().getProduces(), Matchers.contains(MediaType.APPLICATION_JSON));
}
Also used : SwaggerOperations(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations) SwaggerOperation(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation) Test(org.junit.Test)

Example 5 with SwaggerOperations

use of org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations in project java-chassis by ServiceComb.

the class ApiProcessorTest method process.

@Test
public void process() {
    SwaggerOperations swaggerOperations = SwaggerOperations.generate(SwaggerTestTarget.class);
    SwaggerOperation swaggerOperation = swaggerOperations.findOperation("op");
    assertThat(swaggerOperation.getOperation().getTags(), contains("tag1", "tag2"));
    assertThat(swaggerOperation.getSwagger().getConsumes(), Matchers.contains(MediaType.APPLICATION_JSON));
    assertThat(swaggerOperation.getSwagger().getProduces(), Matchers.contains(MediaType.APPLICATION_JSON));
}
Also used : SwaggerOperations(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations) SwaggerOperation(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation) Test(org.junit.Test)

Aggregations

SwaggerOperation (org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation)5 SwaggerOperations (org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations)5 Test (org.junit.Test)3 Method (java.lang.reflect.Method)2 ContextArgumentMapperFactory (org.apache.servicecomb.swagger.invocation.arguments.ContextArgumentMapperFactory)2 ConsumerContextArgumentMapperFactory (org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerContextArgumentMapperFactory)2 ProducerArgumentsMapper (org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper)2 ProducerContextArgumentMapperFactory (org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerContextArgumentMapperFactory)2 ResponseMapperFactorys (org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys)2 ModelImpl (io.swagger.models.ModelImpl)1 BodyParameter (io.swagger.models.parameters.BodyParameter)1 ByteArrayProperty (io.swagger.models.properties.ByteArrayProperty)1 ArgumentsMapper (org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper)1 ConsumerArgumentsMapperCreator (org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapperCreator)1 ProducerArgumentsMapperCreator (org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapperCreator)1 ConsumerResponseMapper (org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper)1 ProducerResponseMapper (org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper)1