Search in sources :

Example 16 with SwaggerOperation

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

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 17 with SwaggerOperation

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

the class TestApiResponse method checkSingle.

@Test
public void checkSingle() {
    SwaggerOperation swaggerOperation = swaggerOperations.findOperation("testSingle");
    Assert.assertEquals("/testSingle", swaggerOperation.getPath());
    Response response = swaggerOperation.getOperation().getResponses().get("200");
    Assert.assertEquals("integer", ((ModelImpl) response.getResponseSchema()).getType());
    Assert.assertEquals("int32", ((ModelImpl) response.getResponseSchema()).getFormat());
}
Also used : Response(io.swagger.models.Response) ApiResponse(io.swagger.annotations.ApiResponse) SwaggerOperation(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation) Test(org.junit.Test)

Example 18 with SwaggerOperation

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

the class TestApiResponse method checkApiResponseHeader.

@Test
public void checkApiResponseHeader() {
    SwaggerOperation swaggerOperation = swaggerOperations.findOperation("testApiResponseHeader");
    Assert.assertEquals("/testApiResponseHeader", swaggerOperation.getPath());
    Response response = swaggerOperation.getOperation().getResponses().get("200");
    Property property = response.getHeaders().get("k1");
    Assert.assertEquals("integer", property.getType());
    Assert.assertEquals("int32", property.getFormat());
    property = response.getHeaders().get("k2");
    Assert.assertEquals("string", property.getType());
    Assert.assertNull(property.getFormat());
}
Also used : Response(io.swagger.models.Response) ApiResponse(io.swagger.annotations.ApiResponse) SwaggerOperation(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation) Property(io.swagger.models.properties.Property) Test(org.junit.Test)

Example 19 with SwaggerOperation

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

the class TestApiResponse method checkResponseHeader.

@Test
public void checkResponseHeader() {
    SwaggerOperation swaggerOperation = swaggerOperations.findOperation("testResponseHeader");
    Assert.assertEquals("/testResponseHeader", swaggerOperation.getPath());
    Response response = swaggerOperation.getOperation().getResponses().get("200");
    Property property = response.getHeaders().get("k1");
    Assert.assertEquals("integer", property.getType());
    Assert.assertEquals("int32", property.getFormat());
}
Also used : Response(io.swagger.models.Response) ApiResponse(io.swagger.annotations.ApiResponse) SwaggerOperation(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation) Property(io.swagger.models.properties.Property) Test(org.junit.Test)

Example 20 with SwaggerOperation

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

the class TestApiResponse method checkResponseDesc.

@Test
public void checkResponseDesc() {
    SwaggerOperation swaggerOperation = swaggerOperations.findOperation("testMulti");
    Assert.assertEquals("/testMulti", swaggerOperation.getPath());
    Response response1 = swaggerOperation.getOperation().getResponses().get("200");
    Response response2 = swaggerOperation.getOperation().getResponses().get("301");
    Assert.assertEquals("msg1", response1.getDescription());
    Assert.assertEquals("msg2", response2.getDescription());
}
Also used : Response(io.swagger.models.Response) ApiResponse(io.swagger.annotations.ApiResponse) SwaggerOperation(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation) Test(org.junit.Test)

Aggregations

SwaggerOperation (org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation)36 Test (org.junit.Test)32 ApiResponse (io.swagger.annotations.ApiResponse)14 Response (io.swagger.models.Response)14 SwaggerOperations (org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations)10 Property (io.swagger.models.properties.Property)6 Method (java.lang.reflect.Method)4 ContextArgumentMapperFactory (org.apache.servicecomb.swagger.invocation.arguments.ContextArgumentMapperFactory)4 ConsumerContextArgumentMapperFactory (org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerContextArgumentMapperFactory)4 ProducerArgumentsMapper (org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper)4 ProducerContextArgumentMapperFactory (org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerContextArgumentMapperFactory)4 ResponseMapperFactorys (org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys)4 ModelImpl (io.swagger.models.ModelImpl)2 BodyParameter (io.swagger.models.parameters.BodyParameter)2 ByteArrayProperty (io.swagger.models.properties.ByteArrayProperty)2 ArgumentsMapper (org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper)2 ConsumerArgumentsMapperCreator (org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapperCreator)2 ProducerArgumentsMapperCreator (org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapperCreator)2 ConsumerResponseMapper (org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper)2 ProducerResponseMapper (org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper)2