Skip to content

Commit 584f704

Browse files
committed
refs issue#6488 adding option NotNullJacksonAnnotation
1 parent b3e53b0 commit 584f704

File tree

13 files changed

+295
-4
lines changed

13 files changed

+295
-4
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Map;
1111
import java.util.regex.Pattern;
1212

13+
import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
1314
import org.apache.commons.lang3.BooleanUtils;
1415
import org.apache.commons.lang3.StringUtils;
1516

@@ -43,6 +44,7 @@
4344
import io.swagger.models.properties.Property;
4445
import io.swagger.models.properties.StringProperty;
4546

47+
import static io.swagger.codegen.languages.features.NotNullAnnotationFeatures.NOT_NULL_JACKSON_ANNOTATION;
4648

4749
public abstract class AbstractJavaCodegen extends DefaultCodegen implements CodegenConfig {
4850

@@ -88,6 +90,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
8890
protected String modelDocPath = "docs/";
8991
protected boolean supportJava6= false;
9092
protected boolean disableHtmlEscaping = false;
93+
private NotNullAnnotationFeatures notNullOption;
9194

9295
public AbstractJavaCodegen() {
9396
super();
@@ -161,7 +164,9 @@ public AbstractJavaCodegen() {
161164
cliOptions.add(CliOption.newBoolean(FULL_JAVA_UTIL, "whether to use fully qualified name for classes under java.util. This option only works for Java API client"));
162165
cliOptions.add(new CliOption("hideGenerationTimestamp", "hides the timestamp when files were generated"));
163166
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)"));
164-
167+
if(this instanceof NotNullAnnotationFeatures){
168+
cliOptions.add(CliOption.newBoolean(NOT_NULL_JACKSON_ANNOTATION, "adds @JsonInclude(JsonInclude.Include.NON_NULL) annotation to model classes"));
169+
}
165170
CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use");
166171
Map<String, String> dateOptions = new HashMap<String, String>();
167172
dateOptions.put("java8", "Java 8 native JSR310 (preferred for jdk 1.8+) - note: this also sets \"" + JAVA8_MODE + "\" to true");
@@ -338,6 +343,17 @@ public void processOpts() {
338343
this.setFullJavaUtil(Boolean.valueOf(additionalProperties.get(FULL_JAVA_UTIL).toString()));
339344
}
340345

346+
if (this instanceof NotNullAnnotationFeatures) {
347+
notNullOption = (NotNullAnnotationFeatures)this;
348+
if (additionalProperties.containsKey(NOT_NULL_JACKSON_ANNOTATION)) {
349+
notNullOption.setNotNullJacksonAnnotation(convertPropertyToBoolean(NOT_NULL_JACKSON_ANNOTATION));
350+
writePropertyBack(NOT_NULL_JACKSON_ANNOTATION, notNullOption.isNotNullJacksonAnnotation());
351+
if (notNullOption.isNotNullJacksonAnnotation()) {
352+
importMapping.put("JsonInclude", "com.fasterxml.jackson.annotation.JsonInclude");
353+
}
354+
}
355+
}
356+
341357
if (fullJavaUtil) {
342358
javaUtilPrefix = "java.util.";
343359
}
@@ -907,6 +923,16 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
907923
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel);
908924
codegenModel = AbstractJavaCodegen.reconcileInlineEnums(codegenModel, parentCodegenModel);
909925
}
926+
if (this instanceof NotNullAnnotationFeatures) {
927+
if (this instanceof NotNullAnnotationFeatures) {
928+
notNullOption = (NotNullAnnotationFeatures)this;
929+
if (additionalProperties.containsKey(NOT_NULL_JACKSON_ANNOTATION)) {
930+
if (notNullOption.isNotNullJacksonAnnotation()) {
931+
codegenModel.imports.add("JsonInclude");
932+
}
933+
}
934+
}
935+
}
910936
return codegenModel;
911937
}
912938

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.swagger.codegen.*;
77
import io.swagger.codegen.languages.features.BeanValidationFeatures;
88
import io.swagger.codegen.languages.features.GzipFeatures;
9+
import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
910
import io.swagger.codegen.languages.features.PerformBeanValidationFeatures;
1011

1112
import org.apache.commons.lang3.BooleanUtils;
@@ -19,7 +20,7 @@
1920

