Skip to content

Commit 751e59d

Browse files
authored
Merge pull request #10953 from Ravisankar-Challa/master
Add new additional-property ignoreUnknownJacksonAnnotation to add a class level annotation @JsonIgnoreProperties(ignoreUnknown = true)
2 parents e5ef6cb + 0a1b3c2 commit 751e59d

File tree

13 files changed

+306
-2
lines changed

13 files changed

+306
-2
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.regex.Pattern;
1212

1313
import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
14+
import io.swagger.codegen.languages.features.IgnoreUnknownJacksonFeatures;
1415
import io.swagger.models.RefModel;
1516
import io.swagger.models.properties.RefProperty;
1617
import org.apache.commons.lang3.BooleanUtils;
@@ -47,6 +48,7 @@
4748
import io.swagger.models.properties.StringProperty;
4849

4950
import static io.swagger.codegen.languages.features.NotNullAnnotationFeatures.NOT_NULL_JACKSON_ANNOTATION;
51+
import static io.swagger.codegen.languages.features.IgnoreUnknownJacksonFeatures.IGNORE_UNKNOWN_JACKSON_ANNOTATION;
5052

5153
public abstract class AbstractJavaCodegen extends DefaultCodegen implements CodegenConfig {
5254

@@ -95,6 +97,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
9597
protected boolean supportJava6= false;
9698
protected boolean disableHtmlEscaping = false;
9799
private NotNullAnnotationFeatures notNullOption;
100+
private IgnoreUnknownJacksonFeatures ignoreUnknown;
98101

99102
public AbstractJavaCodegen() {
100103
super();
@@ -171,6 +174,10 @@ public AbstractJavaCodegen() {
171174
if(this instanceof NotNullAnnotationFeatures){
172175
cliOptions.add(CliOption.newBoolean(NOT_NULL_JACKSON_ANNOTATION, "adds @JsonInclude(JsonInclude.Include.NON_NULL) annotation to model classes"));
173176
}
177+
if (this instanceof IgnoreUnknownJacksonFeatures){
178+
cliOptions.add(CliOption.newBoolean(IGNORE_UNKNOWN_JACKSON_ANNOTATION,
179+
"adds @JsonIgnoreProperties(ignoreUnknown = true) annotation to model classes"));
180+
}
174181
CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use");
175182
Map<String, String> dateOptions = new HashMap<String, String>();
176183
dateOptions.put("java8", "Java 8 native JSR310 (preferred for jdk 1.8+) - note: this also sets \"" + JAVA8_MODE + "\" to true");
@@ -359,6 +366,17 @@ public void processOpts() {
359366
}
360367
}
361368

369+
if (this instanceof IgnoreUnknownJacksonFeatures) {
370+
ignoreUnknown = (IgnoreUnknownJacksonFeatures)this;
371+
if (additionalProperties.containsKey(IGNORE_UNKNOWN_JACKSON_ANNOTATION)) {
372+
ignoreUnknown.setIgnoreUnknownJacksonAnnotation(convertPropertyToBoolean(IGNORE_UNKNOWN_JACKSON_ANNOTATION));
373+
writePropertyBack(IGNORE_UNKNOWN_JACKSON_ANNOTATION, ignoreUnknown.isIgnoreUnknownJacksonAnnotation());
374+
if (ignoreUnknown.isIgnoreUnknownJacksonAnnotation()) {
375+
importMapping.put("JsonIgnoreProperties", "com.fasterxml.jackson.annotation.JsonIgnoreProperties");
376+
}
377+
}
378+
}
379+
362380
if (fullJavaUtil) {
363381
javaUtilPrefix = "java.util.";
364382
}
@@ -943,6 +961,16 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
943961
}
944962
}
945963
}
964+
if (this instanceof IgnoreUnknownJacksonFeatures) {
965+
if (this instanceof IgnoreUnknownJacksonFeatures) {
966+
ignoreUnknown = (IgnoreUnknownJacksonFeatures)this;
967+
if (additionalProperties.containsKey(IGNORE_UNKNOWN_JACKSON_ANNOTATION)) {
968+
if (ignoreUnknown.isIgnoreUnknownJacksonAnnotation()) {
969+
codegenModel.imports.add("JsonIgnoreProperties");
970+
}
971+
}
972+
}
973+
}
946974
return codegenModel;
947975
}
948976

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.swagger.codegen.languages.features.BeanValidationFeatures;
88
import io.swagger.codegen.languages.features.GzipFeatures;
99
import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
10+
import io.swagger.codegen.languages.features.IgnoreUnknownJacksonFeatures;
1011
import io.swagger.codegen.languages.features.PerformBeanValidationFeatures;
1112

