Search in sources :

Example 26 with SwaggerOperation

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

the class TestApiResponse method checkMulti.

@Test
public void checkMulti() {
    SwaggerOperation swaggerOperation = swaggerOperations.findOperation("testMulti");
    Assert.assertEquals("/testMulti", swaggerOperation.getPath());
    Response response = swaggerOperation.getOperation().getResponses().get("200");
    Assert.assertEquals("integer", ((ModelImpl) response.getResponseSchema()).getType());
    Assert.assertEquals("int32", ((ModelImpl) response.getResponseSchema()).getFormat());
    response = swaggerOperation.getOperation().getResponses().get("301");
    Assert.assertEquals("string", ((ModelImpl) response.getResponseSchema()).getType());
    Assert.assertNull(((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 27 with SwaggerOperation

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

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

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

the class TestOperationGenerator method responseThenApiOperation.

@Test
public void responseThenApiOperation() {
    SwaggerOperation swaggerOperation = swaggerOperations.findOperation("responseThenApiOperation");
    List<String> tags = swaggerOperation.getOperation().getTags();
    assertThat(tags, contains("tag1", "tag2"));
    Response response = swaggerOperation.getOperation().getResponses().get("200");
    assertEquals("200 is ok............", response.getDescription());
    Assert.assertNotNull(response.getHeaders().get("x-user-domain"));
    Assert.assertNull(response.getHeaders().get("x-user-name"));
    Assert.assertNotNull(swaggerOperation.getOperation().getVendorExtensions().get("x-class-name"));
}
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 29 with SwaggerOperation

use of org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation 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 30 with SwaggerOperation

use of org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation 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)

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