Search in sources :

Example 36 with ArgumentsMapper

use of org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper in project java-chassis by ServiceComb.

the class TestSpringmvcV2V2 method addBody_addWrapper.

@Test
public void addBody_addWrapper() {
    SwaggerEnvironment environment = new SwaggerEnvironment();
    Swagger swagger = SwaggerGenerator.generate(SpringmvcAddWrapperV2.class);
    SwaggerConsumer swaggerConsumer = environment.createConsumer(ConsumerAddBodyV2.class, swagger);
    ArgumentsMapper mapper = swaggerConsumer.findOperation("add").getArgumentsMapper();
    Map<String, Object> arguments = new HashMap<>();
    arguments.put("addBody", new AddWrapperV2(1, 2, 3));
    SwaggerInvocation invocation = new SwaggerInvocation();
    Map<String, Object> result = mapper.invocationArgumentToSwaggerArguments(invocation, arguments);
    Assert.assertEquals(3, result.size());
    Assert.assertEquals(1, (int) result.get("x"));
    Assert.assertEquals(2, (int) result.get("y"));
    Assert.assertEquals(3, (int) result.get("x-z"));
}
Also used : SwaggerInvocation(org.apache.servicecomb.swagger.invocation.SwaggerInvocation) SpringmvcAddWrapperV2(org.apache.servicecomb.swagger.invocation.schemas.SpringmvcAddWrapperV2) AddWrapperV2(org.apache.servicecomb.swagger.invocation.schemas.models.AddWrapperV2) SwaggerConsumer(org.apache.servicecomb.swagger.engine.SwaggerConsumer) HashMap(java.util.HashMap) Swagger(io.swagger.models.Swagger) SwaggerEnvironment(org.apache.servicecomb.swagger.engine.SwaggerEnvironment) ArgumentsMapper(org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper) Test(org.junit.Test)

Example 37 with ArgumentsMapper

use of org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper 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 38 with ArgumentsMapper

use of org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper in project java-chassis by ServiceComb.

the class TestJaxrsV1V1 method should_mapper_consumer_body_to_swagger_body.

@Test
public void should_mapper_consumer_body_to_swagger_body() {
    SwaggerEnvironment environment = new SwaggerEnvironment();
    Swagger swagger = SwaggerGenerator.generate(JaxrsAddBodyV1.class);
    SwaggerConsumer swaggerConsumer = environment.createConsumer(ConsumerAddBodyV1.class, swagger);
    ArgumentsMapper mapper = swaggerConsumer.findOperation("add").getArgumentsMapper();
    Map<String, Object> arguments = new HashMap<>();
    arguments.put("addBody", new AddWrapperV1(1, 2));
    SwaggerInvocation invocation = new SwaggerInvocation();
    Map<String, Object> result = mapper.invocationArgumentToSwaggerArguments(invocation, arguments);
    Assert.assertSame(result.get("addBody"), arguments.get("addBody"));
}
Also used : AddWrapperV1(org.apache.servicecomb.swagger.invocation.schemas.models.AddWrapperV1) SwaggerInvocation(org.apache.servicecomb.swagger.invocation.SwaggerInvocation) SwaggerConsumer(org.apache.servicecomb.swagger.engine.SwaggerConsumer) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Swagger(io.swagger.models.Swagger) SwaggerEnvironment(org.apache.servicecomb.swagger.engine.SwaggerEnvironment) ArgumentsMapper(org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper) Test(org.junit.Test)

Example 39 with ArgumentsMapper

use of org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper in project java-chassis by ServiceComb.

the class TestJaxrsV1V1 method should_mapper_consumer_multi_args_to_swagger_multi_args_with_diff_order.

