[camel] branch sandbox/camel-3.x updated: CAMEL-12865: camel-restdsl-swagger-plugin - Allow for specifying apiContextPath

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch sandbox/camel-3.x updated: CAMEL-12865: camel-restdsl-swagger-plugin - Allow for specifying apiContextPath

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

acosentino pushed a commit to branch sandbox/camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/sandbox/camel-3.x by this push:
     new f53a549  CAMEL-12865: camel-restdsl-swagger-plugin - Allow for specifying apiContextPath
f53a549 is described below

commit f53a549128a0ff12d229aa8b6fb751b07134416f
Author: Dmitry Volodin <[hidden email]>
AuthorDate: Thu Nov 15 20:30:10 2018 +0300

    CAMEL-12865: camel-restdsl-swagger-plugin - Allow for specifying
    apiContextPath
---
 .../camel-restdsl-swagger-plugin/src/it/customized/pom.xml     |  1 +
 .../src/it/customized/verify.groovy                            |  4 ++++
 .../src/main/docs/camel-restdsl-swagger-plugin.adoc            |  6 ++++--
 .../camel/maven/generator/swagger/AbstractGenerateMojo.java    |  3 +++
 .../org/apache/camel/maven/generator/swagger/GenerateMojo.java |  4 ++++
 .../org/apache/camel/generator/swagger/RestDslGenerator.java   | 10 ++++++++++
 .../camel/generator/swagger/RestDslSourceCodeGenerator.java    |  3 +++
 .../apache/camel/generator/swagger/RestDslXmlGenerator.java    |  9 +++++++--
 8 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/tooling/maven/camel-restdsl-swagger-plugin/src/it/customized/pom.xml b/tooling/maven/camel-restdsl-swagger-plugin/src/it/customized/pom.xml
index b91faba..3aaad86 100644
--- a/tooling/maven/camel-restdsl-swagger-plugin/src/it/customized/pom.xml
+++ b/tooling/maven/camel-restdsl-swagger-plugin/src/it/customized/pom.xml
@@ -53,6 +53,7 @@
               <className>MyRestRoute</className>
               <indent>\t</indent>
               <destinationGenerator>com.example.MyDestinationGenerator</destinationGenerator>
+              <apiContextPath>/api-docs</apiContextPath>
             </configuration>
           </execution>
         </executions>
diff --git a/tooling/maven/camel-restdsl-swagger-plugin/src/it/customized/verify.groovy b/tooling/maven/camel-restdsl-swagger-plugin/src/it/customized/verify.groovy
index f65964b..3c154ed 100644
--- a/tooling/maven/camel-restdsl-swagger-plugin/src/it/customized/verify.groovy
+++ b/tooling/maven/camel-restdsl-swagger-plugin/src/it/customized/verify.groovy
@@ -18,3 +18,7 @@
 def File restdsl = new File(basedir, "target/classes/generated/com/example/MyRestRoute.java")
 
 assert restdsl.exists()
+
+def String data = restdsl.text
+
+assert data.contains('restConfiguration().component("servlet").apiContextPath("/api-docs");')
diff --git a/tooling/maven/camel-restdsl-swagger-plugin/src/main/docs/camel-restdsl-swagger-plugin.adoc b/tooling/maven/camel-restdsl-swagger-plugin/src/main/docs/camel-restdsl-swagger-plugin.adoc
index 58066de..356c0ba 100644
--- a/tooling/maven/camel-restdsl-swagger-plugin/src/main/docs/camel-restdsl-swagger-plugin.adoc
+++ b/tooling/maven/camel-restdsl-swagger-plugin/src/main/docs/camel-restdsl-swagger-plugin.adoc
@@ -66,7 +66,8 @@ in the `<configuration>` tag.
 | `indent` | `"&nbsp;&nbsp;&nbsp;&nbsp;"` | What identing character(s) to use, by default four spaces, you can use `\t` to signify tab character
 | `outputDirectory` | `generated-sources/restdsl-swagger` | Where to place the generated source file, by default `generated-sources/restdsl-swagger` within the project directory
 | `destinationGenerator` | | Fully qualified class name of the class that implements `org.apache.camel.generator.swagger.DestinationGenerator` interface for customizing destination endpoint
-| `restConfiguration` | `true` | Whether to include generation of the rest configuration with detected rest component to be used. |
+| `restConfiguration` | `true` | Whether to include generation of the rest configuration with detected rest component to be used.
+| `apiContextPath` | | Define swagger endpoint path if `restConfiguration` is set to `true`. |
 |========================================
 
 === Spring Boot Project with Servlet component
@@ -138,7 +139,8 @@ in the `<configuration>` tag.
 | `fileName` | `camel-rest.xml` | The name of the XML file as output.
 | `blueprint` | `false` | If enabled generates OSGi Blueprint XML instead of Spring XML.
 | `destinationGenerator` | | Fully qualified class name of the class that implements `org.apache.camel.generator.swagger.DestinationGenerator` interface for customizing destination endpoint
-| `restConfiguration` | `true` | Whether to include generation of the rest configuration with detected rest component to be used. |
+| `restConfiguration` | `true` | Whether to include generation of the rest configuration with detected rest component to be used.
+| `apiContextPath` | | Define swagger endpoint path if `restConfiguration` is set to `true`. |
 |========================================
 
 == camel-restdsl-swagger:generate-xml-with-dto
