Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NotNullJacksonAnnotation option #690

Merged
merged 5 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fres - swagger/codegen#6488 - notNullJacksonAnnotation option
  • Loading branch information
gracekarina committed May 22, 2020
commit c23bcc609de02846309290578a167214cb88d47d
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