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 0afd957bf3a040d43274b64b2ba5567c57aab6c4 Author: kakulisen <[email protected]> AuthorDate: Sat Oct 26 09:13:44 2019 +0800 add oas-generator-jaxrs to support parse jaxrs code Signed-off-by: kakulisen <[email protected]> --- oas-generator/{ => oas-generator-jaxrs}/pom.xml | 36 ++---- .../annotation/HttpMethodAnnotationProcessor.java | 32 +++++ .../annotation/PathClassAnnotationProcessor.java | 29 +++++ .../annotation/PathMethodAnnotationProcessor.java | 29 +++++ .../generator/parser/JaxRsAnnotationParser.java | 69 +++++++++++ ...it.generator.parser.api.OpenApiAnnotationParser | 18 +++ .../generator/JaxrsAnnotationProcessorTest.java | 79 ++++++++++++ .../toolkit/generator/JaxrsParserTest.java | 66 ++++++++++ .../toolkit/generator/ServerOperationResource.java | 138 +++++++++++++++++++++ oas-generator/pom.xml | 1 + 10 files changed, 468 insertions(+), 29 deletions(-) diff --git a/oas-generator/pom.xml b/oas-generator/oas-generator-jaxrs/pom.xml similarity index 61% copy from oas-generator/pom.xml copy to oas-generator/oas-generator-jaxrs/pom.xml index 123ec2c..b634cb0 100644 --- a/oas-generator/pom.xml +++ b/oas-generator/oas-generator-jaxrs/pom.xml @@ -19,46 +19,24 @@ 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> - </modules> - <artifactId>oas-generator</artifactId> + <artifactId>oas-generator-jaxrs</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-core</artifactId> </dependency> <dependency> - <groupId>org.powermock</groupId> - <artifactId>powermock-api-mockito</artifactId> - <version>1.6.2</version> - <scope>test</scope> + <groupId>javax.ws.rs</groupId> + <artifactId>javax.ws.rs-api</artifactId> + <version>2.1</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> - </project> \ No newline at end of file diff --git a/oas-generator/oas-generator-jaxrs/src/main/java/org/apache/servicecomb/toolkit/generator/annotation/HttpMethodAnnotationProcessor.java b/oas-generator/oas-generator-jaxrs/src/main/java/org/apache/servicecomb/toolkit/generator/annotation/HttpMethodAnnotationProcessor.java new file mode 100644 index 0000000..fbe0e83 --- /dev/null +++ b/oas-generator/oas-generator-jaxrs/src/main/java/org/apache/servicecomb/toolkit/generator/annotation/HttpMethodAnnotationProcessor.java @@ -0,0 +1,32 @@ +/* + * 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.annotation; + +import java.lang.annotation.Annotation; + +import javax.ws.rs.HttpMethod; + +import org.apache.servicecomb.toolkit.generator.OperationContext; + +public class HttpMethodAnnotationProcessor implements MethodAnnotationProcessor<Annotation, OperationContext> { + @Override + public void process(Annotation annotation, OperationContext operationContext) { + HttpMethod httpMethod = annotation.annotationType().getAnnotation(HttpMethod.class); + operationContext.setHttpMethod(httpMethod.value()); + } +} diff --git a/oas-generator/oas-generator-jaxrs/src/main/java/org/apache/servicecomb/toolkit/generator/annotation/PathClassAnnotationProcessor.java b/oas-generator/oas-generator-jaxrs/src/main/java/org/apache/servicecomb/toolkit/generator/annotation/PathClassAnnotationProcessor.java new file mode 100644 index 0000000..42c5f72 --- /dev/null +++ b/oas-generator/oas-generator-jaxrs/src/main/java/org/apache/servicecomb/toolkit/generator/annotation/PathClassAnnotationProcessor.java @@ -0,0 +1,29 @@ +/* + * 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.annotation; + +import javax.ws.rs.Path; + +import org.apache.servicecomb.toolkit.generator.OasContext; + +public class PathClassAnnotationProcessor implements ClassAnnotationProcessor<Path, OasContext> { + @Override + public void process(Path path, OasContext context) { + context.setBasePath(path.value()); + } +} diff --git a/oas-generator/oas-generator-jaxrs/src/main/java/org/apache/servicecomb/toolkit/generator/annotation/PathMethodAnnotationProcessor.java b/oas-generator/oas-generator-jaxrs/src/main/java/org/apache/servicecomb/toolkit/generator/annotation/PathMethodAnnotationProcessor.java new file mode 100644 index 0000000..187006c --- /dev/null +++ b/oas-generator/oas-generator-jaxrs/src/main/java/org/apache/servicecomb/toolkit/generator/annotation/PathMethodAnnotationProcessor.java @@ -0,0 +1,29 @@ +/* + * 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.annotation; + +import javax.ws.rs.Path; + +import org.apache.servicecomb.toolkit.generator.OperationContext; + +public class PathMethodAnnotationProcessor implements MethodAnnotationProcessor<Path, OperationContext> { + @Override + public void process(Path path, OperationContext context) { + context.setPath(path.value()); + } +} diff --git a/oas-generator/oas-generator-jaxrs/src/main/java/org/apache/servicecomb/toolkit/generator/parser/JaxRsAnnotationParser.java b/oas-generator/oas-generator-jaxrs/src/main/java/org/apache/servicecomb/toolkit/generator/parser/JaxRsAnnotationParser.java new file mode 100644 index 0000000..a5b0b12 --- /dev/null +++ b/oas-generator/oas-generator-jaxrs/src/main/java/org/apache/servicecomb/toolkit/generator/parser/JaxRsAnnotationParser.java @@ -0,0 +1,69 @@ +/* + * 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.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.HEAD; +import javax.ws.rs.OPTIONS; +import javax.ws.rs.PATCH; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; + +import org.apache.servicecomb.toolkit.generator.annotation.HttpMethodAnnotationProcessor; +import org.apache.servicecomb.toolkit.generator.annotation.PathClassAnnotationProcessor; +import org.apache.servicecomb.toolkit.generator.annotation.PathMethodAnnotationProcessor; + +public class JaxRsAnnotationParser extends AbstractAnnotationParser { + + @Override + public void initClassAnnotationProcessor() { + super.initClassAnnotationProcessor(); + classAnnotationMap.put(Path.class, new PathClassAnnotationProcessor()); + } + + @Override + public void initMethodAnnotationProcessor() { + super.initMethodAnnotationProcessor(); + methodAnnotationMap.put(Path.class, new PathMethodAnnotationProcessor()); + + HttpMethodAnnotationProcessor httpMethodAnnotationProcessor = new HttpMethodAnnotationProcessor(); + methodAnnotationMap.put(GET.class, httpMethodAnnotationProcessor); + methodAnnotationMap.put(POST.class, httpMethodAnnotationProcessor); + methodAnnotationMap.put(DELETE.class, httpMethodAnnotationProcessor); + methodAnnotationMap.put(PATCH.class, httpMethodAnnotationProcessor); + methodAnnotationMap.put(PUT.class, httpMethodAnnotationProcessor); + methodAnnotationMap.put(OPTIONS.class, httpMethodAnnotationProcessor); + methodAnnotationMap.put(HEAD.class, httpMethodAnnotationProcessor); + } + + @Override + public int getOrder() { + return 100; + } + + @Override + public boolean canProcess(Class<?> cls) { + + if (cls.getAnnotation(Path.class) != null) { + return true; + } + return false; + } +} diff --git a/oas-generator/oas-generator-jaxrs/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.generator.parser.api.OpenApiAnnotationParser b/oas-generator/oas-generator-jaxrs/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.generator.parser.api.OpenApiAnnotationParser new file mode 100644 index 0000000..7f60cef --- /dev/null +++ b/oas-generator/oas-generator-jaxrs/src/main/resources/META-INF/services/org.apache.servicecomb.toolkit.generator.parser.api.OpenApiAnnotationParser @@ -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.toolkit.generator.parser.JaxRsAnnotationParser \ No newline at end of file diff --git a/oas-generator/oas-generator-jaxrs/src/test/java/org/apache/servicecomb/toolkit/generator/JaxrsAnnotationProcessorTest.java b/oas-generator/oas-generator-jaxrs/src/test/java/org/apache/servicecomb/toolkit/generator/JaxrsAnnotationProcessorTest.java new file mode 100644 index 0000000..72f49e5 --- /dev/null +++ b/oas-generator/oas-generator-jaxrs/src/test/java/org/apache/servicecomb/toolkit/generator/JaxrsAnnotationProcessorTest.java @@ -0,0 +1,79 @@ +/* + * 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.GET; +import javax.ws.rs.HttpMethod; +import javax.ws.rs.Path; + +import org.apache.servicecomb.toolkit.generator.annotation.HttpMethodAnnotationProcessor; +import org.apache.servicecomb.toolkit.generator.annotation.PathClassAnnotationProcessor; +import org.apache.servicecomb.toolkit.generator.annotation.PathMethodAnnotationProcessor; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +public class JaxrsAnnotationProcessorTest { + + @Test + public void processApiResponseAnnotation() throws NoSuchMethodException { + + OasContext oasContext = new OasContext(null); + OperationContext context = new OperationContext(null, oasContext); + HttpMethodAnnotationProcessor httpMethodAnnotationProcessor = new HttpMethodAnnotationProcessor(); + + GET get = GetClass.class.getMethod("get").getAnnotation(GET.class); + httpMethodAnnotationProcessor.process(get, context); + + Assert.assertEquals(HttpMethod.GET, context.getHttpMethod()); + } + + @Test + public void processPathClassAnnotation() { + + OasContext oasContext = new OasContext(null); + PathClassAnnotationProcessor httpMethodAnnotationProcessor = new PathClassAnnotationProcessor(); + + Path path = Mockito.mock(Path.class); + Mockito.when(path.value()).thenReturn("/path"); + httpMethodAnnotationProcessor.process(path, oasContext); + + Assert.assertEquals("/path", oasContext.getBasePath()); + } + + @Test + public void processPathMethodAnnotation() { + + OasContext oasContext = new OasContext(null); + OperationContext operationContext = new OperationContext(null, oasContext); + PathMethodAnnotationProcessor pathMethodAnnotationProcessor = new PathMethodAnnotationProcessor(); + + Path path = Mockito.mock(Path.class); + Mockito.when(path.value()).thenReturn("/path"); + pathMethodAnnotationProcessor.process(path, operationContext); + + Assert.assertEquals("/path", operationContext.getPath()); + } + + class GetClass { + @GET + public String get() { + return "get"; + } + } +} diff --git a/oas-generator/oas-generator-jaxrs/src/test/java/org/apache/servicecomb/toolkit/generator/JaxrsParserTest.java b/oas-generator/oas-generator-jaxrs/src/test/java/org/apache/servicecomb/toolkit/generator/JaxrsParserTest.java new file mode 100644 index 0000000..cef30d2 --- /dev/null +++ b/oas-generator/oas-generator-jaxrs/src/test/java/org/apache/servicecomb/toolkit/generator/JaxrsParserTest.java @@ -0,0 +1,66 @@ +/* + * 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.toolkit.generator.parser.JaxRsAnnotationParser; +import org.junit.Assert; +import org.junit.Test; + +import io.swagger.v3.oas.models.Components; + +public class JaxrsParserTest { + + @Test + public void parseJaxrs() { + + JaxRsAnnotationParser parser = new JaxRsAnnotationParser(); + boolean canProcess = parser.canProcess(NoResource.class); + Assert.assertEquals(false, canProcess); + + canProcess = parser.canProcess(OneResource.class); + Assert.assertEquals(true, canProcess); + + OasContext context = new OasContext(parser); + parser.parser(OneResource.class, context); + } + + class NoResource { + public String name() { + return "no resource"; + } + } + + @Path("/path") + class OneResource { + + @Path("/name") + public String name() { + return "no resource"; + } + } + + public static void main(String[] args) { + OneResource.class.getMethods(); + Components components1 = new Components(); + Components components2 = new Components(); + + System.out.println(components1.equals(components2)); + } +} diff --git a/oas-generator/oas-generator-jaxrs/src/test/java/org/apache/servicecomb/toolkit/generator/ServerOperationResource.java b/oas-generator/oas-generator-jaxrs/src/test/java/org/apache/servicecomb/toolkit/generator/ServerOperationResource.java new file mode 100644 index 0000000..604e4f5 --- /dev/null +++ b/oas-generator/oas-generator-jaxrs/src/test/java/org/apache/servicecomb/toolkit/generator/ServerOperationResource.java @@ -0,0 +1,138 @@ +/* + * 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.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; + +@Path("") +public class ServerOperationResource { + + @Path("/serversoperation") + @GET + @ApiResponse( + responseCode = "500", + description = "voila!", + content = { + @Content( + mediaType = "application/json", + schema = @Schema(implementation = String.class) + ), + @Content( + mediaType = "application/xml", + schema = @Schema(implementation = String.class) + ) + }) + @ApiResponse(responseCode = "400") + @ApiResponse(responseCode = "404", description = "User not found") + @Operation(operationId = "getPet", description = "Pets Example") + public ResponseBean getPet() { + return new ResponseBean(); + } + + + @Path("/serversoperation2") + @POST + @Operation(operationId = "getPet2", description = "Pets Example") + public String getPet2(@Parameter(required = true) @QueryParam("pet") String pet) { + return ""; + } + + + @Path("/serversoperation3") + @POST + @Operation(operationId = "getPet3", description = "Pets Example") + public String getPet3(String[] pet1, String pet2, RequestBean[] requestBean) { + return ""; + } + + @Path("/serversoperation4") + @POST + @Operation(operationId = "getPet4", description = "Pets Example") + public String getPet4(String[] pet1, RequestBean responseBean) { + return ""; + } + + public String noResource() { + return "no resource"; + } + + class RequestBean { + + private String name; + + private String content; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + } + + class ResponseBean { + + private String status; + + private String content; + + private RequestBean request; + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public RequestBean getRequest() { + return request; + } + + public void setRequest(RequestBean request) { + this.request = request; + } + } +} diff --git a/oas-generator/pom.xml b/oas-generator/pom.xml index 123ec2c..dafeed5 100644 --- a/oas-generator/pom.xml +++ b/oas-generator/pom.xml @@ -27,6 +27,7 @@ <packaging>pom</packaging> <modules> <module>oas-generator-core</module> + <module>oas-generator-jaxrs</module> </modules> <artifactId>oas-generator</artifactId>
