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.

This commit is contained in:
David Guthrie 2017-03-23 11:24:53 -04:00
parent 98d55ba7cd
commit 58d4187054
2 changed files with 36 additions and 0 deletions

View File

@ -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;
}

View File

@ -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;
}