You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by wu...@apache.org on 2018/06/04 01:08:46 UTC

[incubator-servicecomb-java-chassis] 02/03: [SCB-601]valid swagger common errors

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

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

commit 78bed31f1d0cf1da98eb46209730dba95f10e9db
Author: liubao <ba...@huawei.com>
AuthorDate: Thu May 31 19:56:41 2018 +0800

    [SCB-601]valid swagger common errors
---
 .../servicecomb/core/definition/SchemaUtils.java   |  4 ++-
 .../apache/servicecomb/swagger/SwaggerUtils.java   | 25 ++++++++++++++
 .../servicecomb/swagger/TestSwaggerUtils.java      | 17 +++++++++
 .../src/test/resources/swagger1.yaml               | 39 +++++++++++++++++++++
 .../src/test/resources/swagger2.yaml               | 40 ++++++++++++++++++++++
 5 files changed, 124 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/SchemaUtils.java b/core/src/main/java/org/apache/servicecomb/core/definition/SchemaUtils.java
index d93ba9d..80709d9 100644
--- a/core/src/main/java/org/apache/servicecomb/core/definition/SchemaUtils.java
+++ b/core/src/main/java/org/apache/servicecomb/core/definition/SchemaUtils.java
@@ -45,6 +45,8 @@ public final class SchemaUtils {
   }
 
   public static Swagger parseSwagger(String swaggerContent) {
-    return SwaggerUtils.parseSwagger(swaggerContent);
+    Swagger swagger = SwaggerUtils.parseSwagger(swaggerContent);
+    SwaggerUtils.invalidateSwagger(swagger);
+    return swagger;
   }
 }
diff --git a/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/SwaggerUtils.java b/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/SwaggerUtils.java
index 78cbaad..5d19bbc 100644
--- a/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/SwaggerUtils.java
+++ b/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/SwaggerUtils.java
@@ -19,6 +19,8 @@ package org.apache.servicecomb.swagger;
 
 import java.io.IOException;
 import java.net.URL;
+import java.util.List;
+import java.util.Map;
 import java.util.Map.Entry;
 
 import javax.ws.rs.core.Response.Status;
@@ -36,6 +38,8 @@ import io.swagger.models.Operation;
 import io.swagger.models.Path;
 import io.swagger.models.Response;
 import io.swagger.models.Swagger;
+import io.swagger.models.parameters.BodyParameter;
+import io.swagger.models.parameters.Parameter;
 import io.swagger.util.Yaml;
 
 public final class SwaggerUtils {
@@ -67,6 +71,27 @@ public final class SwaggerUtils {
     }
   }
 
+  /**
+   * Provide a method to validate swagger. This method is now implemented to check common errors, and the logic
+   * will be changed when necessary. For internal use only.
+   */
+  public static void invalidateSwagger(Swagger swagger) {
+    Map<String, Path> paths = swagger.getPaths();
+    for (Path path : paths.values()) {
+      Operation operation = path.getPost();
+      if (operation != null) {
+        List<Parameter> parameters = operation.getParameters();
+        for (Parameter parameter : parameters) {
+          if (BodyParameter.class.isInstance(parameter)) {
+            if (((BodyParameter) parameter).getSchema() == null) {
+              throw new ServiceCombException("swagger validator: body parameter schema is empty.");
+            }
+          }
+        }
+      }
+    }
+  }
+
   private static Swagger internalParseSwagger(String swaggerContent)
       throws JsonParseException, JsonMappingException, IOException {
     Swagger swagger = Yaml.mapper().readValue(swaggerContent, Swagger.class);
diff --git a/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/TestSwaggerUtils.java b/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/TestSwaggerUtils.java
index ced37dc..da93f72 100644
--- a/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/TestSwaggerUtils.java
+++ b/swagger/swagger-generator/generator-core/src/test/java/org/apache/servicecomb/swagger/TestSwaggerUtils.java
@@ -174,4 +174,21 @@ public class TestSwaggerUtils {
 
     Assert.assertEquals("response of 200", response.getDescription());
   }
+
+
+  @Test(expected = ServiceCombException.class)
+  public void testInvalidate() throws Exception {
+    URL resource = TestSwaggerUtils.class.getResource("/swagger1.yaml");
+    byte[] buffer = new byte[2048];
+    Swagger swagger = SwaggerUtils.parseSwagger(resource);
+    SwaggerUtils.invalidateSwagger(swagger);
+  }
+
+  @Test
+  public void testInvalidateValid() throws Exception {
+    URL resource = TestSwaggerUtils.class.getResource("/swagger2.yaml");
+    byte[] buffer = new byte[2048];
+    Swagger swagger = SwaggerUtils.parseSwagger(resource);
+    SwaggerUtils.invalidateSwagger(swagger);
+  }
 }
diff --git a/swagger/swagger-generator/generator-core/src/test/resources/swagger1.yaml b/swagger/swagger-generator/generator-core/src/test/resources/swagger1.yaml
new file mode 100644
index 0000000..14d10c4
--- /dev/null
+++ b/swagger/swagger-generator/generator-core/src/test/resources/swagger1.yaml
@@ -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.
+## ---------------------------------------------------------------------------
+
+swagger: '2.0'
+info:
+  title: rest test
+  version: 1.0.0
+basePath: /springmvc/controller
+produces:
+  - application/json
+
+paths:
+  /sayhello:
+    post:
+      operationId: sayHello
+      parameters:
+        - name: name
+          in: body
+          required: true
+          type: string
+      responses:
+        200:
+          description: say hello
+          schema:
+            type: string
\ No newline at end of file
diff --git a/swagger/swagger-generator/generator-core/src/test/resources/swagger2.yaml b/swagger/swagger-generator/generator-core/src/test/resources/swagger2.yaml
new file mode 100644
index 0000000..5f2f23f
--- /dev/null
+++ b/swagger/swagger-generator/generator-core/src/test/resources/swagger2.yaml
@@ -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.
+## ---------------------------------------------------------------------------
+
+swagger: '2.0'
+info:
+  title: rest test
+  version: 1.0.0
+basePath: /springmvc/controller
+produces:
+  - application/json
+
+paths:
+  /sayhello:
+    post:
+      operationId: sayHello
+      parameters:
+        - name: name
+          in: body
+          required: true
+          schema:
+            type: string
+      responses:
+        200:
+          description: say hello
+          schema:
+            type: string
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
wujimin@apache.org.