You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2018/06/18 08:31:22 UTC

[camel] 04/04: CAMEL-9751: Allow to configure swagger security requirements in generated swagger api docs in rest-dsl.

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

commit 14eea890c1f5ddbffc40f8151c389f1f69af5149
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Jun 17 10:52:05 2018 +0200

    CAMEL-9751: Allow to configure swagger security requirements in generated swagger api docs in rest-dsl.
---
 .../camel/model/rest/RestSecurityApiKey.java       |  8 +--
 .../camel/model/rest/SecurityDefinition.java       |  6 +-
 .../apache/camel/component/rest/RestRefTest.xml    |  1 -
 components/camel-swagger-java/pom.xml              |  2 +-
 .../apache/camel/swagger/RestSwaggerReader.java    | 10 +++-
 .../RestSwaggerReaderModelApiSecurityTest.java     |  1 +
 ...ringRestSwaggerReaderModelApiSecurityTest.java} | 51 +++--------------
 ...SpringRestSwaggerReaderModelApiSecurityTest.xml | 65 ++++++++++++++++++++++
 8 files changed, 90 insertions(+), 54 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityApiKey.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityApiKey.java
index 3e48330..254150c 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityApiKey.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestSecurityApiKey.java
@@ -40,10 +40,6 @@ public class RestSecurityApiKey extends RestSecurityDefinition {
     @XmlAttribute(name = "inQuery")
     private Boolean inQuery;
 
-    public String getName() {
-        return name;
-    }
-
     public RestSecurityApiKey() {
     }
 
@@ -51,6 +47,10 @@ public class RestSecurityApiKey extends RestSecurityDefinition {
         super(rest);
     }
 
+    public String getName() {
+        return name;
+    }
+
     /**
      * The name of the header or query parameter to be used.
      */
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/SecurityDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/SecurityDefinition.java
index 52ea20d..0c7b056 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/SecurityDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/SecurityDefinition.java
@@ -5,9 +5,9 @@
  * 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
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
+ *
+ *      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.
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/rest/RestRefTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/component/rest/RestRefTest.xml
index 732c73f..c53d943 100644
--- a/components/camel-spring/src/test/resources/org/apache/camel/component/rest/RestRefTest.xml
+++ b/components/camel-spring/src/test/resources/org/apache/camel/component/rest/RestRefTest.xml
@@ -31,7 +31,6 @@
     <rest path="/say/hello">
       <get>
         <to uri="direct:hello"/>
-        <security key="ausss" scopes="write:pets,read:pets"/>
       </get>
     </rest>
   </restContext>
diff --git a/components/camel-swagger-java/pom.xml b/components/camel-swagger-java/pom.xml
index 98454f5..d83f307 100644
--- a/components/camel-swagger-java/pom.xml
+++ b/components/camel-swagger-java/pom.xml
@@ -131,7 +131,7 @@
     <!-- testing -->
     <dependency>
       <groupId>org.apache.camel</groupId>
-      <artifactId>camel-test</artifactId>
+      <artifactId>camel-test-spring</artifactId>
       <scope>test</scope>
     </dependency>
     <dependency>
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
index 128ea21..cd3d58c 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
@@ -158,7 +158,15 @@ public class RestSwaggerReader {
                     RestSecurityOAuth2 rs = (RestSecurityOAuth2) def;
                     OAuth2Definition auth = new OAuth2Definition();
                     auth.setDescription(rs.getDescription());
-                    auth.setFlow(rs.getFlow());
+                    String flow = rs.getFlow();
+                    if (flow == null) {
+                        if (rs.getAuthorizationUrl() != null && rs.getTokenUrl() != null) {
+                            flow = "accessCode";
+                        } else if (rs.getTokenUrl() == null && rs.getAuthorizationUrl() != null) {
+                            flow = "implicit";
+                        }
+                    }
+                    auth.setFlow(flow);
                     auth.setAuthorizationUrl(rs.getAuthorizationUrl());
                     auth.setTokenUrl(rs.getTokenUrl());
                     for (RestPropertyDefinition scope : rs.getScopes()) {
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java
index c8708d2..9b4729b 100644
--- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java
@@ -95,6 +95,7 @@ public class RestSwaggerReaderModelApiSecurityTest extends CamelTestSupport {
         assertTrue(json.contains("\"securityDefinitions\" : {"));
         assertTrue(json.contains("\"type\" : \"oauth2\","));
         assertTrue(json.contains("\"authorizationUrl\" : \"http://petstore.swagger.io/oauth/dialog\","));
+        assertTrue(json.contains("\"flow\" : \"implicit\""));
         assertTrue(json.contains("\"type\" : \"apiKey\","));
         assertTrue(json.contains("\"in\" : \"header\""));
         assertTrue(json.contains("\"host\" : \"localhost:8080\""));
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/SpringRestSwaggerReaderModelApiSecurityTest.java
similarity index 56%
copy from components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java
copy to components/camel-swagger-java/src/test/java/org/apache/camel/swagger/SpringRestSwaggerReaderModelApiSecurityTest.java
index c8708d2..808ac07 100644
--- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/SpringRestSwaggerReaderModelApiSecurityTest.java
@@ -21,54 +21,17 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
 import io.swagger.jaxrs.config.BeanConfig;
 import io.swagger.models.Swagger;
-import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.DefaultClassResolver;
-import org.apache.camel.impl.JndiRegistry;
-import org.apache.camel.model.rest.RestParamType;
-import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.camel.test.spring.CamelSpringTestSupport;
 import org.junit.Test;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
 
-public class RestSwaggerReaderModelApiSecurityTest extends CamelTestSupport {
+public class SpringRestSwaggerReaderModelApiSecurityTest extends CamelSpringTestSupport {
 
     @Override
-    protected JndiRegistry createRegistry() throws Exception {
-        JndiRegistry jndi = super.createRegistry();
-        jndi.bind("dummy-rest", new DummyRestConsumerFactory());
-        return jndi;
-    }
-
-    @Override
-    protected RouteBuilder createRouteBuilder() throws Exception {
-        return new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                rest("/user").tag("dude").description("User rest service")
-                    // setup security definitions
-                    .securityDefinitions()
-                        .oauth2("petstore_auth").authorizationUrl("http://petstore.swagger.io/oauth/dialog").end()
-                        .apiKey("api_key").withHeader("myHeader").end()
-                    .end()
-                    .consumes("application/json").produces("application/json")
-
-                    .get("/{id}/{date}").description("Find user by id and date").outType(User.class)
-                        .responseMessage().message("The user returned").endResponseMessage()
-                        // setup security for this rest verb
-                        .security("api_key")
-                        .param().name("id").type(RestParamType.path).description("The id of the user to get").endParam()
-                        .param().name("date").type(RestParamType.path).description("The date").dataFormat("date").endParam()
-                        .to("bean:userService?method=getUser(${header.id})")
-
-                    .put().description("Updates or create a user").type(User.class)
-                        // setup security for this rest verb
-                        .security("petstore_auth", "write:pets,read:pets")
-                        .param().name("body").type(RestParamType.body).description("The user to update or create").endParam()
-                        .to("bean:userService?method=updateUser")
-
-                    .get("/findAll").description("Find all users").outTypeList(User.class)
-                        .responseMessage().message("All the found users").endResponseMessage()
-                        .to("bean:userService?method=listUsers");
-            }
-        };
+    protected AbstractApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/swagger/SpringRestSwaggerReaderModelApiSecurityTest.xml");
     }
 
     @Test
@@ -95,6 +58,7 @@ public class RestSwaggerReaderModelApiSecurityTest extends CamelTestSupport {
         assertTrue(json.contains("\"securityDefinitions\" : {"));
         assertTrue(json.contains("\"type\" : \"oauth2\","));
         assertTrue(json.contains("\"authorizationUrl\" : \"http://petstore.swagger.io/oauth/dialog\","));
+        assertTrue(json.contains("\"flow\" : \"implicit\""));
         assertTrue(json.contains("\"type\" : \"apiKey\","));
         assertTrue(json.contains("\"in\" : \"header\""));
         assertTrue(json.contains("\"host\" : \"localhost:8080\""));
@@ -110,5 +74,4 @@ public class RestSwaggerReaderModelApiSecurityTest extends CamelTestSupport {
         assertFalse(json.contains("\"enum\""));
         context.stop();
     }
-
 }
diff --git a/components/camel-swagger-java/src/test/resources/org/apache/camel/swagger/SpringRestSwaggerReaderModelApiSecurityTest.xml b/components/camel-swagger-java/src/test/resources/org/apache/camel/swagger/SpringRestSwaggerReaderModelApiSecurityTest.xml
new file mode 100644
index 0000000..9fc8609
--- /dev/null
+++ b/components/camel-swagger-java/src/test/resources/org/apache/camel/swagger/SpringRestSwaggerReaderModelApiSecurityTest.xml
@@ -0,0 +1,65 @@
+<?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.
+
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+  <!-- use a dummy rest consumer factory for the rest engine -->
+  <bean id="dummy-rest" class="org.apache.camel.swagger.DummyRestConsumerFactory"/>
+
+  <camelContext xmlns="http://camel.apache.org/schema/spring">
+
+    <rest path="/user" tag="dude" consumes="application/json" produces="application/json">
+      <description>User rest service</description>
+      <securityDefinitions>
+        <oauth2 key="petstore_auth" authorizationUrl="http://petstore.swagger.io/oauth/dialog"/>
+        <apiKey key="api_key" name="myHeader" inHeader="true"/>
+      </securityDefinitions>
+
+      <get uri="/{id}/{date}" outType="org.apache.camel.swagger.User">
+        <description>Find user by id and date</description>
+        <param name="id" type="path" description="The id of the user to get"/>
+        <param name="date" type="path" dataFormat="date" description="The date"/>
+        <responseMessage message="The user returned"/>
+        <security key="api_key"/>
+        <to uri="bean:userService?method=getUser(${header.id})"/>
+      </get>
+
+      <put type="org.apache.camel.swagger.User">
+        <description>Updates or create a user</description>
+        <param name="body" type="body" description="The user to update or create"/>
+        <security key="petstore_auth" scopes="write:pets,read:pets"/>
+        <to uri="bean:userService?method=updateUser"/>
+      </put>
+
+      <get uri="/findAll" outType="org.apache.camel.swagger.User[]">
+        <description>Find all users</description>
+        <responseMessage message="All the found users"/>
+        <to uri="bean:userService?method=listUsers"/>
+      </get>
+
+    </rest>
+
+  </camelContext>
+
+</beans>

-- 
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.