[ https://issues.apache.org/jira/browse/SCB-601?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16499634#comment-16499634 ]
ASF GitHub Bot commented on SCB-601: ------------------------------------ wujimin closed pull request #743: [SCB-601] when java.lang.Error: not support def type: class io.swagger.models.ComposedMode we don't know which swagger URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/743 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/core/src/main/java/org/apache/servicecomb/core/definition/SchemaMeta.java b/core/src/main/java/org/apache/servicecomb/core/definition/SchemaMeta.java index 565290479..2ced524e7 100644 --- a/core/src/main/java/org/apache/servicecomb/core/definition/SchemaMeta.java +++ b/core/src/main/java/org/apache/servicecomb/core/definition/SchemaMeta.java @@ -57,21 +57,26 @@ private SwaggerToClassGenerator swaggerToClassGenerator; public SchemaMeta(Swagger swagger, MicroserviceMeta microserviceMeta, String schemaId) { - this.packageName = SchemaUtils.generatePackageName(microserviceMeta, schemaId); + try { + this.packageName = SchemaUtils.generatePackageName(microserviceMeta, schemaId); - this.swagger = swagger; - this.name = schemaId; + this.swagger = swagger; + this.name = schemaId; - this.microserviceMeta = microserviceMeta; - this.microserviceQualifiedName = microserviceMeta.getName() + "." + schemaId; + this.microserviceMeta = microserviceMeta; + this.microserviceQualifiedName = microserviceMeta.getName() + "." + schemaId; - swaggerToClassGenerator = new SwaggerToClassGenerator(microserviceMeta.getClassLoader(), swagger, packageName); - swaggerIntf = swaggerToClassGenerator.convert(); + swaggerToClassGenerator = new SwaggerToClassGenerator(microserviceMeta.getClassLoader(), swagger, packageName); + swaggerIntf = swaggerToClassGenerator.convert(); - createOperationMgr("schemaMeta " + schemaId + " operation mgr"); - operationMgr.setRegisterErrorFmt("Operation name repeat, schema=%s, operation=%s"); + createOperationMgr("schemaMeta " + schemaId + " operation mgr"); + operationMgr.setRegisterErrorFmt("Operation name repeat, schema=%s, operation=%s"); - initOperations(); + initOperations(); + } catch (Throwable e) { + LOGGER.error("Unhandled exception to service " + microserviceMeta.getName() + " schema " + schemaId); + throw e; + } } public SwaggerToClassGenerator getSwaggerToClassGenerator() { 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 d93ba9d67..3346a240e 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 static Swagger parseSwagger(URL url) { } public static Swagger parseSwagger(String swaggerContent) { - return SwaggerUtils.parseSwagger(swaggerContent); + Swagger swagger = SwaggerUtils.parseSwagger(swaggerContent); + SwaggerUtils.validateSwagger(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 78cbaadb9..78f319a55 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 @@ 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.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 static Swagger parseSwagger(String swaggerContent) { } } + /** + * 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 validateSwagger(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 ced37dc16..596256881 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 void correctResponsesHavePaths() { 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.validateSwagger(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.validateSwagger(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 000000000..14d10c4d1 --- /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 000000000..5f2f23f66 --- /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 ---------------------------------------------------------------- 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: us...@infra.apache.org > ServiceComb integrated to spring boot or tomcat will print too many logs > ------------------------------------------------------------------------ > > Key: SCB-601 > URL: https://issues.apache.org/jira/browse/SCB-601 > Project: Apache ServiceComb > Issue Type: Task > Reporter: liubao > Assignee: liubao > Priority: Minor > Fix For: java-chassis-1.0.0-m2 > > > 2018-05-23 19:40:31.495 INFO 4812 --- [ntloop-thread-0] > o.a.s.t.r.c.http.RestClientInvocation : http connection connected, > local:192.168.0.204:59718, remote:192.168.0.204:7211. > 2018-05-23 19:40:31.500 INFO 4812 --- [ntloop-thread-0] > o.a.s.t.r.c.http.RestClientInvocation : http connection closed, > local:192.168.0.204:59705, remote:192.168.0.204:7211. > 2 > > In tomcat or some other HTTP short connections scenario, this logs comes too > frequently/ -- This message was sent by Atlassian JIRA (v7.6.3#76005)