From 58d4187054cc9e5f967f2b5153d2a89f5d81c8cd Mon Sep 17 00:00:00 2001 From: David Guthrie Date: Thu, 23 Mar 2017 11:24:53 -0400 Subject: [PATCH] Added a new additional option that allows one to replace the the 'file' generated class with org.springframework.core.io.Resource, which will allow returing file data from Urls or anything that can be converted to a stream. --- .../codegen/languages/SpringCodegen.java | 34 +++++++++++++++++++ .../options/SpringOptionsProvider.java | 2 ++ 2 files changed, 36 insertions(+) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java index 9616f4f11ad..0f4867507ba 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java @@ -5,6 +5,7 @@ import io.swagger.codegen.languages.features.BeanValidationFeatures; import io.swagger.models.Operation; import io.swagger.models.Path; import io.swagger.models.Swagger; +import io.swagger.models.properties.Property; import java.io.File; import java.util.*; @@ -24,6 +25,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation public static final String SPRING_MVC_LIBRARY = "spring-mvc"; public static final String SPRING_CLOUD_LIBRARY = "spring-cloud"; public static final String IMPLICIT_HEADERS = "implicitHeaders"; + public static final String RESOURCE_FILE_STREAMS = "resourceStreams"; protected String title = "swagger-petstore"; protected String configPackage = "io.swagger.configuration"; @@ -66,6 +68,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation cliOptions.add(CliOption.newBoolean(USE_TAGS, "use tags for creating interface and controller classnames")); cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations")); cliOptions.add(CliOption.newBoolean(IMPLICIT_HEADERS, "Use of @ApiImplicitParams for headers.")); + cliOptions.add(CliOption.newBoolean(RESOURCE_FILE_STREAMS, "When \"file\" is specified as a type, generate the type as org.springframework.core.io.Resource, and FileSystemResource for example values.")); supportedLibraries.put(DEFAULT_LIBRARY, "Spring-boot Server application using the SpringFox integration."); supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration."); @@ -148,6 +151,11 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation this.setUseBeanValidation(convertPropertyToBoolean(USE_BEANVALIDATION)); } + if (additionalProperties.containsKey(RESOURCE_FILE_STREAMS)) { + typeMapping.put("file", "Resource"); + importMapping.put("Resource", "org.springframework.core.io.Resource"); + } + if (useBeanValidation) { writePropertyBack(USE_BEANVALIDATION, useBeanValidation); } @@ -448,6 +456,32 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation return camelize(name) + "Api"; } + @Override + public void setParameterExampleValue(CodegenParameter p) { + String type = p.baseType; + if (type == null) { + type = p.dataType; + } + + if ("File".equals(type)) { + String example; + + if (p.defaultValue == null) { + example = p.example; + } else { + example = p.defaultValue; + } + + if (example == null) { + example = "/path/to/file"; + } + example = "new org.springframework.core.io.FileSystemResource(new java.io.File(\"" + escapeText(example) + "\"))"; + p.example = example; + } else { + super.setParameterExampleValue(p); + } + } + public void setTitle(String title) { this.title = title; } diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/SpringOptionsProvider.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/SpringOptionsProvider.java index 6387d49f144..d3cc27dc723 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/SpringOptionsProvider.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/SpringOptionsProvider.java @@ -20,6 +20,7 @@ public class SpringOptionsProvider extends JavaOptionsProvider { public static final String USE_TAGS = "useTags"; public static final String USE_BEANVALIDATION = "false"; public static final String IMPLICIT_HEADERS = "false"; + public static final String RESOURCE_FILE_STREAMS = "false"; @Override public String getLanguage() { @@ -42,6 +43,7 @@ public class SpringOptionsProvider extends JavaOptionsProvider { options.put(SpringCodegen.USE_TAGS, USE_TAGS); options.put(SpringCodegen.USE_BEANVALIDATION, USE_BEANVALIDATION); options.put(SpringCodegen.IMPLICIT_HEADERS, IMPLICIT_HEADERS); + options.put(SpringCodegen.RESOURCE_FILE_STREAMS, RESOURCE_FILE_STREAMS); return options; }