@Test
public void should_mapper_consumer_multi_args_to_swagger_multi_args_with_diff_order() {
    SwaggerEnvironment environment = new SwaggerEnvironment();
    Swagger swagger = SwaggerGenerator.generate(JaxrsAddV1.class);
    SwaggerConsumer swaggerConsumer = environment.createConsumer(ConsumerAddV1_diff_order.class, swagger);
    ArgumentsMapper mapper = swaggerConsumer.findOperation("add").getArgumentsMapper();
    Map<String, Object> arguments = new HashMap<>();
    arguments.put("x", 1);
    arguments.put("y", 2);
    SwaggerInvocation invocation = new SwaggerInvocation();
    Map<String, Object> result = mapper.invocationArgumentToSwaggerArguments(invocation, arguments);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(1, (int) result.get("x"));
    Assert.assertEquals(2, (int) result.get("y"));
}
Also used : SwaggerInvocation(org.apache.servicecomb.swagger.invocation.SwaggerInvocation) SwaggerConsumer(org.apache.servicecomb.swagger.engine.SwaggerConsumer) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Swagger(io.swagger.models.Swagger) SwaggerEnvironment(org.apache.servicecomb.swagger.engine.SwaggerEnvironment) ArgumentsMapper(org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper) Test(org.junit.Test)

Example 40 with ArgumentsMapper

use of org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper in project java-chassis by ServiceComb.

the class TestJaxrsV1V1 method should_mapper_consumer_wrapped_body_to_swagger_multi_args_gen_by_BeanParam.

@Test
public void should_mapper_consumer_wrapped_body_to_swagger_multi_args_gen_by_BeanParam() {
    SwaggerEnvironment environment = new SwaggerEnvironment();
    Swagger swagger = SwaggerGenerator.generate(JaxrsAddBeanParamV1.class);
    SwaggerConsumer swaggerConsumer = environment.createConsumer(ConsumerAddBodyV1.class, swagger);
    ArgumentsMapper mapper = swaggerConsumer.findOperation("add").getArgumentsMapper();
    Map<String, Object> arguments = new HashMap<>();
    arguments.put("addBody", new AddWrapperV1(1, 2));
    SwaggerInvocation invocation = new SwaggerInvocation();
    Map<String, Object> result = mapper.invocationArgumentToSwaggerArguments(invocation, arguments);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(1, (int) result.get("x"));
    Assert.assertEquals(2, (int) result.get("y"));
}
Also used : AddWrapperV1(org.apache.servicecomb.swagger.invocation.schemas.models.AddWrapperV1) SwaggerInvocation(org.apache.servicecomb.swagger.invocation.SwaggerInvocation) SwaggerConsumer(org.apache.servicecomb.swagger.engine.SwaggerConsumer) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Swagger(io.swagger.models.Swagger) SwaggerEnvironment(org.apache.servicecomb.swagger.engine.SwaggerEnvironment) ArgumentsMapper(org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper) Test(org.junit.Test)

Aggregations

ArgumentsMapper (org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper)70 Swagger (io.swagger.models.Swagger)69 HashMap (java.util.HashMap)69 SwaggerConsumer (org.apache.servicecomb.swagger.engine.SwaggerConsumer)69 SwaggerEnvironment (org.apache.servicecomb.swagger.engine.SwaggerEnvironment)69 SwaggerInvocation (org.apache.servicecomb.swagger.invocation.SwaggerInvocation)69 Test (org.junit.Test)68 LinkedHashMap (java.util.LinkedHashMap)27 AddWrapperV1 (org.apache.servicecomb.swagger.invocation.schemas.models.AddWrapperV1)16 AddWrapperV2 (org.apache.servicecomb.swagger.invocation.schemas.models.AddWrapperV2)16 SpringmvcAddWrapperV1 (org.apache.servicecomb.swagger.invocation.schemas.SpringmvcAddWrapperV1)3 SpringmvcAddWrapperV2 (org.apache.servicecomb.swagger.invocation.schemas.SpringmvcAddWrapperV2)3 Method (java.lang.reflect.Method)1 User (org.apache.servicecomb.foundation.test.scaffolding.model.User)1 SwaggerOperation (org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation)1 SwaggerOperations (org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations)1 ContextArgumentMapperFactory (org.apache.servicecomb.swagger.invocation.arguments.ContextArgumentMapperFactory)1 ConsumerArgumentsMapperCreator (org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapperCreator)1 ConsumerContextArgumentMapperFactory (org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerContextArgumentMapperFactory)1 ProducerArgumentsMapper (org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper)1