Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;
import java.util.regex.Pattern;

import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -43,6 +44,7 @@
import io.swagger.models.properties.Property;
import io.swagger.models.properties.StringProperty;

import static io.swagger.codegen.languages.features.NotNullAnnotationFeatures.NOT_NULL_JACKSON_ANNOTATION;

public abstract class AbstractJavaCodegen extends DefaultCodegen implements CodegenConfig {

Expand Down Expand Up @@ -88,6 +90,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
protected String modelDocPath = "docs/";
protected boolean supportJava6= false;
protected boolean disableHtmlEscaping = false;
private NotNullAnnotationFeatures notNullOption;

public AbstractJavaCodegen() {
super();
Expand Down Expand Up @@ -161,7 +164,9 @@ public AbstractJavaCodegen() {
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"));
cliOptions.add(new CliOption("hideGenerationTimestamp", "hides the timestamp when files were generated"));
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)"));

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 @@ -338,6 +343,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 @@ -907,6 +923,16 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel);
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 @@ -6,6 +6,7 @@
import io.swagger.codegen.*;
import io.swagger.codegen.languages.features.BeanValidationFeatures;
import io.swagger.codegen.languages.features.GzipFeatures;
import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
import io.swagger.codegen.languages.features.PerformBeanValidationFeatures;

import org.apache.commons.lang3.BooleanUtils;
Expand All @@ -19,7 +20,7 @@

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

Expand Down Expand Up @@ -52,7 +53,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
protected boolean performBeanValidation = false;
protected boolean useGzipFeature = false;
protected boolean useRuntimeException = false;

private boolean notNullJacksonAnnotation;

public JavaClientCodegen() {
super();
Expand Down Expand Up @@ -611,4 +612,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 @@ -4,6 +4,7 @@
import com.samskivert.mustache.Template;
import io.swagger.codegen.*;
import io.swagger.codegen.languages.features.BeanValidationFeatures;
import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
import io.swagger.codegen.languages.features.OptionalFeatures;
import io.swagger.models.Operation;
import io.swagger.models.Path;
Expand All @@ -17,7 +18,7 @@


public class SpringCodegen extends AbstractJavaCodegen
implements BeanValidationFeatures, OptionalFeatures {
implements BeanValidationFeatures, OptionalFeatures, NotNullAnnotationFeatures {
public static final String DEFAULT_LIBRARY = "spring-boot";
public static final String TITLE = "title";
public static final String CONFIG_PACKAGE = "configPackage";
Expand Down Expand Up @@ -53,6 +54,7 @@ public class SpringCodegen extends AbstractJavaCodegen
protected boolean useOptional = false;
protected boolean openFeign = false;
protected boolean defaultInterfaces = true;
private boolean notNullJacksonAnnotation;

public SpringCodegen() {
super();
Expand Down Expand Up @@ -499,6 +501,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
@@ -0,0 +1,10 @@
package io.swagger.codegen.languages.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 @@ -236,6 +236,13 @@
<scope>provided</scope>
</dependency>
{{/useBeanValidation}}
{{#notNullJacksonAnnotation}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.1</version>
</dependency>
{{/notNullJacksonAnnotation}}
{{#performBeanValidation}}
<!-- Bean Validation Impl. used to perform BeanValidation -->
<dependency>
Expand Down
3 changes: 3 additions & 0 deletions modules/swagger-codegen/src/main/resources/Java/pojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*/{{#description}}
@ApiModel(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}}{{/parcelableModel}}{{^parcelableModel}}{{#serializableModel}}implements Serializable {{/serializableModel}}{{/parcelableModel}}{
{{#serializableModel}}
private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,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>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@
<scope>provided</scope>
</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>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@
<scope>provided</scope>
</dependency>
{{/useBeanValidation}}
{{#notNullJacksonAnnotation}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-version}</version>
</dependency>
{{/notNullJacksonAnnotation}}
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
@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{{/serializableModel}} {
{{#serializableModel}}
private static final long serialVersionUID = 1L;
Expand Down
Loading