This is an automated email from the ASF dual-hosted git repository. chanjarster pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/servicecomb-toolkit.git
commit 9916d8c1500e2c89fed0eeece926459538f50c81 Author: kakulisen <[email protected]> AuthorDate: Mon Oct 28 20:48:07 2019 +0800 add oas-generator-servicecomb to support parse servicecomb code Signed-off-by: kakulisen <[email protected]> --- .../{ => oas-generator-servicecomb}/pom.xml | 55 +++++++--------- .../generator/parser/ServicecombJaxrsParser.java | 34 ++++++++++ .../generator/parser/ServicecombPojoParser.java | 74 +++++++++++++++++++++ .../parser/ServicecombSpringmvcParser.java | 33 ++++++++++ ...it.generator.parser.api.OpenApiAnnotationParser | 20 ++++++ .../toolkit/generator/ServiceCombParserTest.java | 76 ++++++++++++++++++++++ oas-generator/pom.xml | 1 + 7 files changed, 263 insertions(+), 30 deletions(-) diff --git a/oas-generator/pom.xml b/oas-generator/oas-generator-servicecomb/pom.xml similarity index 59% copy from oas-generator/pom.xml copy to oas-generator/oas-generator-servicecomb/pom.xml index 849e520..55cf0c9 100644 --- a/oas-generator/pom.xml +++ b/oas-generator/oas-generator-servicecomb/pom.xml @@ -19,48 +19,43 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> - <artifactId>toolkit</artifactId> + <artifactId>oas-generator</artifactId> <groupId>org.apache.servicecomb.toolkit</groupId> <version>0.2.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> - <packaging>pom</packaging> - <modules> - <module>oas-generator-core</module> - <module>oas-generator-jaxrs</module> - <module>oas-generator-spring</module> - </modules> - <artifactId>oas-generator</artifactId> + <artifactId>oas-generator-servicecomb</artifactId> <dependencies> <dependency> - <groupId>org.powermock</groupId> - <artifactId>powermock-module-junit4</artifactId> - <version>1.6.2</version> - <scope>test</scope> + <groupId>org.apache.servicecomb.toolkit</groupId> + <artifactId>oas-generator-jaxrs</artifactId> </dependency> <dependency> - <groupId>org.powermock</groupId> - <artifactId>powermock-api-mockito</artifactId> - <version>1.6.2</version> - <scope>test</scope> + <groupId>org.apache.servicecomb.toolkit</groupId> + <artifactId>oas-generator-spring</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.servicecomb</groupId> + <artifactId>provider-rest-common</artifactId> + <version>1.2.0</version> </dependency> - </dependencies> - <build> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <version>3.8.1</version> - <configuration> - <target>1.8</target> - <source>1.8</source> - </configuration> - </plugin> - </plugins> - </build> + <dependency> + <groupId>org.apache.servicecomb</groupId> + <artifactId>provider-pojo</artifactId> + <version>1.2.0</version> + <exclusions> + <exclusion> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + </exclusion> + </exclusions> + </dependency> + + </dependencies> </project> \ No newline at end of file diff --git a/oas-generator/oas-generator-servicecomb/src/main/java/org/apache/servicecomb/toolkit/generator/parser/ServicecombJaxrsParser.java b/oas-generator/oas-generator-servicecomb/src/main/java/org/apache/servicecomb/toolkit/generator/parser/ServicecombJaxrsParser.java new file mode 100644 index 0000000..6b79e9b --- /dev/null +++ b/oas-generator/oas-generator-servicecomb/src/main/java/org/apache/servicecomb/toolkit/generator/parser/ServicecombJaxrsParser.java @@ -0,0 +1,34 @@ +/* + * 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.toolkit.generator.parser; + +import javax.ws.rs.Path; + +import org.apache.servicecomb.provider.rest.common.RestSchema; + +public class ServicecombJaxrsParser extends JaxRsAnnotationParser { + + @Override + public boolean canProcess(Class<?> cls) { + if (cls.getAnnotation(RestSchema.class) != null && cls.getAnnotation(Path.class) != null) { + return true; + } + + return false; + } +} diff --git a/oas-generator/oas-generator-servicecomb/src/main/java/org/apache/servicecomb/toolkit/generator/parser/ServicecombPojoParser.java b/oas-generator/oas-generator-servicecomb/src/main/java/org/apache/servicecomb/toolkit/generator/parser/ServicecombPojoParser.java new file mode 100644 index 0000000..beb42d5 --- /dev/null +++ b/oas-generator/oas-generator-servicecomb/src/main/java/org/apache/servicecomb/toolkit/generator/parser/ServicecombPojoParser.java @@ -0,0 +1,74 @@ +/* + * 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.toolkit.generator.parser; + +import java.lang.reflect.Method; + +import org.apache.commons.lang3.StringUtils; +import org.apache.servicecomb.provider.pojo.RpcSchema; +import org.apache.servicecomb.toolkit.generator.OperationContext; + +import io.swagger.v3.oas.models.PathItem.HttpMethod; + +public class ServicecombPojoParser extends AbstractAnnotationParser { + + @Override + public int getOrder() { + return 0; + } + + @Override + public boolean canProcess(Class<?> cls) { + if (cls.getAnnotation(RpcSchema.class) != null) { + return true; + } + + return false; + } + + + @Override + public void postParseMethodAnnotation(OperationContext context) { + super.postParseMethodAnnotation(context); + + Method currentMethod = context.getMethod(); + + if (StringUtils.isEmpty(context.getHttpMethod())) { + context.setHttpMethod(HttpMethod.POST.toString()); + } + + if (StringUtils.isEmpty(context.getOperationId())) { + context.setOperationId(currentMethod.getName()); + } + + if (StringUtils.isEmpty(context.getPath())) { + context.setPath(correctPath(currentMethod.getName())); + } + + if (context.getApiResponses() == null || context.getApiResponses().size() == 0) { + context.correctResponse(context.getApiResponses()); + } + } + + private String correctPath(String path) { + if (path == null || path.startsWith("/")) { + return path; + } + return "/" + path; + } +} diff --git a/oas-generator/oas-generator-servicecomb/src/main/java/org/apache/servicecomb/toolkit/generator/parser/ServicecombSpringmvcParser.java b/oas-generator/oas-generator-servicecomb/src/main/java/org/apache/servicecomb/toolkit/generator/parser/ServicecombSpringmvcParser.java new file mode 100644 index 0000000..7f59bc2 --- /dev/null +++ b/oas-generator/oas-generator-servicecomb/src/main/java/org/apache/servicecomb/toolkit/generator/parser/ServicecombSpringmvcParser.java @@ -0,0 +1,33 @@ +/* + * 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.toolkit.generator.parser; + +import org.apache.servicecomb.provider.rest.common.RestSchema; +import org.springframework.web.bind.annotation.RequestMapping; + +public class ServicecombSpringmvcParser extends SpringmvcAnnotationParser { + + @Override + public boolean canProcess(Class<?> cls) { + if (cls.getAnnotation(RestSchema.class) != null && cls.getAnnotation(RequestMapping.class) != null) { + return true; + } + + return false; + } +} diff --git a/oas-generator/oas-generator-servicecomb/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.generator.parser.api.OpenApiAnnotationParser b/oas-generator/oas-generator-servicecomb/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.generator.parser.api.OpenApiAnnotationParser new file mode 100644 index 0000000..0658b91 --- /dev/null +++ b/oas-generator/oas-generator-servicecomb/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.generator.parser.api.OpenApiAnnotationParser @@ -0,0 +1,20 @@ +# +# 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.toolkit.generator.parser.ServicecombJaxrsParser +org.apache.servicecomb.toolkit.generator.parser.ServicecombSpringmvcParser +org.apache.servicecomb.toolkit.generator.parser.ServicecombPojoParser \ No newline at end of file diff --git a/oas-generator/oas-generator-servicecomb/src/test/java/org/apache/servicecomb/toolkit/generator/ServiceCombParserTest.java b/oas-generator/oas-generator-servicecomb/src/test/java/org/apache/servicecomb/toolkit/generator/ServiceCombParserTest.java new file mode 100644 index 0000000..78c6bfc --- /dev/null +++ b/oas-generator/oas-generator-servicecomb/src/test/java/org/apache/servicecomb/toolkit/generator/ServiceCombParserTest.java @@ -0,0 +1,76 @@ +/* + * 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.toolkit.generator; + +import javax.ws.rs.Path; + +import org.apache.servicecomb.provider.pojo.RpcSchema; +import org.apache.servicecomb.provider.rest.common.RestSchema; +import org.apache.servicecomb.toolkit.generator.parser.ServicecombJaxrsParser; +import org.apache.servicecomb.toolkit.generator.parser.ServicecombPojoParser; +import org.apache.servicecomb.toolkit.generator.parser.ServicecombSpringmvcParser; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.web.bind.annotation.RequestMapping; + +public class ServiceCombParserTest { + + @Test + public void parse() { + + ServicecombJaxrsParser servicecombJaxrsParser = new ServicecombJaxrsParser(); + ServicecombPojoParser servicecombPojoParser = new ServicecombPojoParser(); + ServicecombSpringmvcParser servicecombSpringmvcParser = new ServicecombSpringmvcParser(); + + boolean canProcess = servicecombJaxrsParser.canProcess(ServicecombJaxrs.class); + Assert.assertTrue(canProcess); + + canProcess = servicecombSpringmvcParser.canProcess(ServicecombSpringmvc.class); + Assert.assertTrue(canProcess); + + canProcess = servicecombPojoParser.canProcess(ServicecombPojo.class); + Assert.assertTrue(canProcess); + + OasContext pojoOasContext = new OasContext(servicecombPojoParser); + servicecombPojoParser.parser(ServicecombPojo.class, pojoOasContext); + Assert.assertNull(pojoOasContext.getBasePath()); + } + + + @RestSchema(schemaId = "servicecombJaxrs") + @Path("/") + class ServicecombJaxrs { + + @Path("/path") + public Object path() { + return null; + } + } + + @RestSchema(schemaId = "servicecombSpringmvc") + @RequestMapping + class ServicecombSpringmvc { + } + + @RpcSchema + class ServicecombPojo { + public Object path() { + return null; + } + } +} diff --git a/oas-generator/pom.xml b/oas-generator/pom.xml index 849e520..9603fdb 100644 --- a/oas-generator/pom.xml +++ b/oas-generator/pom.xml @@ -29,6 +29,7 @@ <module>oas-generator-core</module> <module>oas-generator-jaxrs</module> <module>oas-generator-spring</module> + <module>oas-generator-servicecomb</module> </modules> <artifactId>oas-generator</artifactId>
