You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/01/12 01:19:12 UTC

[GitHub] liubao68 closed pull request #501: SCB-179 support -(identifiers) as parameter name with RequestParam

liubao68 closed pull request #501: SCB-179 support -(identifiers) as parameter name with RequestParam
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/501
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/common/common-protobuf/src/main/java/io/servicecomb/codec/protobuf/utils/ProtobufSchemaUtils.java b/common/common-protobuf/src/main/java/io/servicecomb/codec/protobuf/utils/ProtobufSchemaUtils.java
index eb02155f9..dd5911dba 100644
--- a/common/common-protobuf/src/main/java/io/servicecomb/codec/protobuf/utils/ProtobufSchemaUtils.java
+++ b/common/common-protobuf/src/main/java/io/servicecomb/codec/protobuf/utils/ProtobufSchemaUtils.java
@@ -25,6 +25,8 @@
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
+import javax.lang.model.SourceVersion;
+
 import org.springframework.util.ClassUtils;
 
 import com.fasterxml.jackson.databind.JavaType;
@@ -147,8 +149,8 @@ public static WrapSchema getOrCreateArgsSchema(OperationMeta operationMeta) {
       Parameter[] params = method.getParameters();
       for (int idx = 0; idx < params.length; idx++) {
         Parameter param = params[idx];
-        String paramName = operationMeta.getParamName(idx);
-
+        String paramName = io.servicecomb.swagger.generator.core.utils.ClassUtils
+            .correctMethodParameterName(operationMeta.getParamName(idx));
         config.addField(paramName, param.getParameterizedType());
       }
 
diff --git a/integration-tests/jaxrs-tests/src/test/java/io/servicecomb/demo/jaxrs/tests/JaxrsIntegrationTestBase.java b/integration-tests/jaxrs-tests/src/test/java/io/servicecomb/demo/jaxrs/tests/JaxrsIntegrationTestBase.java
index 4487aef2e..3d5df3f32 100644
--- a/integration-tests/jaxrs-tests/src/test/java/io/servicecomb/demo/jaxrs/tests/JaxrsIntegrationTestBase.java
+++ b/integration-tests/jaxrs-tests/src/test/java/io/servicecomb/demo/jaxrs/tests/JaxrsIntegrationTestBase.java
@@ -237,6 +237,24 @@ public void ableToPostWithHeader() {
     }
   }
 
