Skip to content

Commit c23bcc6

Browse files
committed
fres - swagger/codegen#6488 - notNullJacksonAnnotation option
1 parent 76ea8a8 commit c23bcc6

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.swagger.codegen.v3.generators.features;
2+
3+
public interface NotNullAnnotationFeatures {
4+
5+
// Language supports generating not Null Jackson Annotation
6+
String NOT_NULL_JACKSON_ANNOTATION = "notNullJacksonAnnotation";
7+
8+
void setNotNullJacksonAnnotation(boolean notNullJacksonAnnotation);
9+
10+
boolean isNotNullJacksonAnnotation();
11+
}

src/main/java/io/swagger/codegen/v3/generators/java/AbstractJavaCodegen.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.swagger.codegen.v3.CodegenParameter;
1010
import io.swagger.codegen.v3.CodegenProperty;
1111
import io.swagger.codegen.v3.generators.DefaultCodegenConfig;
12+
import io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures;
1213
import io.swagger.codegen.v3.generators.handlebars.java.JavaHelper;
1314
import io.swagger.v3.oas.models.OpenAPI;
1415
import io.swagger.v3.oas.models.Operation;
@@ -40,6 +41,7 @@
4041

4142
import static io.swagger.codegen.v3.CodegenConstants.HAS_ENUMS_EXT_NAME;
4243
import static io.swagger.codegen.v3.CodegenConstants.IS_ENUM_EXT_NAME;
44+
import static io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures.NOT_NULL_JACKSON_ANNOTATION;
4345
import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue;
4446

4547
public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
@@ -81,6 +83,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
8183
protected String apiDocPath = "docs/";
8284
protected String modelDocPath = "docs/";
8385
protected boolean supportJava6= false;
86+
private NotNullAnnotationFeatures notNullOption;
8487

8588
public AbstractJavaCodegen() {
8689
super();
@@ -150,7 +153,9 @@ public AbstractJavaCodegen() {
150153
cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC));
151154
cliOptions.add(CliOption.newBoolean(WITH_XML, "whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)"));
152155
cliOptions.add(CliOption.newBoolean(CodegenConstants.USE_OAS2, CodegenConstants.USE_OAS2_DESC));
153-
156+
if(this instanceof NotNullAnnotationFeatures){
157+
cliOptions.add(CliOption.newBoolean(NOT_NULL_JACKSON_ANNOTATION, "adds @JsonInclude(JsonInclude.Include.NON_NULL) annotation to model classes"));
158+
}
154159
CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use");
155160
Map<String, String> dateOptions = new HashMap<String, String>();
156161
dateOptions.put("java8", "Java 8 native JSR310 (preferred for jdk 1.8+) - note: this also sets \"" + JAVA8_MODE + "\" to true");
@@ -328,6 +333,17 @@ public void processOpts() {
328333
this.setFullJavaUtil(Boolean.valueOf(additionalProperties.get(FULL_JAVA_UTIL).toString()));
329334
}
330335

336+
if (this instanceof NotNullAnnotationFeatures) {
337+
notNullOption = (NotNullAnnotationFeatures)this;
338+
if (additionalProperties.containsKey(NOT_NULL_JACKSON_ANNOTATION)) {
339+
notNullOption.setNotNullJacksonAnnotation(convertPropertyToBoolean(NOT_NULL_JACKSON_ANNOTATION));
340+
writePropertyBack(NOT_NULL_JACKSON_ANNOTATION, notNullOption.isNotNullJacksonAnnotation());
341+
if (notNullOption.isNotNullJacksonAnnotation()) {
342+
importMapping.put("JsonInclude", "com.fasterxml.jackson.annotation.JsonInclude");
343+
}
344+
}
345+
}
346+
331347
if (fullJavaUtil) {
332348
javaUtilPrefix = "java.util.";
333349
}
@@ -912,6 +928,16 @@ public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> al
912928
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel, allSchemas);
913929
codegenModel = AbstractJavaCodegen.reconcileInlineEnums(codegenModel, parentCodegenModel);
914930
}
931+
if (this instanceof NotNullAnnotationFeatures) {
932+
if (this instanceof NotNullAnnotationFeatures) {
933+
notNullOption = (NotNullAnnotationFeatures)this;
934+
if (additionalProperties.containsKey(NOT_NULL_JACKSON_ANNOTATION)) {
935+
if (notNullOption.isNotNullJacksonAnnotation()) {
936+
codegenModel.imports.add("JsonInclude");
937+
}
938+
}
939+
}
940+
}
915941
return codegenModel;
916942
}
917943

