Skip to content

Commit

Permalink
Merge pull request #10953 from Ravisankar-Challa/master
Browse files Browse the repository at this point in the history
Add new additional-property ignoreUnknownJacksonAnnotation to add a class level annotation @JsonIgnoreProperties(ignoreUnknown = true)
  • Loading branch information
HugoMario authored Apr 7, 2021
2 parents e5ef6cb + 0a1b3c2 commit 751e59d
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.regex.Pattern;

import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
import io.swagger.codegen.languages.features.IgnoreUnknownJacksonFeatures;
import io.swagger.models.RefModel;
import io.swagger.models.properties.RefProperty;
import org.apache.commons.lang3.BooleanUtils;
Expand Down Expand Up @@ -47,6 +48,7 @@
import io.swagger.models.properties.StringProperty;

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

public abstract class AbstractJavaCodegen extends DefaultCodegen implements CodegenConfig {

Expand Down Expand Up @@ -95,6 +97,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
protected boolean supportJava6= false;
protected boolean disableHtmlEscaping = false;
private NotNullAnnotationFeatures notNullOption;
private IgnoreUnknownJacksonFeatures ignoreUnknown;

public AbstractJavaCodegen() {
super();
Expand Down Expand Up @@ -171,6 +174,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 @@ -359,6 +366,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 (fullJavaUtil) {
javaUtilPrefix = "java.util.";
}
Expand Down Expand Up @@ -943,6 +961,16 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
}
}
}
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 @@ -7,6 +7,7 @@
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.IgnoreUnknownJacksonFeatures;
import io.swagger.codegen.languages.features.PerformBeanValidationFeatures;

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

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

Expand Down Expand Up @@ -54,6 +55,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
protected boolean useGzipFeature = false;
protected boolean useRuntimeException = false;
private boolean notNullJacksonAnnotation;
private boolean ignoreUnknownJacksonAnnotation = false;

public JavaClientCodegen() {
super();
Expand Down Expand Up @@ -620,4 +622,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 @@ -5,6 +5,7 @@
import io.swagger.codegen.*;
import io.swagger.codegen.languages.features.BeanValidationFeatures;
import io.swagger.codegen.languages.features.NotNullAnnotationFeatures;
import io.swagger.codegen.languages.features.IgnoreUnknownJacksonFeatures;
import io.swagger.codegen.languages.features.OptionalFeatures;
import io.swagger.models.Operation;
import io.swagger.models.Path;
Expand All @@ -18,7 +19,8 @@


public class SpringCodegen extends AbstractJavaCodegen
implements BeanValidationFeatures, OptionalFeatures, NotNullAnnotationFeatures {
implements BeanValidationFeatures, OptionalFeatures,
NotNullAnnotationFeatures, IgnoreUnknownJacksonFeatures {
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 @@ -55,6 +57,7 @@ public class SpringCodegen extends AbstractJavaCodegen
protected boolean openFeign = false;
protected boolean defaultInterfaces = true;
private boolean notNullJacksonAnnotation;
private boolean ignoreUnknownJacksonAnnotation = false;

public SpringCodegen() {
super();
Expand Down Expand Up @@ -511,6 +514,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
@@ -0,0 +1,10 @@
package io.swagger.codegen.languages.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 @@ -243,6 +243,15 @@
<version>2.11.4</version>
</dependency>
{{/notNullJacksonAnnotation}}
{{^notNullJacksonAnnotation}}
{{#ignoreUnknownJacksonAnnotation}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.4</version>
</dependency>
{{/ignoreUnknownJacksonAnnotation}}
{{/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 @@ -6,6 +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}}{{/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 @@ -102,6 +102,15 @@
<version>2.11.4</version>
</dependency>
{{/notNullJacksonAnnotation}}
{{^notNullJacksonAnnotation}}
{{#ignoreUnknownJacksonAnnotation}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.4</version>
</dependency>
{{/ignoreUnknownJacksonAnnotation}}
{{/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 @@ -100,6 +100,15 @@
<version>2.11.4</version>
</dependency>
{{/notNullJacksonAnnotation}}
{{^notNullJacksonAnnotation}}
{{#ignoreUnknownJacksonAnnotation}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.4</version>
</dependency>
{{/ignoreUnknownJacksonAnnotation}}
{{/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 @@ -168,6 +168,15 @@
<version>${jackson-version}</version>
</dependency>
{{/notNullJacksonAnnotation}}
{{^notNullJacksonAnnotation}}
{{#ignoreUnknownJacksonAnnotation}}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.4</version>
</dependency>
{{/ignoreUnknownJacksonAnnotation}}
{{/notNullJacksonAnnotation}}
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
{{#notNullJacksonAnnotation}}
@JsonInclude(JsonInclude.Include.NON_NULL)
{{/notNullJacksonAnnotation}}
{{#ignoreUnknownJacksonAnnotation}}
@JsonIgnoreProperties(ignoreUnknown = true)
{{/ignoreUnknownJacksonAnnotation}}
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
{{#serializableModel}}
private static final long serialVersionUID = 1L;
Expand Down
Loading

0 comments on commit 751e59d

Please sign in to comment.