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/06/27 01:02:18 UTC

[servicecomb-java-chassis] branch weak-contract-type updated: [SCB-1323][WIP][WEAK] should ignore parameter of HttpServletRequest when generate swagger

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

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


The following commit(s) were added to refs/heads/weak-contract-type by this push:
     new 4f2941c  [SCB-1323][WIP][WEAK] should ignore parameter of HttpServletRequest when generate swagger
4f2941c is described below

commit 4f2941c63dcbe30e5a7fea87999e2764f864c4e5
Author: wujimin <wu...@huawei.com>
AuthorDate: Mon Jun 24 23:42:35 2019 +0800

    [SCB-1323][WIP][WEAK] should ignore parameter of HttpServletRequest when generate swagger
---
 .../common/log/LogMarkerLeakFixUtils.java          |  2 +-
 .../foundation/common/utils/ReflectUtils.java      | 12 ++++--
 .../common/log/TestLogMarkerLeakFixUtils.java      |  2 +-
 .../HttpServletRequestContextRegister.java         | 31 ++++++++++++++
 ...cecomb.swagger.generator.SwaggerContextRegister | 18 ++++++++
 .../swagger/generator/core/TestSwaggerUtils.java   |  5 +++
 .../swagger/generator/core/schema/Schema.java      |  5 +++
 .../src/test/resources/schemas/allMethod.yaml      | 18 ++++++++
 .../src/test/resources/schemas/ignoreRequest.yaml  | 48 ++++++++++++++++++++++
 9 files changed, 135 insertions(+), 6 deletions(-)

diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/log/LogMarkerLeakFixUtils.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/log/LogMarkerLeakFixUtils.java
index 1ce841f..82ab6a3 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/log/LogMarkerLeakFixUtils.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/log/LogMarkerLeakFixUtils.java
@@ -44,7 +44,7 @@ public final class LogMarkerLeakFixUtils {
 
   @SuppressWarnings("unchecked")
   public static void fix() {
-    Class<?> staticMarkerBinderClass = ReflectUtils.getClassByName(null, "org.slf4j.impl.StaticMarkerBinder");
+    Class<?> staticMarkerBinderClass = ReflectUtils.getClassByName("org.slf4j.impl.StaticMarkerBinder");
     if (staticMarkerBinderClass == null) {
       return;
     }
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/ReflectUtils.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/ReflectUtils.java
index bf85349..9730eda 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/ReflectUtils.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/ReflectUtils.java
@@ -92,12 +92,16 @@ public final class ReflectUtils {
     return (T) Array.newInstance(cls, 0).getClass();
   }
 
-  public static Class<?> getClassByName(ClassLoader classLoader, String clsName) {
-    classLoader = JvmUtils.correctClassLoader(classLoader);
+  public static Class<?> getClassByName(String clsName) {
     try {
-      return classLoader.loadClass(clsName);
+      return Class.forName(clsName);
     } catch (ClassNotFoundException e) {
-      return null;
+      ClassLoader classLoader = JvmUtils.correctClassLoader(null);
+      try {
+        return classLoader.loadClass(clsName);
+      } catch (ClassNotFoundException e1) {
+        return null;
+      }
     }
   }
 }
diff --git a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/log/TestLogMarkerLeakFixUtils.java b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/log/TestLogMarkerLeakFixUtils.java
index 9864932..eaf018e 100644
--- a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/log/TestLogMarkerLeakFixUtils.java
+++ b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/log/TestLogMarkerLeakFixUtils.java
@@ -38,7 +38,7 @@ public class TestLogMarkerLeakFixUtils {
   public void noBinder() {
     new Expectations(ReflectUtils.class) {
       {
-        ReflectUtils.getClassByName(null, "org.slf4j.impl.StaticMarkerBinder");
+        ReflectUtils.getClassByName("org.slf4j.impl.StaticMarkerBinder");
         result = null;
       }
     };
diff --git a/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/core/processor/parameter/HttpServletRequestContextRegister.java b/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/core/processor/parameter/HttpServletRequestContextRegister.java
new file mode 100644
index 0000000..1348568
--- /dev/null
+++ b/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/core/processor/parameter/HttpServletRequestContextRegister.java
@@ -0,0 +1,31 @@
+/*
+ * 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.swagger.generator.core.processor.parameter;
+
+import java.lang.reflect.Type;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.servicecomb.swagger.generator.SwaggerContextRegister;
+
+public class HttpServletRequestContextRegister implements SwaggerContextRegister {
+  @Override
+  public Type getContextType() {
+    return HttpServletRequest.class;
+  }
+}
diff --git a/swagger/swagger-generator/generator-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.generator.SwaggerContextRegister b/swagger/swagger-generator/generator-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.generator.SwaggerContextRegister
new file mode 100644
index 0000000..55d8231
--- /dev/null
+++ b/swagger/swagger-generator/generator-core/src/main/resources/META-INF/services/org.apache.servicecomb.swagger.generator.SwaggerContextRegister
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.servicecomb.swagger.generator.core.processor.parameter.HttpServletRequestContextRegister
\ No newline at end of file
diff --git a/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/generator/core/TestSwaggerUtils.java b/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/generator/core/TestSwaggerUtils.java
index 3a2a051..95a456d 100644
--- a/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/generator/core/TestSwaggerUtils.java
+++ b/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/generator/core/TestSwaggerUtils.java
@@ -208,6 +208,11 @@ public class TestSwaggerUtils {
   }
 
   @Test
+  public void should_ignore_httpServletRequest() {
+    testSchemaMethod("ignoreRequest", "ignoreRequest");
+  }
+
+  @Test
   public void testRepeatOperation() {
     UnitTestSwaggerUtils.testException(
         "OperationId must be unique. method=org.apache.servicecomb.swagger.generator.core.schema.RepeatOperation:add.",
diff --git a/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/generator/core/schema/Schema.java b/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/generator/core/schema/Schema.java
index 0e66f45..8176361 100644
--- a/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/generator/core/schema/Schema.java
+++ b/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/generator/core/schema/Schema.java
@@ -25,6 +25,7 @@ import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.CompletableFuture;
 
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.Part;
 
 import org.apache.servicecomb.foundation.test.scaffolding.model.Color;
@@ -212,4 +213,8 @@ public class Schema {
   public void partList(List<Part> part) {
 
   }
+
+  public void ignoreRequest(HttpServletRequest request, int value) {
+
+  }
 }
diff --git a/swagger/swagger-generator/generator-core/src/test/resources/schemas/allMethod.yaml b/swagger/swagger-generator/generator-core/src/test/resources/schemas/allMethod.yaml
index 96a4a03..c8fcdc3 100644
--- a/swagger/swagger-generator/generator-core/src/test/resources/schemas/allMethod.yaml
+++ b/swagger/swagger-generator/generator-core/src/test/resources/schemas/allMethod.yaml
@@ -27,6 +27,18 @@ consumes:
 produces:
 - "application/json"
 paths:
+  /ignoreRequest:
+    post:
+      operationId: "ignoreRequest"
+      parameters:
+      - in: "body"
+        name: "value"
+        required: false
+        schema:
+          $ref: "#/definitions/ignoreRequestBody"
+      responses:
+        200:
+          description: "response of 200"
   /nestedListString:
     post:
       operationId: "nestedListString"
@@ -498,6 +510,12 @@ paths:
         200:
           description: "response of 200"
 definitions:
+  ignoreRequestBody:
+    type: "object"
+    properties:
+      value:
+        type: "integer"
+        format: "int32"
   nestedListStringBody:
     type: "object"
     properties:
diff --git a/swagger/swagger-generator/generator-core/src/test/resources/schemas/ignoreRequest.yaml b/swagger/swagger-generator/generator-core/src/test/resources/schemas/ignoreRequest.yaml
new file mode 100644
index 0000000..f1896ef
--- /dev/null
+++ b/swagger/swagger-generator/generator-core/src/test/resources/schemas/ignoreRequest.yaml
@@ -0,0 +1,48 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+---
+swagger: "2.0"
+info:
+  version: "1.0.0"
+  title: "swagger definition for org.apache.servicecomb.swagger.generator.core.schema.Schema"
+  x-java-interface: "gen.cse.ms.ut.SchemaIntf"
+basePath: "/Schema"
+consumes:
+- "application/json"
+produces:
+- "application/json"
+paths:
+  /ignoreRequest:
+    post:
+      operationId: "ignoreRequest"
+      parameters:
+      - in: "body"
+        name: "value"
+        required: false
+        schema:
+          $ref: "#/definitions/ignoreRequestBody"
+      responses:
+        200:
+          description: "response of 200"
+definitions:
+  ignoreRequestBody:
+    type: "object"
+    properties:
+      value:
+        type: "integer"
+        format: "int32"