src/main/java/io/swagger/codegen/v3/generators/java/JavaClientCodegen.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.swagger.codegen.v3.SupportingFile;
1111
import io.swagger.codegen.v3.generators.features.BeanValidationFeatures;
1212
import io.swagger.codegen.v3.generators.features.GzipFeatures;
13+
import io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures;
1314
import io.swagger.codegen.v3.generators.features.PerformBeanValidationFeatures;
1415
import io.swagger.codegen.v3.generators.util.OpenAPIUtil;
1516
import org.apache.commons.lang3.BooleanUtils;
@@ -32,7 +33,7 @@
3233
import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue;
3334
import static java.util.Collections.sort;
3435

35-
public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValidationFeatures, PerformBeanValidationFeatures, GzipFeatures {
36+
public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValidationFeatures, PerformBeanValidationFeatures, GzipFeatures, NotNullAnnotationFeatures {
3637
static final String MEDIA_TYPE = "mediaType";
3738

3839
private static final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);
@@ -62,6 +63,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValida
6263
protected boolean performBeanValidation = false;
6364
protected boolean useGzipFeature = false;
6465
protected boolean useRuntimeException = false;
66+
private boolean notNullJacksonAnnotation = false;
6567

6668

6769
public JavaClientCodegen() {
@@ -611,4 +613,13 @@ static boolean isJsonVendorMimeType(String mime) {
611613
return mime != null && JSON_VENDOR_MIME_PATTERN.matcher(mime).matches();
612614
}
613615

616+
@Override
617+
public void setNotNullJacksonAnnotation(boolean notNullJacksonAnnotation) {
618+
this.notNullJacksonAnnotation = notNullJacksonAnnotation;
619+
}
620+
621+
@Override
622+
public boolean isNotNullJacksonAnnotation() {
623+
return notNullJacksonAnnotation;
624+
}
614625
}

src/main/java/io/swagger/codegen/v3/generators/java/SpringCodegen.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.swagger.codegen.v3.CodegenType;
1515
import io.swagger.codegen.v3.SupportingFile;
1616
import io.swagger.codegen.v3.generators.features.BeanValidationFeatures;
17+
import io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures;
1718
import io.swagger.codegen.v3.generators.features.OptionalFeatures;
1819
import io.swagger.codegen.v3.generators.util.OpenAPIUtil;
1920
import io.swagger.codegen.v3.utils.URLPathUtil;
@@ -36,7 +37,7 @@
3637
import static io.swagger.codegen.v3.CodegenConstants.IS_ENUM_EXT_NAME;
3738
import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue;
3839

39-
public class SpringCodegen extends AbstractJavaCodegen implements BeanValidationFeatures, OptionalFeatures {
40+
public class SpringCodegen extends AbstractJavaCodegen implements BeanValidationFeatures, OptionalFeatures, NotNullAnnotationFeatures {
4041
static Logger LOGGER = LoggerFactory.getLogger(SpringCodegen.class);
4142
public static final String DEFAULT_LIBRARY = "spring-boot";
4243
public static final String TITLE = "title";
@@ -78,6 +79,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
7879
protected boolean defaultInterfaces = true;
7980
protected String springBootVersion = "1.5.22.RELEASE";
8081
protected boolean throwsException = false;
82+
private boolean notNullJacksonAnnotation = false;
8183

8284
public SpringCodegen() {
8385
super();
@@ -591,6 +593,16 @@ public void setReturnContainer(final String returnContainer) {
591593
return objs;
592594
}
593595

596+
@Override
597+
public void setNotNullJacksonAnnotation(boolean notNullJacksonAnnotation) {
598+
this.notNullJacksonAnnotation = notNullJacksonAnnotation;
599+
}
600+
601+
@Override
602+
public boolean isNotNullJacksonAnnotation() {
603+
return notNullJacksonAnnotation;
604+
}
605+
594606
private interface DataTypeAssigner {
595607
void setReturnType(String returnType);
596608
void setReturnContainer(String returnContainer);

0 commit comments

Comments
 (0)