Skip to content

Commit dae9dc0

Browse files
authored
Merge pull request #690 from swagger-api/notNullJacksonAnnotation
NotNullJacksonAnnotation option
2 parents 1f432ad + 9839fc2 commit dae9dc0

File tree

10 files changed

+95
-4
lines changed

10 files changed

+95
-4
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);

src/main/resources/handlebars/Java/libraries/okhttp-gson/pom.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@
183183
<version>${swagger-core-version}</version>
184184
</dependency>
185185
{{/useOas2}}
186+
{{#notNullJacksonAnnotation}}
187+
<dependency>
188+
<groupId>com.fasterxml.jackson.core</groupId>
189+
<artifactId>jackson-annotations</artifactId>
190+
<version>2.10.1</version>
191+
</dependency>
192+
{{/notNullJacksonAnnotation}}
186193
<dependency>
187194
<groupId>com.squareup.okhttp</groupId>
188195
<artifactId>okhttp</artifactId>

src/main/resources/handlebars/Java/pojo.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
{{#description}}{{#useOas2}}@ApiModel{{/useOas2}}{{^useOas2}}@Schema{{/useOas2}}(description = "{{{description}}}"){{/description}}
55
{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}
66

7+
{{#notNullJacksonAnnotation}}@JsonInclude(JsonInclude.Include.NON_NULL){{/notNullJacksonAnnotation}}
8+
79
public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcelableModel}}implements Parcelable {{#serializableModel}}, Serializable {{/serializableModel}}{{#interfaceModels}}{{#@first}}, {{/@first}}{{classname}}{{^@last}}, {{/@last}}{{#@last}} {{/@last}}{{/interfaceModels}}{{/parcelableModel}}{{^parcelableModel}}{{#serializableModel}}implements Serializable{{#interfaceModels}}, {{classname}}{{^@last}}, {{/@last}}{{#@last}} {{/@last}}{{/interfaceModels}}{{/serializableModel}}{{^serializableModel}}{{#interfaceModels}}{{#@first}}implements {{/@first}}{{classname}}{{^@last}}, {{/@last}}{{#@last}} {{/@last}}{{/interfaceModels}}{{/serializableModel}}{{/parcelableModel}}{
810
{{#serializableModel}}
911
private static final long serialVersionUID = 1L;

src/main/resources/handlebars/JavaSpring/libraries/spring-boot/pom.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@
9292
<artifactId>validation-api</artifactId>
9393
</dependency>
9494
{{/useBeanValidation}}
95+
{{#notNullJacksonAnnotation}}
96+
<dependency>
97+
<groupId>com.fasterxml.jackson.core</groupId>
98+
<artifactId>jackson-annotations</artifactId>
99+
<version>2.10.1</version>
100+
</dependency>
101+
{{/notNullJacksonAnnotation}}
95102

96103
<dependency>
97104
<groupId>org.springframework.boot</groupId>

src/main/resources/handlebars/JavaSpring/libraries/spring-cloud/pom.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,12 @@
111111
<artifactId>spring-boot-starter-test</artifactId>
112112
<scope>test</scope>
113113
</dependency>
114+
{{#notNullJacksonAnnotation}}
115+
<dependency>
116+
<groupId>com.fasterxml.jackson.core</groupId>
117+
<artifactId>jackson-annotations</artifactId>
118+
<version>2.10.1</version>
119+
</dependency>
120+
{{/notNullJacksonAnnotation}}
114121
</dependencies>
115122
</project>

src/main/resources/handlebars/JavaSpring/libraries/spring-mvc/pom.mustache

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,13 @@
181181
</exclusion>
182182
</exclusions>
183183
</dependency>
184-
184+
{{#notNullJacksonAnnotation}}
185+
<dependency>
186+
<groupId>com.fasterxml.jackson.core</groupId>
187+
<artifactId>jackson-annotations</artifactId>
188+
<version>${jackson-version}</version>
189+
</dependency>
190+
{{/notNullJacksonAnnotation}}
185191
<dependency>
186192
<groupId>org.apache.httpcomponents</groupId>
187193
<artifactId>httpclient</artifactId>

src/main/resources/handlebars/JavaSpring/pojo.mustache

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

0 commit comments

Comments
 (0)