camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [02/21] camel git commit: CAMEL-7800: camel-swagger-java - work in progress
Date Fri, 18 Sep 2015 15:43:48 GMT
CAMEL-7800: camel-swagger-java - work in progress


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e5c2ffb0
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e5c2ffb0
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e5c2ffb0

Branch: refs/heads/master
Commit: e5c2ffb046b123597d227e72b0316a90868bbf8a
Parents: 1408333
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Thu Sep 17 15:51:59 2015 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Fri Sep 18 16:16:11 2015 +0200

----------------------------------------------------------------------
 .../apache/camel/swagger/RestSwaggerReader.java | 40 +++++++++++++++++---
 .../camel/swagger/RestSwaggerReaderTest.java    | 29 +++++++++-----
 .../src/test/resources/log4j.properties         |  6 +--
 3 files changed, 57 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e5c2ffb0/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java
----------------------------------------------------------------------
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 4a896a3..de2de46 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
@@ -22,17 +22,26 @@ import java.util.Comparator;
 import java.util.List;
 import java.util.Locale;
 
-import io.swagger.config.SwaggerConfig;
+import io.swagger.jaxrs.config.BeanConfig;
 import io.swagger.models.Operation;
 import io.swagger.models.Path;
 import io.swagger.models.Swagger;
+import io.swagger.models.parameters.BodyParameter;
+import io.swagger.models.parameters.FormParameter;
+import io.swagger.models.parameters.HeaderParameter;
+import io.swagger.models.parameters.Parameter;
+import io.swagger.models.parameters.PathParameter;
+import io.swagger.models.parameters.QueryParameter;
 import org.apache.camel.model.rest.RestDefinition;
+import org.apache.camel.model.rest.RestOperationParamDefinition;
+import org.apache.camel.model.rest.RestParamType;
 import org.apache.camel.model.rest.VerbDefinition;
 
 public class RestSwaggerReader {
 
-    public Swagger read(RestDefinition rest) {
+    public Swagger read(RestDefinition rest, BeanConfig config) {
         Swagger swagger = new Swagger();
+        config.configure(swagger);
 
         List<VerbDefinition> verbs = new ArrayList<>(rest.getVerbs());
         // must sort the verbs by uri so we group them together when an uri has multiple
operations
@@ -45,12 +54,9 @@ public class RestSwaggerReader {
 
         for (VerbDefinition verb : verbs) {
 
-            // TODO: should be base path and a lot more
-
-
             // the method must be in lower case
             String method = verb.asVerb().toLowerCase(Locale.US);
-
+            // operation path is a key
             String opPath = getPath(basePath, verb.getUri());
 
             Operation op = new Operation();
@@ -71,6 +77,28 @@ public class RestSwaggerReader {
             if (verb.getDescriptionText() != null) {
                 op.summary(verb.getDescriptionText());
             }
+            for (RestOperationParamDefinition param : verb.getParams()) {
+                Parameter parameter = null;
+                if (param.getType().equals(RestParamType.body)) {
+                    parameter = new BodyParameter();
+                } else if (param.getType().equals(RestParamType.form)) {
+                    parameter = new FormParameter();
+                } else if (param.getType().equals(RestParamType.header)) {
+                    parameter = new HeaderParameter();
+                } else if (param.getType().equals(RestParamType.path)) {
+                    parameter = new PathParameter();
+                } else if (param.getType().equals(RestParamType.query)) {
+                    parameter = new QueryParameter();
+                }
+
+                if (parameter != null) {
+                    parameter.setName(param.getName());
+                    parameter.setAccess(param.getAccess());
+                    parameter.setDescription(param.getDescription());
+                    parameter.setRequired(param.getRequired());
+                    op.addParameter(parameter);
+                }
+            }
 
             // add path
             swagger.path(opPath, path);

http://git-wip-us.apache.org/repos/asf/camel/blob/e5c2ffb0/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderTest.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderTest.java
b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderTest.java
index 2252479..6ee65ea 100644
--- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderTest.java
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderTest.java
@@ -24,6 +24,7 @@ import io.swagger.models.Swagger;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.JndiRegistry;
 import org.apache.camel.model.rest.RestDefinition;
+import org.apache.camel.model.rest.RestParamType;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Test;
 
@@ -42,9 +43,15 @@ public class RestSwaggerReaderTest extends CamelTestSupport {
             @Override
             public void configure() throws Exception {
                 rest("/hello")
-                        .get("/hi").to("log:hi")
-                        .get("/bye").to("log:bye")
-                        .post("/bye").to("log:bye");
+                        .get("/hi/{name}").description("Saying hi")
+                            .param().name("name").type(RestParamType.path).dataType("string").description("Who
is it").endParam()
+                            .to("log:hi")
+                        .get("/bye/{name}").description("Saying bye")
+                            .param().name("name").type(RestParamType.path).dataType("string").description("Who
is it").endParam()
+                            .to("log:bye")
+                        .post("/bye").description("To update the greeting message")
+                            .param().name("greeting").type(RestParamType.body).dataType("string").description("Message
to use as greeting").endParam()
+                            .to("log:bye");
             }
         };
     }
@@ -55,10 +62,12 @@ public class RestSwaggerReaderTest extends CamelTestSupport {
         assertNotNull(rest);
 
         BeanConfig config = new BeanConfig();
-        config.setBasePath("http://localhost:8080/api");
+        config.setHost("localhost:8080");
+        config.setSchemes(new String[]{"http"});
+        config.setBasePath("/api");
         RestSwaggerReader reader = new RestSwaggerReader();
 
-        Swagger swagger = reader.read(rest);
+        Swagger swagger = reader.read(rest, config);
         assertNotNull(swagger);
 
         ObjectMapper mapper = new ObjectMapper();
@@ -68,10 +77,12 @@ public class RestSwaggerReaderTest extends CamelTestSupport {
 
         log.info(json);
 
-        assertTrue(json.contains("\"basePath\":\"http://localhost:8080/api\""));
-        assertTrue(json.contains("\"resourcePath\":\"/hello\""));
-        assertTrue(json.contains("\"method\":\"GET\""));
-        assertTrue(json.contains("\"nickname\":\"getHelloHi\""));
+        assertTrue(json.contains("\"host\" : \"localhost:8080\""));
+        assertTrue(json.contains("\"basePath\" : \"/api\""));
+        assertTrue(json.contains("\"/hello//bye\""));
+        assertTrue(json.contains("\"summary\" : \"To update the greeting message\""));
+        assertTrue(json.contains("\"/hello//bye/{name}\""));
+        assertTrue(json.contains("\"/hello//hi/{name}\""));
 
         context.stop();
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/e5c2ffb0/components/camel-swagger-java/src/test/resources/log4j.properties
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/test/resources/log4j.properties b/components/camel-swagger-java/src/test/resources/log4j.properties
index 36983fa..6e040b0 100644
--- a/components/camel-swagger-java/src/test/resources/log4j.properties
+++ b/components/camel-swagger-java/src/test/resources/log4j.properties
@@ -18,11 +18,11 @@
 #
 # The logging properties used for testing.
 #
-log4j.rootLogger=INFO, out
+log4j.rootLogger=INFO, file
 
 # uncomment the following to enable camel debugging
-#log4j.logger.org.apache.camel.component.swagger=TRACE
-#log4j.logger.com.wordnik.swagger=DEBUG
+#log4j.logger.org.apache.camel.swagger=TRACE
+#log4j.logger.io.swagger=DEBUG
 
 # CONSOLE appender not used by default
 log4j.appender.out=org.apache.log4j.ConsoleAppender


Mime
View raw message