1213
import org.apache.commons.lang3.BooleanUtils;
@@ -20,7 +21,7 @@
2021

2122
public class JavaClientCodegen extends AbstractJavaCodegen
2223
implements BeanValidationFeatures, PerformBeanValidationFeatures,
23-
GzipFeatures, NotNullAnnotationFeatures
24+
GzipFeatures, NotNullAnnotationFeatures, IgnoreUnknownJacksonFeatures
2425
{
2526
static final String MEDIA_TYPE = "mediaType";
2627

@@ -54,6 +55,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
5455
protected boolean useGzipFeature = false;
5556
protected boolean useRuntimeException = false;
5657
private boolean notNullJacksonAnnotation;
58+
private boolean ignoreUnknownJacksonAnnotation = false;
5759

5860
public JavaClientCodegen() {
5961
super();
@@ -620,4 +622,14 @@ public void setNotNullJacksonAnnotation(boolean notNullJacksonAnnotation) {
620622
public boolean isNotNullJacksonAnnotation() {
621623
return notNullJacksonAnnotation;
622624
}
625+
626+
@Override
627+
public void setIgnoreUnknownJacksonAnnotation(boolean ignoreUnknownJacksonAnnotation) {
628+
this.ignoreUnknownJacksonAnnotation = ignoreUnknownJacksonAnnotation;
629+
}
630+
631+
@Override
632+
public boolean isIgnoreUnknownJacksonAnnotation() {
633+
return ignoreUnknownJacksonAnnotation;
634+
}
623635
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.swagger.codegen.*;
66
import io.swagger.codegen.languages.features.BeanValidationFeatures;
77
import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
8+
import io.swagger.codegen.languages.features.IgnoreUnknownJacksonFeatures;
89
import io.swagger.codegen.languages.features.OptionalFeatures;
910
import io.swagger.models.Operation;
1011
import io.swagger.models.Path;
@@ -18,7 +19,8 @@
1819

1920

2021
public class SpringCodegen extends AbstractJavaCodegen
21-
implements BeanValidationFeatures, OptionalFeatures, NotNullAnnotationFeatures {
22+
implements BeanValidationFeatures, OptionalFeatures,
23+
NotNullAnnotationFeatures, IgnoreUnknownJacksonFeatures {
2224
public static final String DEFAULT_LIBRARY = "spring-boot";
2325
public static final String TITLE = "title";
2426
public static final String CONFIG_PACKAGE = "configPackage";
@@ -55,6 +57,7 @@ public class SpringCodegen extends AbstractJavaCodegen
5557
protected boolean openFeign = false;
5658
protected boolean defaultInterfaces = true;
5759
private boolean notNullJacksonAnnotation;
60+
private boolean ignoreUnknownJacksonAnnotation = false;
5861

5962
public SpringCodegen() {
6063
super();
@@ -511,6 +514,16 @@ public boolean isNotNullJacksonAnnotation() {
511514
return notNullJacksonAnnotation;
512515
}
513516

517+
@Override
518+
public void setIgnoreUnknownJacksonAnnotation(boolean ignoreUnknownJacksonAnnotation) {
519+
this.ignoreUnknownJacksonAnnotation = ignoreUnknownJacksonAnnotation;
520+
}
521+
522+
@Override
523+
public boolean isIgnoreUnknownJacksonAnnotation() {
524+
return ignoreUnknownJacksonAnnotation;
525+
}
526+
514527
private interface DataTypeAssigner {
515528
void setReturnType(String returnType);
516529
void setReturnContainer(String returnContainer);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.swagger.codegen.languages.features;
2+
3+
public interface IgnoreUnknownJacksonFeatures {
4+
// Language supports generating JsonIgnoreProperties(ignoreUnknown = true)
5+
String IGNORE_UNKNOWN_JACKSON_ANNOTATION = "ignoreUnknownJacksonAnnotation";
6+
7+
void setIgnoreUnknownJacksonAnnotation(boolean ignoreUnknownJacksonAnnotation);
8+
9+
boolean isIgnoreUnknownJacksonAnnotation();
10+
}

modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/pom.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@
243243
<version>2.11.4</version>
244244
</dependency>
245245
{{/notNullJacksonAnnotation}}
246+
{{^notNullJacksonAnnotation}}
247+
{{#ignoreUnknownJacksonAnnotation}}
248+
<dependency>
249+
<groupId>com.fasterxml.jackson.core</groupId>
250+
<artifactId>jackson-annotations</artifactId>
251+
<version>2.11.4</version>
252+
</dependency>
253+
{{/ignoreUnknownJacksonAnnotation}}
254+
{{/notNullJacksonAnnotation}}
246255
{{#performBeanValidation}}
247256
<!-- Bean Validation Impl. used to perform BeanValidation -->
248257
<dependency>

modules/swagger-codegen/src/main/resources/Java/pojo.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
{{#notNullJacksonAnnotation}}
77
@JsonInclude(JsonInclude.Include.NON_NULL)
88
{{/notNullJacksonAnnotation}}
9+
{{#ignoreUnknownJacksonAnnotation}}
10+
@JsonIgnoreProperties(ignoreUnknown = true)
11+
{{/ignoreUnknownJacksonAnnotation}}
912
public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcelableModel}}implements Parcelable {{#serializableModel}}, Serializable {{/serializableModel}}{{/parcelableModel}}{{^parcelableModel}}{{#serializableModel}}implements Serializable {{/serializableModel}}{{/parcelableModel}}{
1013
{{#serializableModel}}
1114
private static final long serialVersionUID = 1L;

modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-boot/pom.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@
102102
<version>2.11.4</version>
103103
</dependency>
104104
{{/notNullJacksonAnnotation}}
105+
{{^notNullJacksonAnnotation}}
106+
{{#ignoreUnknownJacksonAnnotation}}
107+
<dependency>
108+
<groupId>com.fasterxml.jackson.core</groupId>
109+
<artifactId>jackson-annotations</artifactId>
110+
<version>2.11.4</version>
111+
</dependency>
112+
{{/ignoreUnknownJacksonAnnotation}}
113+
{{/notNullJacksonAnnotation}}
105114
<dependency>
106115
<groupId>org.springframework.boot</groupId>
107116
<artifactId>spring-boot-starter-test</artifactId>

modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-cloud/pom.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@
100100
<version>2.11.4</version>
101101
</dependency>
102102
{{/notNullJacksonAnnotation}}
103+
{{^notNullJacksonAnnotation}}
104+
{{#ignoreUnknownJacksonAnnotation}}
105+
<dependency>
106+
<groupId>com.fasterxml.jackson.core</groupId>
107+
<artifactId>jackson-annotations</artifactId>
108+
<version>2.11.4</version>
109+
</dependency>
110+
{{/ignoreUnknownJacksonAnnotation}}
111+
{{/notNullJacksonAnnotation}}
103112
<dependency>
104113
<groupId>org.springframework.boot</groupId>
105114
<artifactId>spring-boot-starter-test</artifactId>

modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-mvc/pom.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@
168168
<version>${jackson-version}</version>
169169
</dependency>
170170
{{/notNullJacksonAnnotation}}
171+
{{^notNullJacksonAnnotation}}
172+
{{#ignoreUnknownJacksonAnnotation}}
173+
<dependency>
174+
<groupId>com.fasterxml.jackson.core</groupId>
175+
<artifactId>jackson-annotations</artifactId>
176+
<version>2.11.4</version>
177+
</dependency>
178+
{{/ignoreUnknownJacksonAnnotation}}
179+
{{/notNullJacksonAnnotation}}
171180
<dependency>
172181
<groupId>org.testng</groupId>
173182
<artifactId>testng</artifactId>

modules/swagger-codegen/src/main/resources/JavaSpring/pojo.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
{{#notNullJacksonAnnotation}}
1616
@JsonInclude(JsonInclude.Include.NON_NULL)
1717
{{/notNullJacksonAnnotation}}
18+
{{#ignoreUnknownJacksonAnnotation}}
19+
@JsonIgnoreProperties(ignoreUnknown = true)
20+
{{/ignoreUnknownJacksonAnnotation}}
1821
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
1922
{{#serializableModel}}
2023
private static final long serialVersionUID = 1L;

0 commit comments

Comments
 (0)