This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 752e79ec59f CAMEL-19616: camel-restdsl-openapi-plugin - Use platform-http as default. Remove special spring boot code that is no longer needed that was using the old way with Servlet. 752e79ec59f is described below commit 752e79ec59f4a1aa2ca889b9cf6c4e1ba7744c45 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Jul 19 15:34:23 2023 +0200 CAMEL-19616: camel-restdsl-openapi-plugin - Use platform-http as default. Remove special spring boot code that is no longer needed that was using the old way with Servlet. --- .../ROOT/pages/camel-4-migration-guide.adoc | 6 + .../main/docs/camel-restdsl-openapi-plugin.adoc | 31 ----- .../generator/openapi/AbstractGenerateMojo.java | 8 +- .../maven/generator/openapi/GenerateMojo.java | 41 ++----- .../openapi/RestDslSourceCodeGenerator.java | 10 +- .../generator/openapi/RestDslXmlGenerator.java | 13 +- .../generator/openapi/RestDslYamlGenerator.java | 13 +- .../SpringBootProjectSourceCodeGenerator.java | 135 --------------------- .../PathSpringBootProjectSourceGeneratorTest.java | 48 -------- .../test/resources/SpringBootRestController.txt | 25 ---- 10 files changed, 42 insertions(+), 288 deletions(-) diff --git a/docs/user-manual/modules/ROOT/pages/camel-4-migration-guide.adoc b/docs/user-manual/modules/ROOT/pages/camel-4-migration-guide.adoc index a6f214089a9..b4bedb825ae 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-4-migration-guide.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-4-migration-guide.adoc @@ -239,6 +239,12 @@ To use attributes instead: </circuitBreaker> ---- +== OpenAPI Maven Plugin + +The `camel-restdsl-openapi-plugin` Maven plugin now uses `platform-http` as the default rest component +in the generated Rest DSL code. Previously the default was servlet. However, platform-http is a better +default that works out of the box with Spring Boot and Quarkus. + == Component changes === Category diff --git a/tooling/maven/camel-restdsl-openapi-plugin/src/main/docs/camel-restdsl-openapi-plugin.adoc b/tooling/maven/camel-restdsl-openapi-plugin/src/main/docs/camel-restdsl-openapi-plugin.adoc index 8aba70b1e7a..48d06cb473c 100644 --- a/tooling/maven/camel-restdsl-openapi-plugin/src/main/docs/camel-restdsl-openapi-plugin.adoc +++ b/tooling/maven/camel-restdsl-openapi-plugin/src/main/docs/camel-restdsl-openapi-plugin.adoc @@ -80,23 +80,8 @@ in the `<configuration>` tag. | `apiContextPath` | | Define openapi endpoint path if `restConfiguration` is set to `true`. | `clientRequestValidation` | `false` | Whether to enable request validation. | `basePath` | | Overrides the api base path as defined in the OpenAPI specification. -| `requestMappingValues` | `/**` | Allows generation of custom RequestMapping mapping values. Multiple mapping values can be passed as: -`` -<requestMappingValues> - <param>/my-api-path/\****</param> - <param>/my-other-path/**</param> -</requestMappingValues> -`` -| |======================================== -=== Spring Boot Project with Servlet component - -If the Maven project is a Spring Boot project and `restConfiguration` is enabled and the servlet component -is being used as REST component, then this plugin will autodetect the package name (if packageName has not been explicit configured) - where the `@SpringBootApplication` main class is located, and use the same package name - for generating Rest DSL source code and a needed `CamelRestController` support class. - == camel-restdsl-openapi:generate-with-dto Works as `generate` goal but also generates DTO model classes by automatic executing @@ -166,14 +151,6 @@ in the `<configuration>` tag. | `apiContextPath` | | Define openapi endpoint path if `restConfiguration` is set to `true`. | `clientRequestValidation` | `false` | Whether to enable request validation. | `basePath` | | Overrides the api base path as defined in the OpenAPI specification. -| `requestMappingValues` | `/**` | Allows generation of custom RequestMapping mapping values. Multiple mapping values can be passed as: -`` -<requestMappingValues> - <param>/my-api-path/\****</param> - <param>/my-other-path/**</param> -</requestMappingValues> -`` -| |======================================== == camel-restdsl-openapi:generate-xml-with-dto @@ -244,14 +221,6 @@ in the `<configuration>` tag. | `apiContextPath` | | Define openapi endpoint path if `restConfiguration` is set to `true`. | `clientRequestValidation` | `false` | Whether to enable request validation. | `basePath` | | Overrides the api base path as defined in the OpenAPI specification. -| `requestMappingValues` | `/**` | Allows generation of custom RequestMapping mapping values. Multiple mapping values can be passed as: -`` -<requestMappingValues> -<param>/my-api-path/\****</param> -<param>/my-other-path/**</param> -</requestMappingValues> -`` -| |======================================== == camel-restdsl-openapi:generate-yaml-with-dto diff --git a/tooling/maven/camel-restdsl-openapi-plugin/src/main/java/org/apache/camel/maven/generator/openapi/AbstractGenerateMojo.java b/tooling/maven/camel-restdsl-openapi-plugin/src/main/java/org/apache/camel/maven/generator/openapi/AbstractGenerateMojo.java index dedc92fbd96..d45363e069f 100644 --- a/tooling/maven/camel-restdsl-openapi-plugin/src/main/java/org/apache/camel/maven/generator/openapi/AbstractGenerateMojo.java +++ b/tooling/maven/camel-restdsl-openapi-plugin/src/main/java/org/apache/camel/maven/generator/openapi/AbstractGenerateMojo.java @@ -365,15 +365,15 @@ abstract class AbstractGenerateMojo extends AbstractMojo { if (comp != null) { getLog().info("Detected Camel Rest component from classpath: " + comp); } else { - comp = "servlet"; + comp = "platform-http"; String gid = "org.apache.camel"; - String aid = "camel-servlet"; + String aid = "camel-platform-http"; // is it spring boot? if (detectSpringBootFromClasspath()) { gid = "org.apache.camel.springboot"; - aid = "camel-servlet-starter"; + aid = "camel-platform-http-starter"; } String dep = "\n\t\t<dependency>" @@ -385,7 +385,7 @@ abstract class AbstractGenerateMojo extends AbstractMojo { } dep += "\n\t\t</dependency>\n"; - getLog().info("Cannot detect Rest component from classpath. Will use servlet as Rest component."); + getLog().info("Cannot detect Rest component from classpath. Will use platform-http as Rest component."); getLog().info("Add the following dependency in the Maven pom.xml file:\n" + dep + "\n"); } diff --git a/tooling/maven/camel-restdsl-openapi-plugin/src/main/java/org/apache/camel/maven/generator/openapi/GenerateMojo.java b/tooling/maven/camel-restdsl-openapi-plugin/src/main/java/org/apache/camel/maven/generator/openapi/GenerateMojo.java index 8384116d16d..3912c0fa334 100644 --- a/tooling/maven/camel-restdsl-openapi-plugin/src/main/java/org/apache/camel/maven/generator/openapi/GenerateMojo.java +++ b/tooling/maven/camel-restdsl-openapi-plugin/src/main/java/org/apache/camel/maven/generator/openapi/GenerateMojo.java @@ -24,7 +24,6 @@ import io.apicurio.datamodels.models.openapi.OpenApiDocument; import org.apache.camel.generator.openapi.DestinationGenerator; import org.apache.camel.generator.openapi.RestDslGenerator; import org.apache.camel.generator.openapi.RestDslSourceCodeGenerator; -import org.apache.camel.generator.openapi.SpringBootProjectSourceCodeGenerator; import org.apache.camel.util.ObjectHelper; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.LifecyclePhase; @@ -48,9 +47,6 @@ public class GenerateMojo extends AbstractGenerateMojo { @Parameter private String packageName; - @Parameter(defaultValue = "/**", required = true) - private String[] requestMappingValues; - @Override public void execute() throws MojoExecutionException { if (skip) { @@ -85,6 +81,9 @@ public class GenerateMojo extends AbstractGenerateMojo { generator.withIndent(indent.replace("\\t", "\t")); } + if (ObjectHelper.isNotEmpty(basePath)) { + generator.withBasePath(basePath); + } if (ObjectHelper.isNotEmpty(packageName)) { generator.withPackageName(packageName); } @@ -108,38 +107,24 @@ public class GenerateMojo extends AbstractGenerateMojo { if (ObjectHelper.isNotEmpty(apiContextPath)) { generator.withApiContextPath(apiContextPath); } + } - // if its a spring boot project and we use servlet then we should generate additional source code - if (detectSpringBootFromClasspath() && "servlet".equals(comp)) { + if (detectSpringBootFromClasspath()) { + generator.asSpringComponent(); + generator.asSpringBootProject(); + // generate with same package name as spring boot (by default) + if (ObjectHelper.isEmpty(packageName)) { try { - if (ObjectHelper.isEmpty(packageName)) { - // if not explicit package name then try to use package where the spring boot application is located - String pName = detectSpringBootMainPackage(); - if (pName != null) { - packageName = pName; - generator.withPackageName(packageName); - getLog().info( - "Detected @SpringBootApplication, and will be using its package name: " + packageName); - } + String sbPackage = detectSpringBootMainPackage(); + if (sbPackage != null) { + generator.withPackageName(sbPackage); } - getLog().info("Generating Camel Rest Controller source with package name " + packageName - + " in source directory: " + outputPath); - SpringBootProjectSourceCodeGenerator.generator().withPackageName(packageName) - .withMappingValues(requestMappingValues).generate(outputPath); - // the Camel Rest Controller allows to use root as context-path - generator.withRestContextPath("/"); } catch (final IOException e) { - throw new MojoExecutionException( - "Unable to generate Camel Rest Controller source due " + e.getMessage(), e); + // ignore } } } - if (detectSpringBootFromClasspath()) { - generator.asSpringComponent(); - generator.asSpringBootProject(); - } - try { getLog().info("Generating Camel DSL source in directory: " + outputPath); generator.generate(outputPath); diff --git a/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslSourceCodeGenerator.java b/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslSourceCodeGenerator.java index 8983dd10832..84ee4045007 100644 --- a/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslSourceCodeGenerator.java +++ b/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslSourceCodeGenerator.java @@ -103,10 +103,14 @@ public abstract class RestDslSourceCodeGenerator<T> extends RestDslGenerator<Res final MethodBodySourceCodeEmitter emitter = new MethodBodySourceCodeEmitter(configure); - if (restComponent != null) { + boolean restConfig = restComponent != null || restContextPath != null || clientRequestValidation; + if (restConfig) { configure.addCode("\n"); - configure.addCode("restConfiguration().component(\"" + restComponent + "\")"); - if (restContextPath != null) { + configure.addCode("restConfiguration()"); + if (ObjectHelper.isNotEmpty(restComponent)) { + configure.addCode(".component(\"" + restComponent + "\")"); + } + if (ObjectHelper.isNotEmpty(restContextPath)) { configure.addCode(".contextPath(\"" + restContextPath + "\")"); } if (ObjectHelper.isNotEmpty(apiContextPath)) { diff --git a/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslXmlGenerator.java b/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslXmlGenerator.java index bfa5ebe93b7..36f20c2c821 100644 --- a/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslXmlGenerator.java +++ b/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslXmlGenerator.java @@ -85,22 +85,21 @@ public class RestDslXmlGenerator extends RestDslGenerator<RestDslXmlGenerator> { element.removeAttribute("customId"); } - if (restComponent != null) { + boolean restConfig = restComponent != null || restContextPath != null || clientRequestValidation; + if (restConfig) { final Element configuration = document.createElement("restConfiguration"); - configuration.setAttribute("component", restComponent); - - if (restContextPath != null) { + if (ObjectHelper.isNotEmpty(restComponent)) { + configuration.setAttribute("component", restComponent); + } + if (ObjectHelper.isNotEmpty(restContextPath)) { configuration.setAttribute("contextPath", restContextPath); } - if (ObjectHelper.isNotEmpty(apiContextPath)) { configuration.setAttribute("apiContextPath", apiContextPath); } - if (clientRequestValidation) { configuration.setAttribute("clientRequestValidation", "true"); } - root.insertBefore(configuration, root.getFirstChild()); } diff --git a/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslYamlGenerator.java b/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslYamlGenerator.java index f3710ffd80d..f8db777dfed 100644 --- a/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslYamlGenerator.java +++ b/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslYamlGenerator.java @@ -102,22 +102,21 @@ public class RestDslYamlGenerator extends RestDslGenerator<RestDslYamlGenerator> element.removeAttribute("customId"); } - if (restComponent != null) { + boolean restConfig = restComponent != null || restContextPath != null || clientRequestValidation; + if (restConfig) { final Element configuration = document.createElement("restConfiguration"); - configuration.setAttribute("component", restComponent); - - if (restContextPath != null) { + if (ObjectHelper.isNotEmpty(restComponent)) { + configuration.setAttribute("component", restComponent); + } + if (ObjectHelper.isNotEmpty(restContextPath)) { configuration.setAttribute("contextPath", restContextPath); } - if (ObjectHelper.isNotEmpty(apiContextPath)) { configuration.setAttribute("apiContextPath", apiContextPath); } - if (clientRequestValidation) { configuration.setAttribute("clientRequestValidation", "true"); } - root.insertBefore(configuration, root.getFirstChild()); } diff --git a/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/SpringBootProjectSourceCodeGenerator.java b/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/SpringBootProjectSourceCodeGenerator.java deleted file mode 100644 index f16715f45ad..00000000000 --- a/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/SpringBootProjectSourceCodeGenerator.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * 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.camel.generator.openapi; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.Arrays; - -import javax.annotation.processing.Generated; -import javax.lang.model.element.Modifier; - -import com.squareup.javapoet.AnnotationSpec; -import com.squareup.javapoet.ClassName; -import com.squareup.javapoet.JavaFile; -import com.squareup.javapoet.MethodSpec; -import com.squareup.javapoet.TypeSpec; -import org.apache.camel.util.ObjectHelper; -import org.apache.commons.lang3.ArrayUtils; -import org.apache.commons.lang3.StringUtils; - -import static org.apache.camel.util.StringHelper.notEmpty; - -public class SpringBootProjectSourceCodeGenerator { - - private static final String DEFAULT_INDENT = " "; - - private String indent = DEFAULT_INDENT; - - private String packageName; - - private String[] mappingValues; - - public void generate(final Path destination) throws IOException { - final JavaFile javaFile = generateSourceCode(); - - javaFile.writeTo(destination); - } - - public SpringBootProjectSourceCodeGenerator withIndent(final String indent) { - this.indent = ObjectHelper.notNull(indent, "indent"); - return this; - } - - public SpringBootProjectSourceCodeGenerator withPackageName(final String packageName) { - notEmpty(packageName, "packageName"); - this.packageName = packageName; - return this; - } - - public SpringBootProjectSourceCodeGenerator withMappingValues(final String... mappingValues) { - if (!ArrayUtils.isEmpty(mappingValues)) { - this.mappingValues = mappingValues; - } - return this; - } - - String genereateMappingValues() { - if (ArrayUtils.isEmpty(mappingValues)) { - return StringUtils.wrap("/**", '"'); - } - - StringBuilder sb = new StringBuilder("{"); - Arrays.stream(mappingValues) - .forEach(str -> sb.append(sb.toString().endsWith("\"") ? "," : "").append(StringUtils.wrap(str, '"'))); - sb.append("}"); - return sb.toString(); - } - - MethodSpec generateRestMethod() { - final ClassName req = ClassName.bestGuess("jakarta.servlet.http.HttpServletRequest"); - final ClassName res = ClassName.bestGuess("jakarta.servlet.http.HttpServletResponse"); - - final AnnotationSpec.Builder reqAnnotation - = AnnotationSpec.builder(ClassName.bestGuess("org.springframework.web.bind.annotation.RequestMapping")) - .addMember("value", genereateMappingValues()); - - final MethodSpec.Builder forward = MethodSpec.methodBuilder("camelServlet").addModifiers(Modifier.PUBLIC) - .addParameter(req, "request") - .addParameter(res, "response") - .addAnnotation(reqAnnotation.build()) - .returns(void.class); - - forward.addCode("try {\n"); - forward.addCode(" String path = request.getRequestURI();\n"); - forward.addCode(" String camelPrefix = (path != null && path.startsWith(\"/\")) ? \"/camel\" : \"/camel/\";\n"); - forward.addCode( - " request.getServletContext().getRequestDispatcher(camelPrefix + path).forward(request, response);\n"); - forward.addCode("} catch (Exception e) {\n"); - forward.addCode(" response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);\n"); - forward.addCode("}\n"); - - return forward.build(); - } - - JavaFile generateSourceCode() { - notEmpty(packageName, "packageName"); - - final MethodSpec methodSpec = generateRestMethod(); - - final String classNameToUse = "CamelRestController"; - - final AnnotationSpec.Builder generatedAnnotation = AnnotationSpec.builder(Generated.class).addMember("value", - "$S", getClass().getName()); - final AnnotationSpec.Builder restAnnotation - = AnnotationSpec.builder(ClassName.bestGuess("org.springframework.web.bind.annotation.RestController")); - - final TypeSpec.Builder builder = TypeSpec.classBuilder(classNameToUse) - .addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(methodSpec) - .addAnnotation(generatedAnnotation.build()) - .addAnnotation(restAnnotation.build()) - .addJavadoc("Forward requests to the Camel servlet so it can service REST requests.\n"); - final TypeSpec generatedRestController = builder.build(); - - return JavaFile.builder(packageName, generatedRestController).indent(indent).build(); - } - - public static SpringBootProjectSourceCodeGenerator generator() { - return new SpringBootProjectSourceCodeGenerator(); - } - -} diff --git a/tooling/openapi-rest-dsl-generator/src/test/java/org/apache/camel/generator/openapi/PathSpringBootProjectSourceGeneratorTest.java b/tooling/openapi-rest-dsl-generator/src/test/java/org/apache/camel/generator/openapi/PathSpringBootProjectSourceGeneratorTest.java deleted file mode 100644 index f8296fb5109..00000000000 --- a/tooling/openapi-rest-dsl-generator/src/test/java/org/apache/camel/generator/openapi/PathSpringBootProjectSourceGeneratorTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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.camel.generator.openapi; - -import java.io.File; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -public class PathSpringBootProjectSourceGeneratorTest { - - @Test - public void shouldGenerateSourceCodeWithDefaults() throws IOException, URISyntaxException { - final Path path = new File("target/generated-sources").toPath(); - SpringBootProjectSourceCodeGenerator.generator().withPackageName("com.foo").generate(path); - final String generatedContent = new String( - Files.readAllBytes(Paths.get("target/generated-sources/com/foo/CamelRestController.java")), - StandardCharsets.UTF_8); - - final URI file = PathSpringBootProjectSourceGeneratorTest.class.getResource("/SpringBootRestController.txt").toURI(); - final String expectedContent = new String(Files.readAllBytes(Paths.get(file)), StandardCharsets.UTF_8); - - assertThat(generatedContent).isEqualTo(expectedContent); - } - -} diff --git a/tooling/openapi-rest-dsl-generator/src/test/resources/SpringBootRestController.txt b/tooling/openapi-rest-dsl-generator/src/test/resources/SpringBootRestController.txt deleted file mode 100644 index cefdbaaf4da..00000000000 --- a/tooling/openapi-rest-dsl-generator/src/test/resources/SpringBootRestController.txt +++ /dev/null @@ -1,25 +0,0 @@ -package com.foo; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import javax.annotation.processing.Generated; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -/** - * Forward requests to the Camel servlet so it can service REST requests. - */ -@Generated("org.apache.camel.generator.openapi.SpringBootProjectSourceCodeGenerator") -@RestController -public final class CamelRestController { - @RequestMapping("/**") - public void camelServlet(HttpServletRequest request, HttpServletResponse response) { - try { - String path = request.getRequestURI(); - String camelPrefix = (path != null && path.startsWith("/")) ? "/camel" : "/camel/"; - request.getServletContext().getRequestDispatcher(camelPrefix + path).forward(request, response); - } catch (Exception e) { - response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } - } -}