Skip to content

Commit

Permalink
Merge pull request #690 from swagger-api/notNullJacksonAnnotation
Browse files Browse the repository at this point in the history
NotNullJacksonAnnotation option
  • Loading branch information
gracekarina committed May 22, 2020
2 parents 1f432ad + 9839fc2 commit dae9dc0
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.swagger.codegen.v3.generators.features;

public interface NotNullAnnotationFeatures {

// Language supports generating not Null Jackson Annotation
String NOT_NULL_JACKSON_ANNOTATION = "notNullJacksonAnnotation";

void setNotNullJacksonAnnotation(boolean notNullJacksonAnnotation);

boolean isNotNullJacksonAnnotation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.swagger.codegen.v3.CodegenParameter;
import io.swagger.codegen.v3.CodegenProperty;
import io.swagger.codegen.v3.generators.DefaultCodegenConfig;
import io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures;
import io.swagger.codegen.v3.generators.handlebars.java.JavaHelper;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
Expand Down Expand Up @@ -40,6 +41,7 @@

import static io.swagger.codegen.v3.CodegenConstants.HAS_ENUMS_EXT_NAME;
import static io.swagger.codegen.v3.CodegenConstants.IS_ENUM_EXT_NAME;
import static io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures.NOT_NULL_JACKSON_ANNOTATION;
import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue;

public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
Expand Down Expand Up @@ -81,6 +83,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
protected String apiDocPath = "docs/";
protected String modelDocPath = "docs/";
protected boolean supportJava6= false;
private NotNullAnnotationFeatures notNullOption;

public AbstractJavaCodegen() {
super();
Expand Down Expand Up @@ -150,7 +153,9 @@ public AbstractJavaCodegen() {
cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC));
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)"));
cliOptions.add(CliOption.newBoolean(CodegenConstants.USE_OAS2, CodegenConstants.USE_OAS2_DESC));

if(this instanceof NotNullAnnotationFeatures){
cliOptions.add(CliOption.newBoolean(NOT_NULL_JACKSON_ANNOTATION, "adds @JsonInclude(JsonInclude.Include.NON_NULL) annotation to model classes"));
}
CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use");
Map<String, String> dateOptions = new HashMap<String, String>();
dateOptions.put("java8", "Java 8 native JSR310 (preferred for jdk 1.8+) - note: this also sets \"" + JAVA8_MODE + "\" to true");
Expand Down Expand Up @@ -328,6 +333,17 @@ public void processOpts() {
this.setFullJavaUtil(Boolean.valueOf(additionalProperties.get(FULL_JAVA_UTIL).toString()));
}

if (this instanceof NotNullAnnotationFeatures) {
notNullOption = (NotNullAnnotationFeatures)this;
if (additionalProperties.containsKey(NOT_NULL_JACKSON_ANNOTATION)) {
notNullOption.setNotNullJacksonAnnotation(convertPropertyToBoolean(NOT_NULL_JACKSON_ANNOTATION));
writePropertyBack(NOT_NULL_JACKSON_ANNOTATION, notNullOption.isNotNullJacksonAnnotation());
if (notNullOption.isNotNullJacksonAnnotation()) {
importMapping.put("JsonInclude", "com.fasterxml.jackson.annotation.JsonInclude");
}
}
}

if (fullJavaUtil) {
javaUtilPrefix = "java.util.";
}
Expand Down Expand Up @@ -912,6 +928,16 @@ public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> al
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel, allSchemas);
codegenModel = AbstractJavaCodegen.reconcileInlineEnums(codegenModel, parentCodegenModel);
}
if (this instanceof NotNullAnnotationFeatures) {
if (this instanceof NotNullAnnotationFeatures) {
notNullOption = (NotNullAnnotationFeatures)this;
if (additionalProperties.containsKey(NOT_NULL_JACKSON_ANNOTATION)) {
if (notNullOption.isNotNullJacksonAnnotation()) {
codegenModel.imports.add("JsonInclude");
}
}
}
}
return codegenModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.swagger.codegen.v3.SupportingFile;
import io.swagger.codegen.v3.generators.features.BeanValidationFeatures;
import io.swagger.codegen.v3.generators.features.GzipFeatures;
import io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures;
import io.swagger.codegen.v3.generators.features.PerformBeanValidationFeatures;
import io.swagger.codegen.v3.generators.util.OpenAPIUtil;
import org.apache.commons.lang3.BooleanUtils;
Expand All @@ -32,7 +33,7 @@
import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue;
import static java.util.Collections.sort;

