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 2020/12/04 06:09:05 UTC

[camel] branch master updated: CAMEL-15919: map java.io.File to swagger response schema type 'file' (#4719)

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 c242925  CAMEL-15919: map java.io.File to swagger response schema type 'file' (#4719)
c242925 is described below

commit c242925e1226575d37bd03626acf8ff4eeee4f53
Author: Michael Prankl <ei...@gmail.com>
AuthorDate: Fri Dec 4 07:08:46 2020 +0100

    CAMEL-15919: map java.io.File to swagger response schema type 'file' (#4719)
    
    * CAMEL-15919: map java.io.File to swagger response schema type 'file'
    
    * CAMEL-15919: map java.io.File to corresponding OAS schema type (and format)
    
    Co-authored-by: Michael Prankl <mi...@muenchen.de>
---
 .../apache/camel/openapi/RestModelConverters.java  |   5 +-
 .../apache/camel/openapi/RestOpenApiReader.java    |   7 ++
 .../RestOpenApiReaderFileResponseModelTest.java    | 111 +++++++++++++++++++++
 .../apache/camel/swagger/RestSwaggerReader.java    |   3 +
 .../RestSwaggerReaderFileResponseModelTest.java    |  81 +++++++++++++++
 5 files changed, 206 insertions(+), 1 deletion(-)

diff --git a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestModelConverters.java b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestModelConverters.java
index 93eaf6d..eb8460f 100644
--- a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestModelConverters.java
+++ b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestModelConverters.java
@@ -36,7 +36,10 @@ import io.apicurio.datamodels.openapi.v3.models.Oas30SchemaDefinition;
 public class RestModelConverters {
 
     public List<? extends OasSchema> readClass(OasDocument oasDocument, Class<?> clazz) {
-        if (oasDocument instanceof Oas20Document) {
+        if (clazz.equals(java.io.File.class)) {
+            // File is a special type in OAS2 / OAS3 (no model)
+            return null;
+        } else if (oasDocument instanceof Oas20Document) {
             return readClassOas20((Oas20Document) oasDocument, clazz);
         } else if (oasDocument instanceof Oas30Document) {
             return readClassOas30((Oas30Document) oasDocument, clazz);
diff --git a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiReader.java b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiReader.java
index 6e96147..38d5a0d 100644
--- a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiReader.java
+++ b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiReader.java
@@ -1256,6 +1256,13 @@ public class RestOpenApiReader {
             } else if ("boolean".equals(typeName) || "java.lang.Boolean".equals(typeName)) {
                 prop.format = "boolean";
                 prop.type = "number";
+            } else if ("file".equals(typeName) || "java.io.File".equals(typeName)) {
+                if (openApi instanceof Oas20Document) {
+                    prop.type = "file";
+                } else if (openApi instanceof Oas30Document) {
+                    prop.type = "string";
+                    prop.format = "binary";
+                }
             } else {
                 prop.type = "string";
             }
diff --git a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderFileResponseModelTest.java b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderFileResponseModelTest.java
new file mode 100644
index 0000000..2f0745d
--- /dev/null
+++ b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderFileResponseModelTest.java
@@ -0,0 +1,111 @@
+/*
+ * 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.openapi;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import io.apicurio.datamodels.Library;
+import io.apicurio.datamodels.openapi.models.OasDocument;
+import io.apicurio.datamodels.openapi.v2.models.Oas20Info;
+import io.apicurio.datamodels.openapi.v3.models.Oas30Info;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.engine.DefaultClassResolver;
+import org.apache.camel.model.rest.RestParamType;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class RestOpenApiReaderFileResponseModelTest extends CamelTestSupport {
+
+    private static final Logger LOG = LoggerFactory.getLogger(RestOpenApiReaderFileResponseModelTest.class);
+
+    @BindToRegistry("dummy-rest")
+    private DummyRestConsumerFactory factory = new DummyRestConsumerFactory();
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                rest("/hello").consumes("application/json").produces("application/octet-stream").get("/pdf/{name}").description("Saying hi").param().name("name")
+                    .type(RestParamType.path).dataType("string").description("Who is it").example("Donald Duck").endParam().responseMessage().code(200)
+                    .message("A document as reply").responseModel(java.io.File.class).endResponseMessage().to("log:hi");
+            }
+        };
+    }
+
+    @Test
+    public void testReaderRead() throws Exception {
+        BeanConfig config = new BeanConfig();
+        config.setHost("localhost:8080");
+        config.setSchemes(new String[] { "http" });
+        config.setBasePath("/api");
+        Oas20Info info = new Oas20Info();
+        config.setInfo(info);
+        config.setVersion("2.0");
+        RestOpenApiReader reader = new RestOpenApiReader();
+
+        OasDocument openApi = reader.read(context, context.getRestDefinitions(), null, config, context.getName(),
+                new DefaultClassResolver());
+        assertNotNull(openApi);
+
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.enable(SerializationFeature.INDENT_OUTPUT);
+        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+        Object dump = Library.writeNode(openApi);
+        String json = mapper.writeValueAsString(dump);
+
+        LOG.info(json);
+        assertTrue(json.contains("\"type\" : \"file\""));
+
+        context.stop();
+    }
+
+    @Test
+    public void testReaderReadV3() throws Exception {
+        BeanConfig config = new BeanConfig();
+        config.setHost("localhost:8080");
+        config.setSchemes(new String[] { "http" });
+        config.setBasePath("/api");
+        Oas30Info info = new Oas30Info();
+        config.setInfo(info);
+        RestOpenApiReader reader = new RestOpenApiReader();
+
+        OasDocument openApi = reader.read(context, context.getRestDefinitions(), null, config, context.getName(),
+                new DefaultClassResolver());
+        assertNotNull(openApi);
+
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.enable(SerializationFeature.INDENT_OUTPUT);
+        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+        Object dump = Library.writeNode(openApi);
+        String json = mapper.writeValueAsString(dump);
+
+        LOG.info(json);
+        assertTrue(json.contains("\"format\" : \"binary\""));
+        assertTrue(json.contains("\"type\" : \"string\""));
+
+        context.stop();
+    }
+
+}
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 2b6194c..dc32a3e 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
@@ -53,6 +53,7 @@ import io.swagger.models.properties.ArrayProperty;
 import io.swagger.models.properties.BooleanProperty;
 import io.swagger.models.properties.ByteArrayProperty;
 import io.swagger.models.properties.DoubleProperty;
+import io.swagger.models.properties.FileProperty;
 import io.swagger.models.properties.FloatProperty;
 import io.swagger.models.properties.IntegerProperty;
 import io.swagger.models.properties.LongProperty;
@@ -739,6 +740,8 @@ public class RestSwaggerReader {
                 prop = new DoubleProperty();
             } else if ("boolean".equals(typeName) || "java.lang.Boolean".equals(typeName)) {
                 prop = new BooleanProperty();
+            } else if ("file".equals(typeName) || "java.io.File".equals(typeName)) {
+                prop = new FileProperty();
             } else {
                 prop = new StringProperty(typeName);
             }
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderFileResponseModelTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderFileResponseModelTest.java
new file mode 100644
index 0000000..23af758
--- /dev/null
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderFileResponseModelTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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.BindToRegistry;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.engine.DefaultClassResolver;
+import org.apache.camel.model.rest.RestParamType;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class RestSwaggerReaderFileResponseModelTest extends CamelTestSupport {
+
+    private static final Logger LOG = LoggerFactory.getLogger(RestSwaggerReaderFileResponseModelTest.class);
+
+    @BindToRegistry("dummy-rest")
+    private DummyRestConsumerFactory factory = new DummyRestConsumerFactory();
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                rest("/hello").consumes("application/json").produces("application/octet-stream").get("/pdf/{name}")
+                        .description("Saying hi").param().name("name").type(RestParamType.path)
+                        .dataType("string").description("Who is it").example("Donald Duck").endParam()
+                        .responseMessage().code(200).message("A document as reply").responseModel(java.io.File.class).endResponseMessage()
+                        .to("log:hi");
+            }
+        };
+    }
+
+    @Test
+    public void testReaderRead() throws Exception {
+        BeanConfig config = new BeanConfig();
+        config.setHost("localhost:8080");
+        config.setSchemes(new String[] { "http" });
+        config.setBasePath("/api");
+        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("\"type\" : \"file\""));
+
+        context.stop();
+    }
+
+}