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 2019/07/22 03:14:17 UTC

[servicecomb-java-chassis] branch master updated: [SCB-1291]supportApiOperationMethodOverride

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 6509919  [SCB-1291]supportApiOperationMethodOverride
6509919 is described below

commit 6509919726662f2c8aee00d8faff114b8850de7e
Author: wuliuqi <24...@qq.com>
AuthorDate: Fri Jul 12 20:20:31 2019 +0800

    [SCB-1291]supportApiOperationMethodOverride
---
 .../org/apache/servicecomb/it/ConsumerMain.java    |   2 +
 .../it/testcase/TestApiOperationOverride.java      | 112 +++++++++++++++++++++
 .../it/schema/ApiOperationJaxrsSchema.java         |  47 +++++++++
 .../it/schema/ApiOperationSpringmvcSchema.java     |  40 ++++++++
 .../it/schema/ApiOpertionPojoSchema.java           |  35 +++++++
 .../apache/servicecomb/provider/pojo/Invoker.java  |  14 ++-
 .../swagger/engine/SwaggerEnvironment.java         |   2 +-
 .../engine/unittest/LocalProducerInvoker.java      |  14 ++-
 8 files changed, 263 insertions(+), 3 deletions(-)

diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
index afcbd9f..0fb528d 100644
--- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/ConsumerMain.java
@@ -24,6 +24,7 @@ import org.apache.servicecomb.it.junit.ITJUnitUtils;
 import org.apache.servicecomb.it.schema.TestApiOperation;
 import org.apache.servicecomb.it.testcase.TestAcceptType;
 import org.apache.servicecomb.it.testcase.TestAnnotatedAttribute;
+import org.apache.servicecomb.it.testcase.TestApiOperationOverride;
 import org.apache.servicecomb.it.testcase.TestApiParam;
 import org.apache.servicecomb.it.testcase.TestAsyncInvoke;
 import org.apache.servicecomb.it.testcase.TestChangeTransport;
