Search in sources :

Example 1 with Converter

use of org.apache.servicecomb.swagger.invocation.converter.Converter in project incubator-servicecomb-java-chassis by apache.

the class ArgumentsMapperFactory method collectSwaggerArgumentsMapper.

protected void collectSwaggerArgumentsMapper(ArgumentsMapperConfig config, List<ProviderParameter> providerNormalParams) {
    Method swaggerMethod = config.getSwaggerMethod();
    Type[] swaggerParams = swaggerMethod.getGenericParameterTypes();
    // 普通场景,要求除了provider上的context,其他参数必须按顺序一一对应,provider上的有效参数可以与契约不一致
    int minParamCount = Math.min(providerNormalParams.size(), swaggerParams.length);
    for (int swaggerIdx = 0; swaggerIdx < minParamCount; swaggerIdx++) {
        ProviderParameter providerParameter = providerNormalParams.get(swaggerIdx);
        Type swaggerParameter = swaggerParams[swaggerIdx];
        Converter converter = converterMgr.findConverter(type, providerParameter.getType(), swaggerParameter);
        ArgumentMapper mapper = createArgumentMapperWithConverter(swaggerIdx, providerParameter.getIndex(), converter);
        config.addArgumentMapper(mapper);
    }
}
Also used : InvocationType(org.apache.servicecomb.swagger.invocation.InvocationType) Type(java.lang.reflect.Type) Converter(org.apache.servicecomb.swagger.invocation.converter.Converter) Method(java.lang.reflect.Method)

Example 2 with Converter

use of org.apache.servicecomb.swagger.invocation.converter.Converter in project incubator-servicecomb-java-chassis by apache.

the class TestResponseMapperFactorys method createResponseMapper_default.

@Test
public void createResponseMapper_default() {
    ConsumerResponseMapper mapper = consumerResponseMapperFactorys.createResponseMapper(String.class, String.class);
    Assert.assertThat(mapper, Matchers.instanceOf(DefaultConsumerResponseMapper.class));
    Converter converter = Deencapsulation.getField(mapper, "converter");
    Assert.assertSame(ConverterSame.getInstance(), converter);
}
Also used : ConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper) DefaultConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.DefaultConsumerResponseMapper) Converter(org.apache.servicecomb.swagger.invocation.converter.Converter) DefaultConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.DefaultConsumerResponseMapper) Test(org.junit.Test)

Example 3 with Converter

use of org.apache.servicecomb.swagger.invocation.converter.Converter in project incubator-servicecomb-java-chassis by apache.

the class ArgumentsMapperFactory method collectWrapBodyMapper.

protected void collectWrapBodyMapper(ArgumentsMapperConfig config, List<ProviderParameter> providerNormalParams) {
    // 将provider的参数存入唯一swagger参数的field
    // 或是将唯一swagger参数的field存入provider参数
    Method swaggerMethod = config.getSwaggerMethod();
    Class<?> swaggerParam = swaggerMethod.getParameterTypes()[0];
    Field[] swaggerParamFields = swaggerParam.getFields();
    // 普通场景,要求除了provider上的context,其他参数必须按顺序一一对应,provider上的有效参数可以与契约不一致
    int minParamCount = Math.min(providerNormalParams.size(), swaggerParamFields.length);
    Map<Integer, FieldInfo> fieldMap = new HashMap<>();
    for (int swaggerIdx = 0; swaggerIdx < minParamCount; swaggerIdx++) {
        ProviderParameter providerParameter = providerNormalParams.get(swaggerIdx);
        Field swaggerField = swaggerParamFields[swaggerIdx];
        swaggerField.setAccessible(true);
        Converter converter = converterMgr.findConverter(type, providerParameter.getType(), swaggerField.getGenericType());
        FieldInfo info = new FieldInfo(swaggerField, converter);
        fieldMap.put(providerParameter.getIndex(), info);
    }
    ArgumentMapper bodyFieldArg = createBodyFieldArgMapper(config, fieldMap);
    config.addArgumentMapper(bodyFieldArg);
}
Also used : Field(java.lang.reflect.Field) HashMap(java.util.HashMap) Converter(org.apache.servicecomb.swagger.invocation.converter.Converter) Method(java.lang.reflect.Method)

Example 4 with Converter

use of org.apache.servicecomb.swagger.invocation.converter.Converter in project incubator-servicecomb-java-chassis by apache.

the class ArgumentsMapperFactory method isSwaggerWrapBody.

protected boolean isSwaggerWrapBody(ArgumentsMapperConfig config, List<ProviderParameter> providerNormalParams) {
    Method swaggerMethod = config.getSwaggerMethod();
    if (swaggerMethod.getParameterCount() != 1) {
        return false;
    }
    Type swaggerType = config.getSwaggerMethod().getGenericParameterTypes()[0];
    if (!swaggerType.getClass().equals(Class.class)) {
        return false;
    }
    Type firstProviderParam = providerNormalParams.get(0).getType();
    if (TypeUtils.isAssignable(firstProviderParam, swaggerType)) {
        return false;
    }
    // no public field, it's not rpc wrapper class
    if (((Class<?>) swaggerType).getFields().length == 0) {
        return false;
    }
    swaggerType = ((Class<?>) swaggerType).getFields()[0].getGenericType();
    Converter converter = converterMgr.findConverter(type, firstProviderParam, swaggerType);
    if (ConverterCommon.class.isInstance(converter)) {
        return false;
    }
    // 透明rpc的包装场景
    return true;
}
Also used : InvocationType(org.apache.servicecomb.swagger.invocation.InvocationType) Type(java.lang.reflect.Type) Converter(org.apache.servicecomb.swagger.invocation.converter.Converter) Method(java.lang.reflect.Method)

Aggregations

Converter (org.apache.servicecomb.swagger.invocation.converter.Converter)4 Method (java.lang.reflect.Method)3 Type (java.lang.reflect.Type)2 InvocationType (org.apache.servicecomb.swagger.invocation.InvocationType)2 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 ConsumerResponseMapper (org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper)1 DefaultConsumerResponseMapper (org.apache.servicecomb.swagger.invocation.response.consumer.DefaultConsumerResponseMapper)1 Test (org.junit.Test)1