[camel] branch master updated: Added clientRequestValidation parameter (#4869)

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

[camel] branch master updated: Added clientRequestValidation parameter (#4869)

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

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new c70abb2  Added clientRequestValidation parameter (#4869)
c70abb2 is described below

commit c70abb2b7af33652246a6ab2430c41865cb7cf2b
Author: javaduke <[hidden email]>
AuthorDate: Wed Jan 13 01:42:03 2021 -0700

    Added clientRequestValidation parameter (#4869)
---
 .../src/it/client-validation/pom.xml               | 93 ++++++++++++++++++++++
 .../java/com/example/MyDestinationGenerator.java   | 29 +++++++
 .../src/main/resources/petstore.json               |  1 +
 .../src/it/client-validation/verify.groovy         | 24 ++++++
 .../main/docs/camel-restdsl-swagger-plugin.adoc    |  4 +-
 .../generator/swagger/AbstractGenerateMojo.java    |  3 +
 .../maven/generator/swagger/GenerateMojo.java      |  4 +
 .../maven/generator/swagger/GenerateXmlMojo.java   |  3 +
 .../camel/generator/swagger/RestDslGenerator.java  | 10 +++
 .../swagger/RestDslSourceCodeGenerator.java        |  3 +
 .../generator/swagger/RestDslXmlGenerator.java     |  4 +
 11 files changed, 177 insertions(+), 1 deletion(-)

diff --git a/tooling/maven/camel-restdsl-swagger-plugin/src/it/client-validation/pom.xml b/tooling/maven/camel-restdsl-swagger-plugin/src/it/client-validation/pom.xml
new file mode 100644
index 0000000..76c5fb7
--- /dev/null
+++ b/tooling/maven/camel-restdsl-swagger-plugin/src/it/client-validation/pom.xml
@@ -0,0 +1,93 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.camel.maven.generator.swagger.it</groupId>
+  <artifactId>client-validation</artifactId>
+  <version>1-SNAPSHOT</version>
+
+  <description>A simple IT verifying the basic use case.</description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <maven.compiler.source>1.8</maven.compiler.source>
+    <maven.compiler.target>1.8</maven.compiler.target>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>@project.groupId@</groupId>
+        <artifactId>@project.artifactId@</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>client-validation</id>
+            <goals>
+              <goal>generate</goal>
+            </goals>
+            <phase>process-classes</phase>
+            <configuration>
+              <specificationUri>${project.basedir}/src/main/resources/petstore.json</specificationUri>
+              <outputDirectory>${project.build.outputDirectory}/generated</outputDirectory>
+              <packageName>com.example</packageName>
+              <className>MyRestRoute</className>
+              <indent>\t</indent>
+              <destinationGenerator>com.example.MyDestinationGenerator</destinationGenerator>
+              <apiContextPath>/api-docs</apiContextPath>
+              <restConfiguration>true</restConfiguration>
+              <clientRequestValidation>true</clientRequestValidation>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-swagger-rest-dsl-generator</artifactId>
+      <version>@project.version@</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-core</artifactId>
+      <version>@project.version@</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-swagger-java</artifactId>
+      <version>@project.version@</version>
+    </dependency>
+    <dependency>
+      <groupId>com.google.code.gson</groupId>
+      <artifactId>gson</artifactId>
+      <version>2.8.5</version>
+    </dependency>
+    <dependency>
+      <groupId>org.threeten</groupId>
+      <artifactId>threetenbp</artifactId>
+      <version>1.3.7</version>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/tooling/maven/camel-restdsl-swagger-plugin/src/it/client-validation/src/main/java/com/example/MyDestinationGenerator.java b/tooling/maven/camel-restdsl-swagger-plugin/src/it/client-validation/src/main/java/com/example/MyDestinationGenerator.java
new file mode 100644
index 0000000..b9713d6
--- /dev/null
+++ b/tooling/maven/camel-restdsl-swagger-plugin/src/it/client-validation/src/main/java/com/example/MyDestinationGenerator.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 com.example;
+
+import io.swagger.models.Operation;
+import org.apache.camel.generator.swagger.DestinationGenerator;
+
+public class MyDestinationGenerator implements DestinationGenerator {
+
+    @Override
+    public String generateDestinationFor(final Operation operation) {
+        return "rest-" + operation.getOperationId();
+    }
+
+}
diff --git a/tooling/maven/camel-restdsl-swagger-plugin/src/it/client-validation/src/main/resources/petstore.json b/tooling/maven/camel-restdsl-swagger-plugin/src/it/client-validation/src/main/resources/petstore.json
new file mode 100644
index 0000000..816847f
--- /dev/null
+++ b/tooling/maven/camel-restdsl-swagger-plugin/src/it/client-validation/src/main/resources/petstore.json
@@ -0,0 +1 @@
+{"swagger":"2.0","info":{"description":"This is a sample server Petstore server.  You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).  For this sample, you can use the api key `special-key` to test the authorization filters.","version":"1.0.0","title":"Swagger Petstore","termsOfService":"http://swagger.io/terms/","contact":{"email":"[hidden email]"},"license":{"name":"Apache 2.0","url":"http://www.a [...]
\ No newline at end of file
diff --git a/tooling/maven/camel-restdsl-swagger-plugin/src/it/client-validation/verify.groovy b/tooling/maven/camel-restdsl-swagger-plugin/src/it/client-validation/verify.groovy
new file mode 100644
index 0000000..200ca66
--- /dev/null
+++ b/tooling/maven/camel-restdsl-swagger-plugin/src/it/client-validation/verify.groovy
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+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").clientRequestValidation(true);')
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 356c0ba..0fec22c 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.
+| `clientRequestValidation` | `false` | Whether to enable request validation.
 | `apiContextPath` | | Define swagger endpoint path if `restConfiguration` is set to `true`. |
 |========================================
 
@@ -140,6 +141,7 @@ in the `<configuration>` tag.
 | `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.
+| `clientRequestValidation` | `false` | Whether to enable request validation.
 | `apiContextPath` | | Define swagger endpoint path if `restConfiguration` is set to `true`. |
 |========================================
 
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 1591a61..a4fec42 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
@@ -89,6 +89,9 @@ abstract class AbstractGenerateMojo extends AbstractMojo {
     boolean restConfiguration;
 
     @Parameter(defaultValue = "false")
+    boolean clientRequestValidation;
+
+    @Parameter(defaultValue = "false")
     boolean skip;
 
     @Parameter(defaultValue = "${project.basedir}/src/spec/swagger.json", required = true)
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 60ea0c1..4e1c531 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
@@ -100,6 +100,10 @@ public class GenerateMojo extends AbstractGenerateMojo {
                 generator.withApiContextPath(apiContextPath);
             }
 
+            if (clientRequestValidation) {
+                generator.withClientRequestValidation();
+            }
+
             // if its a spring boot project and we use servlet then we should generate additional source code
             if (detectSpringBootFromClasspath() && "servlet".equals(comp)) {
                 try {
diff --git a/tooling/maven/camel-restdsl-swagger-plugin/src/main/java/org/apache/camel/maven/generator/swagger/GenerateXmlMojo.java b/tooling/maven/camel-restdsl-swagger-plugin/src/main/java/org/apache/camel/maven/generator/swagger/GenerateXmlMojo.java
index d989cf3..babcbb9 100644
--- a/tooling/maven/camel-restdsl-swagger-plugin/src/main/java/org/apache/camel/maven/generator/swagger/GenerateXmlMojo.java
+++ b/tooling/maven/camel-restdsl-swagger-plugin/src/main/java/org/apache/camel/maven/generator/swagger/GenerateXmlMojo.java
@@ -81,6 +81,9 @@ public class GenerateXmlMojo extends AbstractGenerateMojo {
 
         if (restConfiguration) {
             generator.withRestComponent(findAppropriateComponent());
+            if (clientRequestValidation) {
+                generator.withClientRequestValidation();
+            }
         }
 
         try {
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 894baf7..fca6514 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
@@ -38,6 +38,7 @@ public abstract class RestDslGenerator<G> {
     String restComponent;
     String restContextPath;
     String apiContextPath;
+    boolean clientRequestValidation;
     boolean springComponent;
     boolean springBootProject;
 
@@ -104,6 +105,15 @@ public abstract class RestDslGenerator<G> {
         return that;
     }
 
+    public G withClientRequestValidation() {
+        this.clientRequestValidation = true;
+
+        @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 03d59fb..1fa0d5f 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
@@ -109,6 +109,9 @@ public abstract class RestDslSourceCodeGenerator<T> extends RestDslGenerator<Res
             if (ObjectHelper.isNotEmpty(apiContextPath)) {
                 configure.addCode(".apiContextPath(\"" + apiContextPath + "\")");
             }
+            if (clientRequestValidation == true) {
+                configure.addCode(".clientRequestValidation(true)");
+            }
             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 ecfe25f..bc57929 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
@@ -94,6 +94,10 @@ public class RestDslXmlGenerator extends RestDslGenerator<RestDslXmlGenerator> {
                 configuration.setAttribute("apiContextPath", apiContextPath);
             }
 
+            if (clientRequestValidation == true) {
+                configuration.setAttribute("clientRequestValidation", "true");
+            }
+
             root.insertBefore(configuration, root.getFirstChild());
         }