camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/2] camel git commit: CAMEL-9220: swagger model should include nested types.
Date Thu, 15 Oct 2015 06:00:31 GMT
Repository: camel
Updated Branches:
  refs/heads/camel-2.16.x b09b1a612 -> ec4919bcf
  refs/heads/master 16e4a4719 -> e33d97b62


CAMEL-9220: swagger model should include nested types.


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

Branch: refs/heads/master
Commit: e33d97b6255f1b0ac6e9e86f8129dc71571c99c4
Parents: 16e4a47
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Thu Oct 15 08:00:16 2015 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu Oct 15 08:00:16 2015 +0200

----------------------------------------------------------------------
 .../camel/swagger/RestModelConverters.java      | 11 +++
 .../apache/camel/swagger/RestSwaggerReader.java |  1 +
 .../org/apache/camel/swagger/BookOrder.java     | 47 ++++++++++
 .../java/org/apache/camel/swagger/LineItem.java | 45 ++++++++++
 .../RestSwaggerReaderModelBookOrderTest.java    | 92 ++++++++++++++++++++
 5 files changed, 196 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e33d97b6/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestModelConverters.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestModelConverters.java
b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestModelConverters.java
index ed5b705..8ea9a99 100644
--- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestModelConverters.java
+++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestModelConverters.java
@@ -33,8 +33,19 @@ public class RestModelConverters extends ModelConverters {
         Map<String, Model> resolved = super.read(clazz);
         if (resolved != null) {
             for (Model model : resolved.values()) {
+                // enrich with the class name of the model
                 model.getVendorExtensions().put("x-className", new StringProperty(name));
             }
+
+            // read any extra using read-all
+            Map<String, Model> extra = super.readAll(clazz);
+            if (extra != null) {
+                for (Map.Entry<String, Model> entry : extra.entrySet()) {
+                    if (!resolved.containsKey(entry.getKey())) {
+                        resolved.put(entry.getKey(), entry.getValue());
+                    }
+                }
+            }
         }
         return resolved;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/e33d97b6/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 ea8a6a2..66b3634 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
@@ -25,6 +25,7 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 
+import io.swagger.converter.ModelConverters;
 import io.swagger.jaxrs.config.BeanConfig;
 import io.swagger.models.Model;
 import io.swagger.models.ModelImpl;

http://git-wip-us.apache.org/repos/asf/camel/blob/e33d97b6/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/BookOrder.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/BookOrder.java
b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/BookOrder.java
new file mode 100644
index 0000000..b41403e
--- /dev/null
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/BookOrder.java
@@ -0,0 +1,47 @@
+/**
+ * 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.swagger;
+
+import java.util.List;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+
+@ApiModel(description = "Represents a book order")
+public class BookOrder {
+
+    private String id;
+    private List<LineItem> items;
+
+    @ApiModelProperty(value = "The id of the order", required = true)
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    @ApiModelProperty(value = "The books ordered", required = true)
+    public List<LineItem> getItems() {
+        return items;
+    }
+
+    public void setItems(List<LineItem> items) {
+        this.items = items;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/e33d97b6/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/LineItem.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/LineItem.java
b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/LineItem.java
new file mode 100644
index 0000000..2b567e2
--- /dev/null
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/LineItem.java
@@ -0,0 +1,45 @@
+/**
+ * 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.swagger;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+
+@ApiModel(description = "Order line")
+public class LineItem {
+
+    private String isbn;
+    private String name;
+
+    @ApiModelProperty(value = "ISBN of the book", required = true)
+    public String getIsbn() {
+        return isbn;
+    }
+
+    public void setIsbn(String isbn) {
+        this.isbn = isbn;
+    }
+
+    @ApiModelProperty(value = "Name of the book", required = true)
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/e33d97b6/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelBookOrderTest.java
----------------------------------------------------------------------
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelBookOrderTest.java
b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelBookOrderTest.java
new file mode 100644
index 0000000..0c534a1
--- /dev/null
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelBookOrderTest.java
@@ -0,0 +1,92 @@
+/**
+ * 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.swagger;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+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.junit.Test;
+
+public class RestSwaggerReaderModelBookOrderTest extends CamelTestSupport {
+
+    @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 {
+                // this user REST service is json only
+                rest("/books").tag("dude").description("Book order service")
+                    .consumes("application/json").produces("application/json")
+
+                    .get("/{id}").description("Find order by id").outType(BookOrder.class)
+                        .responseMessage().message("The order returned").endResponseMessage()
+                        .param().name("id").type(RestParamType.path).description("The id
of the order to get").dataType("integer").endParam()
+                        .to("bean:bookService?method=getOrder(${header.id})");
+            }
+        };
+    }
+
+    @Test
+    public void testReaderRead() throws Exception {
+        BeanConfig config = new BeanConfig();
+        config.setHost("localhost:8080");
+        config.setSchemes(new String[]{"http"});
+        config.setBasePath("/api");
+        config.setTitle("Camel User store");
+        config.setLicense("Apache 2.0");
+        config.setLicenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html");
+        RestSwaggerReader reader = new RestSwaggerReader();
+
+        Swagger swagger = reader.read(context.getRestDefinitions(), null, config, context.getName(),
new DefaultClassResolver());
+        assertNotNull(swagger);
+
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.enable(SerializationFeature.INDENT_OUTPUT);
+        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+        String json = mapper.writeValueAsString(swagger);
+
+        log.info(json);
+
+        assertTrue(json.contains("\"host\" : \"localhost:8080\""));
+        assertTrue(json.contains("\"description\" : \"The order returned\""));
+        assertTrue(json.contains("\"BookOrder\""));
+        assertTrue(json.contains("\"LineItem\""));
+        assertTrue(json.contains("\"$ref\" : \"#/definitions/BookOrder\""));
+        assertTrue(json.contains("\"$ref\" : \"#/definitions/LineItem\""));
+        assertTrue(json.contains("\"x-className\""));
+        assertTrue(json.contains("\"format\" : \"org.apache.camel.swagger.BookOrder\""));
+        // TODO: we do not yet have the classname of nested types
+        // assertTrue(json.contains("\"format\" : \"org.apache.camel.swagger.LineItem\""));
+
+        context.stop();
+    }
+
+}


Mime
View raw message