Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I produce @JsonInclude(value = JsonInclude.Include.NON_NULL) for particular generated POJO from swagger?

My request objects are autogenerated through openapi-generator-maven-plugin based on json interface file. I want to have this annotation @JsonInclude(value = JsonInclude.Include.NON_NULL) on top of one of the autogenerated class's property (not all the classes or the other properties of a class)

following is being autogenerated:

@ApiModel(description = "blabla")
@JsonPropertyOrder({
  Request.JSON_PROPERTY_CONSENT_ID,
})
@JsonTypeName("Request")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2022-02-17T13:14:32.020579400+01:00")
public class Request{
  public static final String JSON_PROPERTY_CONSENT_ID = "consentId";
  private Long consentId;

  @javax.annotation.Nullable
  @ApiModelProperty(value = "blabla")
  @JsonProperty(JSON_PROPERTY_CONSENT_ID)
  @JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)

  public Long getConsentId() {
    return consentId;
  }

  @JsonProperty(JSON_PROPERTY_CONSENT_ID)
  @JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
  public void setConsentId(Long consentId) {
    this.consentId = consentId;
  }

so currently this code is being autogenerated with JsonInclude.Include.USE_DEFAULTS but instead of that i want JsonInclude.Include.NOT_NULL. Can this be achieved ?

i tried using

    spring:
  jackson:
    default-property-inclusion: NON_NULL

in application.yml file but same result with USE_DEFAULTS. I am using spring boot version 2.1.4

like image 735
MiGo Avatar asked Oct 24 '25 16:10

MiGo


1 Answers

I had similar problem. I wanted to generate model class similar to:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Person {

  @JsonInclude(JsonInclude.Include.ALWAYS)
  private String firstName;

  private String middleName;
}

What I did:

I used openapi-generator-maven-plugin version > 6.0.0 that supports x-field-extra-annotation added to specification:

Person:
  type: object
  properties:
    firstName:
      type: string
      x-field-extra-annotation: "@JsonInclude(JsonInclude.Include.ALWAYS)"
    middleName:
      type: string

Then I added additional class annotation by:

<configOptions>
  <additionalModelTypeAnnotations>
    <![CDATA[@JsonInclude(JsonInclude.Include.NON_NULL)]]>
  </additionalModelTypeAnnotations>
</configOptions>

Finally I removed default annotations:

    <plugin>
        <groupId>com.google.code.maven-replacer-plugin</groupId>
        <artifactId>replacer</artifactId>
        <version>${replacer.version}</version>
        <executions>
            <execution>
                <id>removeUnusedAnnotations</id>
                <phase>process-resources</phase>
                <goals>
                    <goal>replace</goal>
                </goals>
                <configuration>
                    <includes>
                        <include>${openapi-generator-maven-plugin.outputBaseDir}/src/**/*.java</include>
                    </includes>
                    <replacements>
                        <replacement>
                            <token>@JsonInclude\(value = JsonInclude.Include.USE_DEFAULTS\)</token>
                            <value />
                        </replacement>
                    </replacements>
                </configuration>
            </execution>
        </executions>
    </plugin>

Last step is dirty workaround, but couldn't find anything better.

like image 86
Karol Bembenek Avatar answered Oct 26 '25 06:10

Karol Bembenek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!