forked from loafle/openapi-generator-original
[Spring] Adds feature useOptional (#5968)
* Adds Valid annotation for request body (#4847) If useBeanValidation is active, this change will add Valid annotation to ReqeustBody * Adds generated samples for bean vaildation in spring boot (#4847) * Adds feature option (#3819) When you enable the feature useOptional the JavaSpring generator will use type Optional for non required parameters. * Adds generated sample for #3819 * Adds generated sample for #3819 * Reverts commit for bean validation * Adds generated sample for #3819 * Reverts commit for bean validation * Fix alignment * update spring samples * update pom.xml to include spring use optional samples * update artifactId to "spring-boot-beanvalidation" * rpelace tab with 4-space * check mvn task result
This commit is contained in:
@@ -4,6 +4,7 @@ import com.samskivert.mustache.Mustache;
|
||||
import com.samskivert.mustache.Template;
|
||||
import io.swagger.codegen.*;
|
||||
import io.swagger.codegen.languages.features.BeanValidationFeatures;
|
||||
import io.swagger.codegen.languages.features.OptionalFeatures;
|
||||
import io.swagger.models.Operation;
|
||||
import io.swagger.models.Path;
|
||||
import io.swagger.models.Swagger;
|
||||
@@ -15,7 +16,8 @@ import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
|
||||
public class SpringCodegen extends AbstractJavaCodegen implements BeanValidationFeatures {
|
||||
public class SpringCodegen extends AbstractJavaCodegen
|
||||
implements BeanValidationFeatures, OptionalFeatures {
|
||||
public static final String DEFAULT_LIBRARY = "spring-boot";
|
||||
public static final String TITLE = "title";
|
||||
public static final String CONFIG_PACKAGE = "configPackage";
|
||||
@@ -43,6 +45,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
|
||||
protected boolean useTags = false;
|
||||
protected boolean useBeanValidation = true;
|
||||
protected boolean implicitHeaders = false;
|
||||
protected boolean useOptional = false;
|
||||
|
||||
public SpringCodegen() {
|
||||
super();
|
||||
@@ -72,6 +75,8 @@ 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(USE_OPTIONAL,
|
||||
"Use Optional container for optional parameters"));
|
||||
|
||||
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.");
|
||||
@@ -154,17 +159,24 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
|
||||
this.setUseBeanValidation(convertPropertyToBoolean(USE_BEANVALIDATION));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(USE_OPTIONAL)) {
|
||||
this.setUseOptional(convertPropertyToBoolean(USE_OPTIONAL));
|
||||
}
|
||||
|
||||
if (useBeanValidation) {
|
||||
writePropertyBack(USE_BEANVALIDATION, useBeanValidation);
|
||||
}
|
||||
|
||||
|
||||
if (additionalProperties.containsKey(IMPLICIT_HEADERS)) {
|
||||
this.setImplicitHeaders(Boolean.valueOf(additionalProperties.get(IMPLICIT_HEADERS).toString()));
|
||||
}
|
||||
|
||||
typeMapping.put("file", "Resource");
|
||||
importMapping.put("Resource", "org.springframework.core.io.Resource");
|
||||
|
||||
if (useOptional) {
|
||||
writePropertyBack(USE_OPTIONAL, useOptional);
|
||||
}
|
||||
|
||||
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
|
||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||
@@ -613,4 +625,8 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
|
||||
this.useBeanValidation = useBeanValidation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUseOptional(boolean useOptional) {
|
||||
this.useOptional = useOptional;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package io.swagger.codegen.languages.features;
|
||||
|
||||
public interface OptionalFeatures {
|
||||
|
||||
// Language supports generating Optional Types
|
||||
String USE_OPTIONAL = "useOptional";
|
||||
|
||||
void setUseOptional(boolean useOptional);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user