@@ -131,6 +132,7 @@ public class ConsumerMain {
     ITJUnitUtils.runWithHighwayAndRest(TestAsyncInvoke.class);
 
     ITJUnitUtils.runWithHighwayAndRest(TestOptional.class);
+    ITJUnitUtils.runWithHighwayAndRest(TestApiOperationOverride.class);
   }
 
   interface ITTask {
diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestApiOperationOverride.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestApiOperationOverride.java
new file mode 100644
index 0000000..8fcf1bc
--- /dev/null
+++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/TestApiOperationOverride.java
@@ -0,0 +1,112 @@
+/*
+ * 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.it.testcase;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+
+import org.apache.servicecomb.it.Consumers;
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.swagger.annotations.ApiOperation;
+
+public class TestApiOperationOverride {
+  interface OptionalIntf {
+
+    @ApiOperation(value = "", nickname = "sayHi")
+    String sayHello();
+
+    @ApiOperation(value = "", nickname = "sayHello")
+    String sayHello(String name);
+
+    @ApiOperation(value = "", nickname = "sayHello")
+    CompletableFuture<String> sayHelloAsync(String name);
+
+    @ApiOperation(value = "", nickname = "sayHi")
+    CompletableFuture<String> sayHelloAsync();
+  }
+
+  private static Consumers<OptionalIntf> consumersPojo = new Consumers<>("apiOpertionPojoSchemaTest",
+      OptionalIntf.class);
+
+  private static Consumers<OptionalIntf> consumersJaxrs = new Consumers<>("apiOperationJaxrsSchemaTest",
+      OptionalIntf.class);
+
+  private static Consumers<OptionalIntf> consumersSpringmvc = new Consumers<>("apiOperationSpringmvcSchemaTest",
+      OptionalIntf.class);
+
+  @Test
+  public void consumersPojo_A_intf() {
+    Assert.assertEquals("ApiOpertionPojoSchema#sayHello", consumersPojo.getIntf().sayHello());
+  }
+
+  @Test
+  public void consumersPojo_B_intf() {
+    Assert.assertEquals("value", consumersPojo.getIntf().sayHello("value"));
+  }
+
+  @Test
+  public void consumersPojo_C_intf() throws ExecutionException, InterruptedException {
+    Assert.assertEquals("value", consumersPojo.getIntf().sayHelloAsync("value").get());
+  }
+
+  @Test
+  public void consumersPojo_D_intf() throws ExecutionException, InterruptedException {
+    Assert.assertEquals("ApiOpertionPojoSchema#sayHello", consumersPojo.getIntf().sayHelloAsync().get());
+  }
+
+  @Test
+  public void consumersJaxrs_A_intf() {
+    Assert.assertEquals("ApiOperationJaxrsSchema#sayHello", consumersJaxrs.getIntf().sayHello());
+  }
+
+  @Test
+  public void consumersJaxrs_B_intf() {
+    Assert.assertEquals("value", consumersJaxrs.getIntf().sayHello("value"));
+  }
+
+  @Test
+  public void consumersJaxrs_C_intf() throws ExecutionException, InterruptedException {
+    Assert.assertEquals("value", consumersJaxrs.getIntf().sayHelloAsync("value").get());
+  }
+
+  @Test
+  public void consumersJaxrs_D_intf() throws ExecutionException, InterruptedException {
+    Assert.assertEquals("ApiOperationJaxrsSchema#sayHello", consumersJaxrs.getIntf().sayHelloAsync().get());
+  }
+
+  @Test
+  public void consumersSpringmvc_A_intf() {
+    Assert.assertEquals("ApiOperationSpringmvcSchema#sayHello", consumersSpringmvc.getIntf().sayHello());
+  }
+
+  @Test
+  public void consumersSpringmvc_B_intf() {
+    Assert.assertEquals("value", consumersSpringmvc.getIntf().sayHello("value"));
+  }
+
+  @Test
+  public void consumersSpringmvc_C_intf() throws ExecutionException, InterruptedException {
+    Assert.assertEquals("value", consumersSpringmvc.getIntf().sayHelloAsync("value").get());
+  }
+
+  @Test
+  public void consumersSpringmvc_D_intf() throws ExecutionException, InterruptedException {
+    Assert.assertEquals("ApiOperationSpringmvcSchema#sayHello", consumersSpringmvc.getIntf().sayHelloAsync().get());
+  }
+}
diff --git a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/ApiOperationJaxrsSchema.java b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/ApiOperationJaxrsSchema.java
new file mode 100644
index 0000000..a0050be
--- /dev/null
+++ b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/ApiOperationJaxrsSchema.java
@@ -0,0 +1,47 @@
+/*
+ * 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.it.schema;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
+
+@RestSchema(schemaId = "apiOperationJaxrsSchemaTest")
+@Path("/v1/apiOperationJaxrsHello")
+public class ApiOperationJaxrsSchema {
+
+  @Path("/sayHi")
+  @GET
+  @Produces("text/plain;charset=UTF-8")
+  @ApiOperation(value = "", nickname = "sayHi")
+  public String sayHello() {
+    return "ApiOperationJaxrsSchema#sayHello";
+  }
+
+  @Path("/sayHello")
+  @GET
+  @Produces("application/json;charset=UTF-8")
+  @ApiOperation(value = "", nickname = "sayHello")
+  public String sayHello(@ApiParam("name") String name) {
+    return name;
+  }
+}
diff --git a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/ApiOperationSpringmvcSchema.java b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/ApiOperationSpringmvcSchema.java
new file mode 100644
index 0000000..9deaa32
--- /dev/null
+++ b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/ApiOperationSpringmvcSchema.java
@@ -0,0 +1,40 @@
+/*
+ * 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.it.schema;
+
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+import io.swagger.annotations.ApiOperation;
+
+@RestSchema(schemaId = "apiOperationSpringmvcSchemaTest")
+@RequestMapping(path = "v1/apiOperationSpringmvcHello")
+public class ApiOperationSpringmvcSchema {
+
+  @RequestMapping(path = "/sayHi", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8")
+  @ApiOperation(value = "", nickname = "sayHi")
+  public String sayHello() {
+    return "ApiOperationSpringmvcSchema#sayHello";
+  }
+
+  @RequestMapping(path = "/sayHello", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
+  @ApiOperation(value = "", nickname = "sayHello")
+  public String sayHello(String name) {
+    return name;
+  }
+}
diff --git a/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/ApiOpertionPojoSchema.java b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/ApiOpertionPojoSchema.java
new file mode 100644
index 0000000..61f4979
--- /dev/null
+++ b/integration-tests/it-producer/src/main/java/org/apache/servicecomb/it/schema/ApiOpertionPojoSchema.java
@@ -0,0 +1,35 @@
+/*
+ * 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.it.schema;
+
+import org.apache.servicecomb.provider.pojo.RpcSchema;
+
+import io.swagger.annotations.ApiOperation;
+
+@RpcSchema(schemaId = "apiOpertionPojoSchemaTest")
+public class ApiOpertionPojoSchema {
+
+  @ApiOperation(value = "", nickname = "sayHi")
+  public String sayHello() {
+    return "ApiOpertionPojoSchema#sayHello";
+  }
+
+  @ApiOperation(value = "", nickname = "sayHello")
+  public String sayHello(String name) {
+    return name;
+  }
+}
diff --git a/providers/provider-pojo/src/main/java/org/apache/servicecomb/provider/pojo/Invoker.java b/providers/provider-pojo/src/main/java/org/apache/servicecomb/provider/pojo/Invoker.java
index 21dfa8d..7d72667 100644
--- a/providers/provider-pojo/src/main/java/org/apache/servicecomb/provider/pojo/Invoker.java
+++ b/providers/provider-pojo/src/main/java/org/apache/servicecomb/provider/pojo/Invoker.java
@@ -37,6 +37,8 @@ import org.apache.servicecomb.swagger.invocation.context.InvocationContextComple
 import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
 import org.springframework.util.StringUtils;
 
+import io.swagger.annotations.ApiOperation;
+
 public class Invoker implements InvocationHandler {
   static class InvokerMeta {
     final ReferenceConfig referenceConfig;
@@ -133,7 +135,8 @@ public class Invoker implements InvocationHandler {
       }
     }
 
-    SwaggerConsumerOperation consumerOperation = currentInvokerMeta.swaggerConsumer.findOperation(method.getName());
+    SwaggerConsumerOperation consumerOperation = currentInvokerMeta.swaggerConsumer
+        .findOperation(findSwaggerMethodName(method));
     if (consumerOperation == null) {
       throw new IllegalStateException(
           String.format(
@@ -158,6 +161,15 @@ public class Invoker implements InvocationHandler {
     return syncInvoke(invocation, consumerOperation);
   }
 
+  protected String findSwaggerMethodName(Method consumerMethod) {
+    ApiOperation apiOperationAnnotation = consumerMethod.getAnnotation(ApiOperation.class);
+    if (apiOperationAnnotation == null || StringUtils.isEmpty(apiOperationAnnotation.nickname())) {
+      return consumerMethod.getName();
+    }
+
+    return apiOperationAnnotation.nickname();
+  }
+
   protected Object syncInvoke(Invocation invocation, SwaggerConsumerOperation consumerOperation) {
     Response response = InvokerUtils.innerSyncInvoke(invocation);
     if (response.isSuccessed()) {
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/SwaggerEnvironment.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/SwaggerEnvironment.java
index 8a5aa2d..d00ff18 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/SwaggerEnvironment.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/SwaggerEnvironment.java
@@ -136,7 +136,7 @@ public class SwaggerEnvironment {
           consumerMethod.getGenericReturnType());
 
       SwaggerConsumerOperation op = new SwaggerConsumerOperation();
-      op.setName(consumerMethod.getName());
+      op.setName(swaggerMethodName);
       op.setConsumerMethod(consumerMethod);
       op.setSwaggerMethod(swaggerMethod);
       op.setArgumentsMapper(argsMapper);
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/unittest/LocalProducerInvoker.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/unittest/LocalProducerInvoker.java
index 31bf31a..ca4d650 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/unittest/LocalProducerInvoker.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/engine/unittest/LocalProducerInvoker.java
@@ -27,6 +27,9 @@ import org.apache.servicecomb.swagger.engine.SwaggerProducer;
 import org.apache.servicecomb.swagger.engine.SwaggerProducerOperation;
 import org.apache.servicecomb.swagger.invocation.Response;
 import org.apache.servicecomb.swagger.invocation.SwaggerInvocation;
+import org.springframework.util.StringUtils;
+
+import io.swagger.annotations.ApiOperation;
 
 public class LocalProducerInvoker implements InvocationHandler {
   private Object proxy;
@@ -73,7 +76,7 @@ public class LocalProducerInvoker implements InvocationHandler {
   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
     invocation = new SwaggerInvocation();
 
-    SwaggerConsumerOperation consumerOp = consumer.findOperation(method.getName());
+    SwaggerConsumerOperation consumerOp = consumer.findOperation(findSwaggerMethodName(method));
     SwaggerProducerOperation producerOp = producer.findOperation(consumerOp.getSwaggerMethod().getName());
 
     consumerOp.getArgumentsMapper().toInvocation(args, invocation);
@@ -91,4 +94,13 @@ public class LocalProducerInvoker implements InvocationHandler {
 
     return future.get();
   }
+
+  protected String findSwaggerMethodName(Method consumerMethod) {
+    ApiOperation apiOperationAnnotation = consumerMethod.getAnnotation(ApiOperation.class);
+    if (apiOperationAnnotation == null || StringUtils.isEmpty(apiOperationAnnotation.nickname())) {
+      return consumerMethod.getName();
+    }
+
+    return apiOperationAnnotation.nickname();
+  }
 }