+  @Test
+  public void ableToPostWithHeaderWithIdentifier() {
+    Person person = new Person();
+    person.setName("person name");
+
+    HttpHeaders headers = new HttpHeaders();
+    headers.setContentType(APPLICATION_JSON);
+    headers.add("prefix-test", "prefix  prefix");
+
+    HttpEntity<Person> requestEntity = new HttpEntity<>(person, headers);
+    for (String url : urls) {
+      ResponseEntity<String> responseEntity = restTemplate
+          .postForEntity(url + "saysomething1", requestEntity, String.class);
+
+      assertEquals("prefix  prefix person name", jsonBodyOf(responseEntity, String.class));
+    }
+  }
+
   @Test
   public void ableToPostObjectAsJson() {
     Map<String, String> personFieldMap = new HashMap<>();
diff --git a/integration-tests/jaxrs-tests/src/test/java/io/servicecomb/demo/jaxrs/tests/endpoints/SchemaFirstJaxrs.java b/integration-tests/jaxrs-tests/src/test/java/io/servicecomb/demo/jaxrs/tests/endpoints/SchemaFirstJaxrs.java
index b13d94ac8..5e926450f 100644
--- a/integration-tests/jaxrs-tests/src/test/java/io/servicecomb/demo/jaxrs/tests/endpoints/SchemaFirstJaxrs.java
+++ b/integration-tests/jaxrs-tests/src/test/java/io/servicecomb/demo/jaxrs/tests/endpoints/SchemaFirstJaxrs.java
@@ -35,6 +35,8 @@
 
   String saySomething(String prefix, Person user);
 
+  String saySomething1(String prefix_test, Person user);
+
   String sayHi(String name);
 
   String sayHi2(String name);
diff --git a/integration-tests/jaxrs-tests/src/test/java/io/servicecomb/demo/jaxrs/tests/endpoints/SomeAbstractJaxrsRestEndpoint.java b/integration-tests/jaxrs-tests/src/test/java/io/servicecomb/demo/jaxrs/tests/endpoints/SomeAbstractJaxrsRestEndpoint.java
index 796ee7e89..a639700c5 100644
--- a/integration-tests/jaxrs-tests/src/test/java/io/servicecomb/demo/jaxrs/tests/endpoints/SomeAbstractJaxrsRestEndpoint.java
+++ b/integration-tests/jaxrs-tests/src/test/java/io/servicecomb/demo/jaxrs/tests/endpoints/SomeAbstractJaxrsRestEndpoint.java
@@ -106,6 +106,12 @@ public String saySomething(@HeaderParam("prefix") String prefix, Person user) {
     return prefix + " " + user.getName();
   }
 
+  @Path("/saysomething1")
+  @POST
+  public String saySomething1(@HeaderParam("prefix-test") String prefix_test, Person user) {
+    return prefix_test + " " + user.getName();
+  }
+
   @Path("/sayhi/{name}")
   @PUT
   public String sayHi(@PathParam("name") String name) {
diff --git a/integration-tests/jaxrs-tests/src/test/resources/microservices/jaxrs/schemaFirst.yaml b/integration-tests/jaxrs-tests/src/test/resources/microservices/jaxrs/schemaFirst.yaml
index 11ff68a97..3d23f7153 100644
--- a/integration-tests/jaxrs-tests/src/test/resources/microservices/jaxrs/schemaFirst.yaml
+++ b/integration-tests/jaxrs-tests/src/test/resources/microservices/jaxrs/schemaFirst.yaml
@@ -111,6 +111,24 @@ paths:
           description: say something
           schema:
             type: string
+  /saysomething1:
+    post:
+      operationId: saySomething1
+      parameters:
+        - name: prefix-test
+          in: header
+          required: true
+          type: string
+        - name: user
+          in: body
+          required: true
+          schema:
+            $ref: '#/definitions/Person'
+      responses:
+        200:
+          description: say something
+          schema:
+            type: string
   /sayhi/{name}:
     put:
       operationId: sayHi
diff --git a/swagger/swagger-generator/generator-core/src/main/java/io/servicecomb/swagger/generator/core/utils/ClassUtils.java b/swagger/swagger-generator/generator-core/src/main/java/io/servicecomb/swagger/generator/core/utils/ClassUtils.java
index d433c3ec9..ca666e023 100644
--- a/swagger/swagger-generator/generator-core/src/main/java/io/servicecomb/swagger/generator/core/utils/ClassUtils.java
+++ b/swagger/swagger-generator/generator-core/src/main/java/io/servicecomb/swagger/generator/core/utils/ClassUtils.java
@@ -24,6 +24,9 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+
+import javax.lang.model.SourceVersion;
+
 import java.util.Set;
 
 import org.springframework.util.StringUtils;
@@ -49,64 +52,6 @@
   // reference:
   //  https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
   //  https://en.wikipedia.org/wiki/List_of_Java_keywords
-  private static final Set<String> JAVA_RESERVED_WORDS = new HashSet<>();
-
-  static {
-    JAVA_RESERVED_WORDS.addAll(Arrays.asList("true",
-        "false",
-        "null",
-        "abstract",
-        "continue",
-        "for",
-        "new",
-        "switch",
-        "assert",
-        "default",
-        "goto",
-        "package",
-        "synchronized",
-        "boolean",
-        "do",
-        "if",
-        "private",
-        "this",
-        "break",
-        "double",
-        "implements",
-        "protected",
-        "throw",
-        "byte",
-        "else",
-        "import",
-        "public",
-        "throws",
-        "case",
-        "enum",
-        "instanceof",
-        "return",
-        "transient",
-        "catch",
-        "extends",
-        "int",
-        "short",
-        "try",
-        "char",
-        "final",
-        "interface",
-        "static",
-        "void",
-        "class",
-        "finally",
-        "long",
-        "strictfp",
-        "volatile",
-        "const",
-        "float",
-        "native",
-        "super",
-        "while"));
-  }
-
   private ClassUtils() {
   }
 
@@ -279,11 +224,27 @@ public static boolean isRawJsonType(Parameter param) {
     return JavassistUtils.createClass(classLoader, classConfig);
   }
 
-  public static String correctMethodParameterName(String name) {
-    return name.replace(".", "_").replace("-", "_");
+  public static String correctMethodParameterName(String paramName) {
+    if (SourceVersion.isName(paramName)) {
+      return paramName;
+    }
+    StringBuffer newParam = new StringBuffer();
+    char tempChar;
+    for (int index = 0; index < paramName.length(); index++) {
+      tempChar = paramName.charAt(index);
+      if (Character.isJavaIdentifierPart(tempChar)) {
+        newParam.append(paramName.charAt(index));
+      } else if (tempChar == '.' || tempChar == '-') {
+        newParam.append('_');
+      }
+    }
+    return newParam.toString();
   }
 
   public static String correctClassName(String name) {
+    if (SourceVersion.isIdentifier(name) && !SourceVersion.isKeyword(name)) {
+      return name;
+    }
     String parts[] = name.split("\\.", -1);
     for (int idx = 0; idx < parts.length; idx++) {
       String part = parts[idx];
@@ -293,7 +254,7 @@ public static String correctClassName(String name) {
       }
 
       part = part.replace('-', '_');
-      if (Character.isDigit(part.charAt(0)) || JAVA_RESERVED_WORDS.contains(part)) {
+      if (Character.isDigit(part.charAt(0)) || SourceVersion.isKeyword(part)) {
         part = "_" + part;
       }
       parts[idx] = part;


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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