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

Fix #11899: Add ignoreUnknownJacksonAnnotation configuration property #1312

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-generators</artifactId>
<version>1.0.52</version>
<version>1.0.53-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.swagger.codegen.v3.generators.features;

public interface IgnoreUnknownJacksonFeatures {
// Language supports generating JsonIgnoreProperties(ignoreUnknown = true)
String IGNORE_UNKNOWN_JACKSON_ANNOTATION = "ignoreUnknownJacksonAnnotation";

void setIgnoreUnknownJacksonAnnotation(boolean ignoreUnknownJacksonAnnotation);

boolean isIgnoreUnknownJacksonAnnotation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,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.IgnoreUnknownJacksonFeatures.IGNORE_UNKNOWN_JACKSON_ANNOTATION;
import static io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures.NOT_NULL_JACKSON_ANNOTATION;
import static io.swagger.codegen.v3.generators.handlebars.ExtensionHelper.getBooleanValue;

Expand All @@ -14,6 +15,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.IgnoreUnknownJacksonFeatures;
import io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures;
import io.swagger.codegen.v3.generators.handlebars.java.JavaHelper;
import io.swagger.codegen.v3.utils.URLPathUtil;
Expand Down Expand Up @@ -100,6 +102,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegenConfig {
protected boolean jakarta = false;
private NotNullAnnotationFeatures notNullOption;
protected boolean useNullableForNotNull = true;
private IgnoreUnknownJacksonFeatures ignoreUnknown;

public AbstractJavaCodegen() {
super();
Expand Down Expand Up @@ -172,6 +175,10 @@ public AbstractJavaCodegen() {
if(this instanceof NotNullAnnotationFeatures){
cliOptions.add(CliOption.newBoolean(NOT_NULL_JACKSON_ANNOTATION, "adds @JsonInclude(JsonInclude.Include.NON_NULL) annotation to model classes"));
}
if (this instanceof IgnoreUnknownJacksonFeatures){
cliOptions.add(CliOption.newBoolean(IGNORE_UNKNOWN_JACKSON_ANNOTATION,
"adds @JsonIgnoreProperties(ignoreUnknown = true) 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 @@ -394,6 +401,17 @@ public void processOpts() {
}
}

if (this instanceof IgnoreUnknownJacksonFeatures) {
ignoreUnknown = (IgnoreUnknownJacksonFeatures)this;
if (additionalProperties.containsKey(IGNORE_UNKNOWN_JACKSON_ANNOTATION)) {
ignoreUnknown.setIgnoreUnknownJacksonAnnotation(convertPropertyToBoolean(IGNORE_UNKNOWN_JACKSON_ANNOTATION));
writePropertyBack(IGNORE_UNKNOWN_JACKSON_ANNOTATION, ignoreUnknown.isIgnoreUnknownJacksonAnnotation());
if (ignoreUnknown.isIgnoreUnknownJacksonAnnotation()) {
importMapping.put("JsonIgnoreProperties", "com.fasterxml.jackson.annotation.JsonIgnoreProperties");
}
}
}

if (additionalProperties.containsKey(USE_NULLABLE_FOR_NOTNULL)) {
this.setUseNullableForNotnull(Boolean.valueOf(additionalProperties.get(USE_NULLABLE_FOR_NOTNULL).toString()));
}
Expand Down Expand Up @@ -1008,6 +1026,16 @@ public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> al
}
}
}
if (this instanceof IgnoreUnknownJacksonFeatures) {
if (this instanceof IgnoreUnknownJacksonFeatures) {
ignoreUnknown = (IgnoreUnknownJacksonFeatures)this;
if (additionalProperties.containsKey(IGNORE_UNKNOWN_JACKSON_ANNOTATION)) {
if (ignoreUnknown.isIgnoreUnknownJacksonAnnotation()) {
codegenModel.imports.add("JsonIgnoreProperties");
}
}
}
}
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.IgnoreUnknownJacksonFeatures;
import io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures;
import io.swagger.codegen.v3.generators.features.PerformBeanValidationFeatures;
import io.swagger.codegen.v3.generators.util.OpenAPIUtil;
Expand All @@ -33,7 +34,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, NotNullAnnotationFeatures {
public class JavaClientCodegen extends AbstractJavaCodegen implements BeanValidationFeatures, PerformBeanValidationFeatures, GzipFeatures, NotNullAnnotationFeatures, IgnoreUnknownJacksonFeatures {
static final String MEDIA_TYPE = "mediaType";

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

private boolean ignoreUnknownJacksonAnnotation = false;

public JavaClientCodegen() {
super();
Expand Down Expand Up @@ -641,4 +642,14 @@ public void setNotNullJacksonAnnotation(boolean notNullJacksonAnnotation) {
public boolean isNotNullJacksonAnnotation() {
return notNullJacksonAnnotation;
}

@Override
public void setIgnoreUnknownJacksonAnnotation(boolean ignoreUnknownJacksonAnnotation) {
this.ignoreUnknownJacksonAnnotation = ignoreUnknownJacksonAnnotation;
}

@Override
public boolean isIgnoreUnknownJacksonAnnotation() {
return ignoreUnknownJacksonAnnotation;
}
}
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.IgnoreUnknownJacksonFeatures;
import io.swagger.codegen.v3.generators.features.NotNullAnnotationFeatures;
import io.swagger.codegen.v3.generators.features.OptionalFeatures;
import io.swagger.codegen.v3.generators.util.OpenAPIUtil;
Expand Down Expand Up @@ -45,7 +46,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, NotNullAnnotationFeatures {
public class SpringCodegen extends AbstractJavaCodegen implements BeanValidationFeatures, OptionalFeatures, NotNullAnnotationFeatures, IgnoreUnknownJacksonFeatures {
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 @@ -98,6 +99,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
protected boolean throwsException = false;
private boolean notNullJacksonAnnotation = false;
protected String validationMode = "strict";
private boolean ignoreUnknownJacksonAnnotation = false;

public SpringCodegen() {
super();
Expand Down Expand Up @@ -701,6 +703,16 @@ public boolean isNotNullJacksonAnnotation() {
return notNullJacksonAnnotation;
}

@Override
public void setIgnoreUnknownJacksonAnnotation(boolean ignoreUnknownJacksonAnnotation) {
this.ignoreUnknownJacksonAnnotation = ignoreUnknownJacksonAnnotation;
}

@Override
public boolean isIgnoreUnknownJacksonAnnotation() {
return ignoreUnknownJacksonAnnotation;
}

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 @@ -206,6 +206,13 @@
<version>2.10.1</version>
</dependency>
{{/notNullJacksonAnnotation}}
{{#ignoreUnknownJacksonAnnotation}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.1</version>
</dependency>
{{/ignoreUnknownJacksonAnnotation}}
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/handlebars/Java/pojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
{{#notNullJacksonAnnotation}}
@JsonInclude(JsonInclude.Include.NON_NULL)
{{/notNullJacksonAnnotation}}

{{#ignoreUnknownJacksonAnnotation}}
@JsonIgnoreProperties(ignoreUnknown = true)
{{/ignoreUnknownJacksonAnnotation}}
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}}{{#@first}}, {{/@first}}{{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 @@ -155,7 +155,13 @@
<version>2.10.1</version>
</dependency>
{{/notNullJacksonAnnotation}}

{{#ignoreUnknownJacksonAnnotation}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.1</version>
</dependency>
{{/ignoreUnknownJacksonAnnotation}}
{{^useOas2}}
<dependency>
<groupId>org.springframework.plugin</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,13 @@
<version>2.10.1</version>
</dependency>
{{/notNullJacksonAnnotation}}

{{#ignoreUnknownJacksonAnnotation}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.1</version>
</dependency>
{{/ignoreUnknownJacksonAnnotation}}
{{#jakarta}}
<dependency>
<groupId>jakarta.servlet</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@
<version>${jackson-version}</version>
</dependency>
{{/notNullJacksonAnnotation}}
{{#ignoreUnknownJacksonAnnotation}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-version}</version>
</dependency>
{{/ignoreUnknownJacksonAnnotation}}
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/handlebars/JavaSpring/pojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{{#useBeanValidation}}{{#isStrictValidation}}@NotUndefined{{/isStrictValidation}}{{#isLooseValidation}}@NotUndefined{{/isLooseValidation}}{{/useBeanValidation}}
{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}
{{#notNullJacksonAnnotation}}@JsonInclude(JsonInclude.Include.NON_NULL){{/notNullJacksonAnnotation}}

{{#ignoreUnknownJacksonAnnotation}}@JsonIgnoreProperties(ignoreUnknown = true){{/ignoreUnknownJacksonAnnotation}}
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