2021
public class JavaClientCodegen extends AbstractJavaCodegen
2122
implements BeanValidationFeatures, PerformBeanValidationFeatures,
22-
GzipFeatures
23+
GzipFeatures, NotNullAnnotationFeatures
2324
{
2425
static final String MEDIA_TYPE = "mediaType";
2526

@@ -52,7 +53,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
5253
protected boolean performBeanValidation = false;
5354
protected boolean useGzipFeature = false;
5455
protected boolean useRuntimeException = false;
55-
56+
private boolean notNullJacksonAnnotation;
5657

5758
public JavaClientCodegen() {
5859
super();
@@ -611,4 +612,13 @@ static boolean isJsonVendorMimeType(String mime) {
611612
return mime != null && JSON_VENDOR_MIME_PATTERN.matcher(mime).matches();
612613
}
613614

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

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.samskivert.mustache.Template;
55
import io.swagger.codegen.*;
66
import io.swagger.codegen.languages.features.BeanValidationFeatures;
7+
import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
78
import io.swagger.codegen.languages.features.OptionalFeatures;
89
import io.swagger.models.Operation;
910
import io.swagger.models.Path;
@@ -17,7 +18,7 @@
1718

1819

1920
public class SpringCodegen extends AbstractJavaCodegen
20-
implements BeanValidationFeatures, OptionalFeatures {
21+
implements BeanValidationFeatures, OptionalFeatures, NotNullAnnotationFeatures {
2122
public static final String DEFAULT_LIBRARY = "spring-boot";
2223
public static final String TITLE = "title";
2324
public static final String CONFIG_PACKAGE = "configPackage";
@@ -53,6 +54,7 @@ public class SpringCodegen extends AbstractJavaCodegen
5354
protected boolean useOptional = false;
5455
protected boolean openFeign = false;
5556
protected boolean defaultInterfaces = true;
57+
private boolean notNullJacksonAnnotation;
5658

5759
public SpringCodegen() {
5860
super();
@@ -499,6 +501,16 @@ public void setReturnContainer(final String returnContainer) {
499501
return objs;
500502
}
501503

504+
@Override
505+
public void setNotNullJacksonAnnotation(boolean notNullJacksonAnnotation) {
506+
this.notNullJacksonAnnotation = notNullJacksonAnnotation;
507+
}
508+
509+
@Override
510+
public boolean isNotNullJacksonAnnotation() {
511+
return notNullJacksonAnnotation;
512+
}
513+
502514
private interface DataTypeAssigner {
503515
void setReturnType(String returnType);
504516
void setReturnContainer(String returnContainer);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.swagger.codegen.languages.features;
2+
3+
public interface NotNullAnnotationFeatures {
4+
// Language supports generating not Null Jackson Annotation
5+
String NOT_NULL_JACKSON_ANNOTATION = "notNullJacksonAnnotation";
6+
7+
void setNotNullJacksonAnnotation(boolean notNullJacksonAnnotation);
8+
9+
boolean isNotNullJacksonAnnotation();
10+
}

modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/pom.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@
236236
<scope>provided</scope>
237237
</dependency>
238238
{{/useBeanValidation}}
239+
{{#notNullJacksonAnnotation}}
240+
<dependency>
241+
<groupId>com.fasterxml.jackson.core</groupId>
242+
<artifactId>jackson-annotations</artifactId>
243+
<version>2.10.1</version>
244+
</dependency>
245+
{{/notNullJacksonAnnotation}}
239246
{{#performBeanValidation}}
240247
<!-- Bean Validation Impl. used to perform BeanValidation -->
241248
<dependency>

modules/swagger-codegen/src/main/resources/Java/pojo.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
*/{{#description}}
44
@ApiModel(description = "{{{description}}}"){{/description}}
55
{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}
6+
7+
{{#notNullJacksonAnnotation}}@JsonInclude(JsonInclude.Include.NON_NULL){{/notNullJacksonAnnotation}}
8+
69
public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcelableModel}}implements Parcelable {{#serializableModel}}, Serializable {{/serializableModel}}{{/parcelableModel}}{{^parcelableModel}}{{#serializableModel}}implements Serializable {{/serializableModel}}{{/parcelableModel}}{
710
{{#serializableModel}}
811
private static final long serialVersionUID = 1L;

modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-boot/pom.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@
9595
<artifactId>validation-api</artifactId>
9696
</dependency>
9797
{{/useBeanValidation}}
98+
{{#notNullJacksonAnnotation}}
99+
<dependency>
100+
<groupId>com.fasterxml.jackson.core</groupId>
101+
<artifactId>jackson-annotations</artifactId>
102+
<version>2.10.1</version>
103+
</dependency>
104+
{{/notNullJacksonAnnotation}}
98105
<dependency>
99106
<groupId>org.springframework.boot</groupId>
100107
<artifactId>spring-boot-starter-test</artifactId>

modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-cloud/pom.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@
9393
<scope>provided</scope>
9494
</dependency>
9595
{{/useBeanValidation}}
96+
{{#notNullJacksonAnnotation}}
97+
<dependency>
98+
<groupId>com.fasterxml.jackson.core</groupId>
99+
<artifactId>jackson-annotations</artifactId>
100+
<version>2.10.1</version>
101+
</dependency>
102+
{{/notNullJacksonAnnotation}}
96103
<dependency>
97104
<groupId>org.springframework.boot</groupId>
98105
<artifactId>spring-boot-starter-test</artifactId>

modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-mvc/pom.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@
161161
<scope>provided</scope>
162162
</dependency>
163163
{{/useBeanValidation}}
164+
{{#notNullJacksonAnnotation}}
165+
<dependency>
166+
<groupId>com.fasterxml.jackson.core</groupId>
167+
<artifactId>jackson-annotations</artifactId>
168+
<version>${jackson-version}</version>
169+
</dependency>
170+
{{/notNullJacksonAnnotation}}
164171
<dependency>
165172
<groupId>org.testng</groupId>
166173
<artifactId>testng</artifactId>

modules/swagger-codegen/src/main/resources/JavaSpring/pojo.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
@ApiModel(description = "{{{description}}}"){{/description}}
55
{{#useBeanValidation}}@Validated{{/useBeanValidation}}
66
{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}
7+
8+
{{#notNullJacksonAnnotation}}@JsonInclude(JsonInclude.Include.NON_NULL){{/notNullJacksonAnnotation}}
9+
710
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
811
{{#serializableModel}}
912
private static final long serialVersionUID = 1L;

0 commit comments

Comments
 (0)