public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValidationFeatures, PerformBeanValidationFeatures, GzipFeatures {
public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValidationFeatures, PerformBeanValidationFeatures, GzipFeatures, NotNullAnnotationFeatures {
static final String MEDIA_TYPE = "mediaType";

private static final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);
Expand Down Expand Up @@ -62,6 +63,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValida
protected boolean performBeanValidation = false;
protected boolean useGzipFeature = false;
protected boolean useRuntimeException = false;
private boolean notNullJacksonAnnotation = false;


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

@Override
public void setNotNullJacksonAnnotation(boolean notNullJacksonAnnotation) {
this.notNullJacksonAnnotation = notNullJacksonAnnotation;
}

@Override
public boolean isNotNullJacksonAnnotation() {
return notNullJacksonAnnotation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.swagger.codegen.v3.CodegenType;
import io.swagger.codegen.v3.SupportingFile;
import io.swagger.codegen.v3.generators.features.BeanValidationFeatures;
import io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures;
import io.swagger.codegen.v3.generators.features.OptionalFeatures;
import io.swagger.codegen.v3.generators.util.OpenAPIUtil;
import io.swagger.codegen.v3.utils.URLPathUtil;
Expand All @@ -36,7 +37,7 @@
import static io.swagger.codegen.v3.CodegenConstants.IS_ENUM_EXT_NAME;
import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue;

public class SpringCodegen extends AbstractJavaCodegen implements BeanValidationFeatures, OptionalFeatures {
public class SpringCodegen extends AbstractJavaCodegen implements BeanValidationFeatures, OptionalFeatures, NotNullAnnotationFeatures {
static Logger LOGGER = LoggerFactory.getLogger(SpringCodegen.class);
public static final String DEFAULT_LIBRARY = "spring-boot";
public static final String TITLE = "title";
Expand Down Expand Up @@ -78,6 +79,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
protected boolean defaultInterfaces = true;
protected String springBootVersion = "1.5.22.RELEASE";
protected boolean throwsException = false;
private boolean notNullJacksonAnnotation = false;

public SpringCodegen() {
super();
Expand Down Expand Up @@ -591,6 +593,16 @@ public void setReturnContainer(final String returnContainer) {
return objs;
}

@Override
public void setNotNullJacksonAnnotation(boolean notNullJacksonAnnotation) {
this.notNullJacksonAnnotation = notNullJacksonAnnotation;
}

@Override
public boolean isNotNullJacksonAnnotation() {
return notNullJacksonAnnotation;
}

private interface DataTypeAssigner {
void setReturnType(String returnType);
void setReturnContainer(String returnContainer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@
<version>${swagger-core-version}</version>
</dependency>
{{/useOas2}}
{{#notNullJacksonAnnotation}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.1</version>
</dependency>
{{/notNullJacksonAnnotation}}
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/handlebars/Java/pojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
{{#description}}{{#useOas2}}@ApiModel{{/useOas2}}{{^useOas2}}@Schema{{/useOas2}}(description = "{{{description}}}"){{/description}}
{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}

{{#notNullJacksonAnnotation}}@JsonInclude(JsonInclude.Include.NON_NULL){{/notNullJacksonAnnotation}}

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}}{
{{#serializableModel}}
private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@
<artifactId>validation-api</artifactId>
</dependency>
{{/useBeanValidation}}
{{#notNullJacksonAnnotation}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.1</version>
</dependency>
{{/notNullJacksonAnnotation}}

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
{{#notNullJacksonAnnotation}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.1</version>
</dependency>
{{/notNullJacksonAnnotation}}
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,13 @@
</exclusion>
</exclusions>
</dependency>

{{#notNullJacksonAnnotation}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-version}</version>
</dependency>
{{/notNullJacksonAnnotation}}
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/handlebars/JavaSpring/pojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
@ApiModel(description = "{{{description}}}"){{/description}}
{{#useBeanValidation}}@Validated{{/useBeanValidation}}
{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}
{{#notNullJacksonAnnotation}}@JsonInclude(JsonInclude.Include.NON_NULL){{/notNullJacksonAnnotation}}

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}} {
{{#serializableModel}}
private static final long serialVersionUID = 1L;
Expand Down

0 comments on commit dae9dc0

Please sign in to comment.