diff --git a/tooling/maven/camel-restdsl-swagger-plugin/src/main/java/org/apache/camel/maven/generator/swagger/AbstractGenerateMojo.java b/tooling/maven/camel-restdsl-swagger-plugin/src/main/java/org/apache/camel/maven/generator/swagger/AbstractGenerateMojo.java
index 8dbbde7..7d1903d 100644
--- a/tooling/maven/camel-restdsl-swagger-plugin/src/main/java/org/apache/camel/maven/generator/swagger/AbstractGenerateMojo.java
+++ b/tooling/maven/camel-restdsl-swagger-plugin/src/main/java/org/apache/camel/maven/generator/swagger/AbstractGenerateMojo.java
@@ -97,6 +97,9 @@ abstract class AbstractGenerateMojo extends AbstractMojo {
 
     @Parameter(defaultValue = "${session}", readonly = true)
     private MavenSession mavenSession;
+    
+    @Parameter
+    String apiContextPath;
 
     @Component
     private BuildPluginManager pluginManager;
diff --git a/tooling/maven/camel-restdsl-swagger-plugin/src/main/java/org/apache/camel/maven/generator/swagger/GenerateMojo.java b/tooling/maven/camel-restdsl-swagger-plugin/src/main/java/org/apache/camel/maven/generator/swagger/GenerateMojo.java
index 41aaa94..74b3702 100644
--- a/tooling/maven/camel-restdsl-swagger-plugin/src/main/java/org/apache/camel/maven/generator/swagger/GenerateMojo.java
+++ b/tooling/maven/camel-restdsl-swagger-plugin/src/main/java/org/apache/camel/maven/generator/swagger/GenerateMojo.java
@@ -118,6 +118,10 @@ public class GenerateMojo extends AbstractGenerateMojo {
 
                 generator.withRestComponent("servlet");
             }
+            
+            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)) {
diff --git a/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslGenerator.java b/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslGenerator.java
index a96f2aa..a896962 100644
--- a/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslGenerator.java
+++ b/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslGenerator.java
@@ -36,6 +36,7 @@ public abstract class RestDslGenerator<G> {
     OperationFilter filter = new OperationFilter();
     String restComponent;
     String restContextPath;
+    String apiContextPath;
     boolean springComponent;
     boolean springBootProject;
 
@@ -92,6 +93,15 @@ public abstract class RestDslGenerator<G> {
 
         return that;
     }
+    
+    public G withApiContextPath(String contextPath) {
+        this.apiContextPath = contextPath;
+
+        @SuppressWarnings("unchecked")
+        final G that = (G) this;
+
+        return that;
+    }
 
     public G asSpringComponent() {
         this.springComponent = true;
diff --git a/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslSourceCodeGenerator.java b/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslSourceCodeGenerator.java
index a2fe715..7ec8922 100644
--- a/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslSourceCodeGenerator.java
+++ b/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslSourceCodeGenerator.java
@@ -108,6 +108,9 @@ public abstract class RestDslSourceCodeGenerator<T> extends RestDslGenerator<Res
             if (restContextPath != null) {
                 configure.addCode(".contextPath(\"" + restContextPath + "\")");
             }
+            if (ObjectHelper.isNotEmpty(apiContextPath)) {
+                configure.addCode(".apiContextPath(\"" + apiContextPath + "\")");
+            }
             configure.addCode(";\n\n");
         }
 
diff --git a/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslXmlGenerator.java b/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslXmlGenerator.java
index 20b8490..36034d5 100644
--- a/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslXmlGenerator.java
+++ b/tooling/swagger-rest-dsl-generator/src/main/java/org/apache/camel/generator/swagger/RestDslXmlGenerator.java
@@ -20,6 +20,7 @@ import io.swagger.models.Swagger;
 import org.apache.camel.CamelContext;
 import org.apache.camel.model.ModelHelper;
 import org.apache.camel.model.rest.RestsDefinition;
+import org.apache.camel.util.ObjectHelper;
 
 public class RestDslXmlGenerator extends RestDslGenerator<RestDslXmlGenerator> {
 
@@ -51,10 +52,14 @@ public class RestDslXmlGenerator extends RestDslGenerator<RestDslXmlGenerator> {
         xml = xml.replaceAll(" customId=\"false\"", "");
 
         if (restComponent != null) {
-            String extra = "<restConfiguration component=\"" + restComponent + "\"/>";
+            String extra = "<restConfiguration component=\"" + restComponent + "\"";
             if (restContextPath != null) {
-                extra = "<restConfiguration component=\"" + restComponent + "\" contextPath=\"" + restContextPath + "\"/>";
+                extra = extra.concat(" contextPath=\"" + restContextPath + "\"");
             }
+            if (ObjectHelper.isNotEmpty(apiContextPath)) {
+                extra = extra.concat(" apiContextPath=\"" + apiContextPath + "\"");
+            }
+            extra = extra.concat("/>");
             xml = xml.replaceFirst("<rest>", extra + "\n    <rest>");
             xml = xml.replaceFirst("<rest ", extra + "\n    <rest ");
         }