You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2019/04/28 01:18:27 UTC

[GitHub] [servicecomb-java-chassis] liubao68 commented on a change in pull request #1141: [SCB-1117][WIP][WEAK] Weak swagger consumer arguments

liubao68 commented on a change in pull request #1141:  [SCB-1117][WIP][WEAK] Weak swagger consumer arguments
URL: https://github.com/apache/servicecomb-java-chassis/pull/1141#discussion_r279173424
 
 

 ##########
 File path: swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/SwaggerEnvironment.java
 ##########
 @@ -17,115 +17,78 @@
 package org.apache.servicecomb.swagger.engine;
 
 import java.lang.reflect.Method;
-import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
-
-import javax.inject.Inject;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.servicecomb.foundation.common.utils.BeanUtils;
-import org.apache.servicecomb.foundation.common.utils.ReflectUtils;
-import org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapperConfig;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation;
+import org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations;
+import org.apache.servicecomb.swagger.invocation.arguments.ContextArgumentMapperFactory;
 import org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapper;
-import org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapperFactory;
+import org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapperCreator;
+import org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerContextArgumentMapperFactory;
 import org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper;
 import org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapperFactory;
-import org.apache.servicecomb.swagger.invocation.converter.ConverterMgr;
 import org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys;
 import org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper;
 import org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapperFactory;
 import org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper;
 import org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapperFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
 
 import io.swagger.annotations.ApiOperation;
-import io.swagger.models.Operation;
+import io.swagger.models.Swagger;
+import io.swagger.util.Json;
 
-@Component
 public class SwaggerEnvironment {
   private static final Logger LOGGER = LoggerFactory.getLogger(SwaggerEnvironment.class);
 
-  /**
-   * default producerArgumentsFactory
-   */
-  @Inject
-  private ProducerArgumentsMapperFactory producerArgumentsFactory;
-
-  @Autowired
-  private List<ProducerArgumentsMapperFactory> producerArgumentsMapperFactoryList = new ArrayList<>(0);
-
   private ResponseMapperFactorys<ProducerResponseMapper> producerResponseMapperFactorys =
       new ResponseMapperFactorys<>(ProducerResponseMapperFactory.class);
 
-  @Inject
-  private ConsumerArgumentsMapperFactory consumerArgumentsFactory;
-
   private ResponseMapperFactorys<ConsumerResponseMapper> consumerResponseMapperFactorys =
       new ResponseMapperFactorys<>(ConsumerResponseMapperFactory.class);
 
-  @Inject
-  public void setConverterMgr(ConverterMgr converterMgr) {
-    consumerResponseMapperFactorys.setConverterMgr(converterMgr);
-    producerResponseMapperFactorys.setConverterMgr(converterMgr);
-  }
-
-  public ProducerArgumentsMapperFactory getProducerArgumentsFactory() {
-    return producerArgumentsFactory;
-  }
-
-  public void setProducerArgumentsFactory(ProducerArgumentsMapperFactory producerArgumentsFactory) {
-    this.producerArgumentsFactory = producerArgumentsFactory;
-  }
-
-  public ConsumerArgumentsMapperFactory getConsumerArgumentsFactory() {
-    return consumerArgumentsFactory;
-  }
-
-  public void setConsumerArgumentsFactory(ConsumerArgumentsMapperFactory consumerArgumentsFactory) {
-    this.consumerArgumentsFactory = consumerArgumentsFactory;
-  }
-
-  public SwaggerConsumer createConsumer(Class<?> consumerIntf) {
-    // consumer interface equals to contract interface
-    return createConsumer(consumerIntf, consumerIntf);
-  }
+  public SwaggerConsumer createConsumer(Class<?> consumerIntf, Swagger swagger) {
+    Map<Class<?>, ContextArgumentMapperFactory> contextFactorys = SPIServiceUtils
+        .getOrLoadSortedService(ConsumerContextArgumentMapperFactory.class)
+        .stream()
+        .collect(Collectors.toMap(ConsumerContextArgumentMapperFactory::getContextClass, Function.identity()));
+    SwaggerOperations swaggerOperations = new SwaggerOperations(swagger);
 
-  public SwaggerConsumer createConsumer(Class<?> consumerIntf, Class<?> swaggerIntf) {
     SwaggerConsumer consumer = new SwaggerConsumer();
     consumer.setConsumerIntf(consumerIntf);
-    consumer.setSwaggerIntf(swaggerIntf);
     for (Method consumerMethod : consumerIntf.getMethods()) {
-      String swaggerMethodName = findSwaggerMethodName(consumerMethod);
-      // consumer参数不一定等于swagger参数
-      Method swaggerMethod = ReflectUtils.findMethod(swaggerIntf, swaggerMethodName);
-      if (swaggerMethod == null) {
-        // consumer method set bigger than contract, it's invalid
+      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;
       }
 
-      ArgumentsMapperConfig config = new ArgumentsMapperConfig();
-      config.setSwaggerMethod(swaggerMethod);
-      config.setProviderMethod(consumerMethod);
-
-      ConsumerArgumentsMapper argsMapper =
-          consumerArgumentsFactory.createArgumentsMapper(config);
-      ConsumerResponseMapper responseMapper = consumerResponseMapperFactorys.createResponseMapper(
-          swaggerMethod.getGenericReturnType(),
-          consumerMethod.getGenericReturnType());
+      ConsumerArgumentsMapperCreator creator = new ConsumerArgumentsMapperCreator(
+          Json.mapper().getSerializationConfig(),
+          contextFactorys,
+          consumerMethod,
+          swaggerOperation);
+      ConsumerArgumentsMapper argsMapper = creator.createArgumentsMapper();
+      ConsumerResponseMapper responseMapper = null;
 
 Review comment:
   add a TODO?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services