You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2023/05/19 11:39:33 UTC

[servicecomb-java-chassis] branch master updated: [SCB-2794]process unknown parameters so that can compatible to old ve… (#3799)

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new a3c4fb8de [SCB-2794]process unknown parameters so that can compatible to old ve… (#3799)
a3c4fb8de is described below

commit a3c4fb8de0069d091153c732f61f732633aaef14
Author: liubao68 <bi...@qq.com>
AuthorDate: Fri May 19 19:39:27 2023 +0800

    [SCB-2794]process unknown parameters so that can compatible to old ve… (#3799)
---
 .../springmvc/client/ICompatible1xTestSchema.java  | 37 ++++++++++++
 .../client/TestCompatible1xTestSchema.java         | 69 ++++++++++++++++++++++
 .../springmvc/server/Compatible1xTestSchema.java   | 49 +++++++++++++++
 .../demo/springmvc/server/CompatibleQueryBean.java | 39 ++++++++++++
 .../springmvc/server/ProducerTestsAfterBootup.java |  4 +-
 .../arguments/AbstractArgumentsMapperCreator.java  | 13 ++--
 .../consumer/ConsumerArgumentsMapperCreator.java   | 11 ++--
 7 files changed, 212 insertions(+), 10 deletions(-)

diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/ICompatible1xTestSchema.java b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/ICompatible1xTestSchema.java
new file mode 100644
index 000000000..dc4cb5200
--- /dev/null
+++ b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/ICompatible1xTestSchema.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.demo.springmvc.client;
+
+import org.apache.servicecomb.swagger.invocation.context.InvocationContext;
+
+import io.swagger.annotations.ApiOperation;
+
+public interface ICompatible1xTestSchema {
+  String parameterName(int c, int d);
+
+  @ApiOperation(nickname = "parameterName", value = "parameterName")
+  String parameterNamePartMatchLeft(int a, int d);
+
+  @ApiOperation(nickname = "parameterName", value = "parameterName")
+  String parameterNamePartMatchRight(int c, int b);
+
+  String parameterName(InvocationContext context, int c, int d);
+
+  String parameterNameServerContext(int c, int d);
+
+  String beanParameter(String notName, int notAge);
+}
diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestCompatible1xTestSchema.java b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestCompatible1xTestSchema.java
new file mode 100644
index 000000000..a5f624388
--- /dev/null
+++ b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestCompatible1xTestSchema.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.demo.springmvc.client;
+
+import org.apache.servicecomb.demo.CategorizedTestCase;
+import org.apache.servicecomb.demo.TestMgr;
+import org.apache.servicecomb.provider.pojo.RpcReference;
+import org.apache.servicecomb.swagger.invocation.context.InvocationContext;
+import org.springframework.stereotype.Component;
+
+@Component
+public class TestCompatible1xTestSchema implements CategorizedTestCase {
+  @RpcReference(microserviceName = "springmvc", schemaId = "Compatible1xTestSchema")
+  private ICompatible1xTestSchema schema;
+
+  @Override
+  public void testAllTransport() throws Exception {
+    testParameterName();
+    testParameterNameClientContext();
+    testParameterNameServerContext();
+    testBeanParameter();
+    testParameterNamePartMatchLeft();
+    testParameterNamePartMatchRight();
+  }
+
+  private void testParameterNamePartMatchRight() {
+    String result = schema.parameterNamePartMatchRight(3, 4);
+    TestMgr.check("springmvcClient38", result);
+  }
+
+  private void testParameterNamePartMatchLeft() {
+    String result = schema.parameterNamePartMatchLeft(3, 4);
+    TestMgr.check("springmvcClient38", result);
+  }
+
+  private void testBeanParameter() {
+    String result = schema.beanParameter("name", 4);
+    TestMgr.check("name4", result);
+  }
+
+  private void testParameterNameServerContext() {
+    String result = schema.parameterNameServerContext(3, 4);
+    TestMgr.check("springmvcClient38", result);
+  }
+
+  private void testParameterNameClientContext() {
+    String result = schema.parameterName(new InvocationContext(), 3, 4);
+    TestMgr.check("springmvcClient38", result);
+  }
+
+  private void testParameterName() {
+    String result = schema.parameterName(3, 4);
+    TestMgr.check("springmvcClient38", result);
+  }
+}
diff --git a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/Compatible1xTestSchema.java b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/Compatible1xTestSchema.java
new file mode 100644
index 000000000..79d6f6b5d
--- /dev/null
+++ b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/Compatible1xTestSchema.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.demo.springmvc.server;
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.servicecomb.core.Const;
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.apache.servicecomb.swagger.invocation.context.ContextUtils;
+import org.apache.servicecomb.swagger.invocation.context.InvocationContext;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+@RestSchema(schemaId = "Compatible1xTestSchema")
+@RequestMapping(path = "/compatible", produces = MediaType.APPLICATION_JSON)
+public class Compatible1xTestSchema {
+  @GetMapping(path = "/parameterName")
+  public String parameterName(@RequestParam(name = "a", defaultValue = "10") int a,
+      @RequestParam(name = "b", defaultValue = "10") int b) {
+    return ContextUtils.getInvocationContext().getContext(Const.SRC_MICROSERVICE) + a + b * 2;
+  }
+
+  @GetMapping(path = "/parameterNameServerContext")
+  public String parameterNameServerContext(InvocationContext context,
+      @RequestParam(name = "a", defaultValue = "10") int a,
+      @RequestParam(name = "b", defaultValue = "10") int b) {
+    return context.getContext(Const.SRC_MICROSERVICE) + a + b * 2;
+  }
+
+  @GetMapping(path = "/beanParameter")
+  public String beanParameter(CompatibleQueryBean bean) {
+    return bean.getName() + bean.getAge();
+  }
+}
diff --git a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/CompatibleQueryBean.java b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/CompatibleQueryBean.java
new file mode 100644
index 000000000..651350372
--- /dev/null
+++ b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/CompatibleQueryBean.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.demo.springmvc.server;
+
+public class CompatibleQueryBean {
+  private String name;
+
+  private int age;
+
+  public String getName() {
+    return name;
+  }
+
+  public void setName(String name) {
+    this.name = name;
+  }
+
+  public int getAge() {
+    return age;
+  }
+
+  public void setAge(int age) {
+    this.age = age;
+  }
+}
diff --git a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerTestsAfterBootup.java b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerTestsAfterBootup.java
index ebbe1dd8c..e84383681 100644
--- a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerTestsAfterBootup.java
+++ b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerTestsAfterBootup.java
@@ -91,9 +91,9 @@ public class ProducerTestsAfterBootup implements BootListener {
 
   public void testRegisteredBasePath() {
     if (DynamicPropertyFactory.getInstance().getBooleanProperty("servicecomb.test.vert.transport", true).get()) {
-      TestMgr.check(21, RegistrationManager.INSTANCE.getMicroservice().getPaths().size());
-    } else {
       TestMgr.check(22, RegistrationManager.INSTANCE.getMicroservice().getPaths().size());
+    } else {
+      TestMgr.check(23, RegistrationManager.INSTANCE.getMicroservice().getPaths().size());
     }
   }
 
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/arguments/AbstractArgumentsMapperCreator.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/arguments/AbstractArgumentsMapperCreator.java
index 5291399ac..42e5e58db 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/arguments/AbstractArgumentsMapperCreator.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/arguments/AbstractArgumentsMapperCreator.java
@@ -89,6 +89,10 @@ import io.swagger.models.properties.Property;
  * </pre>
  */
 public abstract class AbstractArgumentsMapperCreator {
+  protected int notProcessedSwaggerParamIdx = 0;
+
+  protected boolean isSwaggerBodyField = false;
+
   protected SerializationConfig serializationConfig;
 
   // key is context class
@@ -157,18 +161,20 @@ public abstract class AbstractArgumentsMapperCreator {
     java.lang.reflect.Parameter[] providerParameters = providerMethod.getParameters();
     for (int providerParamIdx = 0; providerParamIdx < providerParameters.length; providerParamIdx++) {
       java.lang.reflect.Parameter providerParameter = providerParameters[providerParamIdx];
-      if (processContextParameter(providerParamIdx, providerParameter)) {
+      if (processContextParameter(providerParameter)) {
         continue;
       }
 
       String parameterName = collectParameterName(providerParameter);
       if (processKnownParameter(providerParamIdx, providerParameter, parameterName)) {
         processedSwaggerParamters.add(parameterName);
+        notProcessedSwaggerParamIdx++;
         continue;
       }
 
       if (processSwaggerBodyField(providerParamIdx, providerParameter, parameterName)) {
         processedSwaggerParamters.add(parameterName);
+        isSwaggerBodyField = true;
         continue;
       }
 
@@ -188,18 +194,17 @@ public abstract class AbstractArgumentsMapperCreator {
 
   /**
    *
-   * @param providerParamIdx
    * @param providerParameter processing provider parameter
    * @return true means processed
    */
-  protected boolean processContextParameter(int providerParamIdx, java.lang.reflect.Parameter providerParameter) {
+  protected boolean processContextParameter(java.lang.reflect.Parameter providerParameter) {
     ContextArgumentMapperFactory contextFactory = contextFactorys.get(providerParameter.getType());
     if (contextFactory == null) {
       return false;
     }
 
     mappers.add(contextFactory
-        .create(this.providerMethod.getParameters()[providerParamIdx].getName(), providerParameter.getName()));
+        .create(providerParameter.getName(), providerParameter.getName()));
     return true;
   }
 
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/arguments/consumer/ConsumerArgumentsMapperCreator.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/arguments/consumer/ConsumerArgumentsMapperCreator.java
index 128a4ae60..838a361e9 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/arguments/consumer/ConsumerArgumentsMapperCreator.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/arguments/consumer/ConsumerArgumentsMapperCreator.java
@@ -81,12 +81,15 @@ public class ConsumerArgumentsMapperCreator extends AbstractArgumentsMapperCreat
 
     // Make best guess, use the index of swagger to invoke server.
     // For compatible to old version behavior
-    if (providerParamIdx < swaggerParameters.size()) {
-      Parameter parameter = swaggerParameters.get(providerParamIdx);
+    if (!isSwaggerBodyField && notProcessedSwaggerParamIdx < swaggerParameters.size()) {
+      Parameter parameter = swaggerParameters.get(notProcessedSwaggerParamIdx);
       if (parameter != null) {
-        ArgumentMapper mapper = createKnownParameterMapper(providerParamIdx, providerParamIdx);
+        ArgumentMapper mapper = createKnownParameterMapper(providerParamIdx, notProcessedSwaggerParamIdx);
         mappers.add(mapper);
-        LOGGER.warn("new consumer invoke old version producer, parameter({}) is not exist in contract, method={}:{}.",
+        processedSwaggerParamters.add(parameterName);
+        notProcessedSwaggerParamIdx++;
+        LOGGER.warn("Old consumer invoke new version producer, parameter({}) is not exist in contract, method={}:{}."
+                + " Please change consumer parameter name to match swagger.",
             parameterName, providerMethod.getDeclaringClass().getName(), providerMethod.getName());
         return;
       }