3

I'm using swagger to define a contract with my web server. In the swagger file I define the REST endpoints and the request/response objects.

I would like to add the annotation @JsonInclude(Include.NON_NULL) to a field in my swagger class. The swagger class is defined something like this:

 MyObject:
    discriminator: valueType
    required:
      - name
      - description
    properties:
      name:
        type: string
      description:
        type: string
      value:
        type: string
      dbName:
        type: string

When dbName is null, I would like to not return it (not serialize it) so I'd get a response that looks like:

{
   "name": "some_name",
   "description": "my description",
   "value": "some value"
}

If it were a java class I create, it would have been very easy to just define it as:

public class MyObject {
    public String name;
    public String description;
    public String value;

    @JsonInclude(Include.NON_NULL)
    public String dbName;
}

But because that's an auto-generated class, I'm not sure how to make it happen.

Avi
  • 21,182
  • 26
  • 82
  • 121
  • try allowEmptyValue and nullable parameters as described in https://swagger.io/docs/specification/describing-parameters/ (Under section Empty-Valued and Nullable Parameters) – Praveen E Apr 04 '19 at 14:07
  • @PraveenE - Thanks, but it's not that I want to allow it to be null, because today it already returns this field when it's null. I want this field to not be serialized at all when it's null. As I understand from your link, it's not the case (or am I wrong?). – Avi Apr 04 '19 at 14:15
  • @Avi Did you ever find the solution to your question? Thanks – howells699 Aug 14 '19 at 13:15
  • @howells699 - nope :( – Avi Aug 16 '19 at 13:35
  • @Avi Are you using Spring Boot? I have a solution if so. Thanks – howells699 Aug 29 '19 at 10:28
  • @howells699 , can you share the solution for spring boot? – matrixguy May 04 '20 at 07:38
  • Hi @Avi any luck on this since then? I am wanting the same ability to either specify Jackson annotations directly on the generated model classes & its fields or an ability to specify them in the yaml definition...it seems the documentation is just non-existent...i am wondering how one can use swagger with such poor support beyond simple hello world examples :-( – lmk Nov 11 '20 at 23:55
  • @howells699 please share your answer if u have a way...it will help us all.. – lmk Nov 11 '20 at 23:55
  • @matrixguy Did you figure out a way by any chance? – lmk Nov 12 '20 at 01:12

1 Answers1

0

If you're using swagger-codegen plugin then in Maven config you need to add the notNullJacksonAnnotation CLI option and also check if you're using latest plugin version, e.g.:

    <build>
    <plugins>
        <plugin>
            <groupId>io.swagger.codegen.v3</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>3.0.32</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>${project.basedir}/src/main/resources/petstore.yaml</inputSpec>
                        <language>java</language>
                        <library>resttemplate</library>
                        <apiPackage>lt.codex.json.api</apiPackage>
                        <modelPackage>lt.codex.json.model</modelPackage>
                        <invokerPackage>lt.codex.json.handler</invokerPackage>
                        <generateApiTests>false</generateApiTests>
                        <generateApiDocumentation>false</generateApiDocumentation>
                        <generateModelTests>false</generateModelTests>
                        <generateModelDocumentation>false</generateModelDocumentation>
                        <generateSupportingFiles>true</generateSupportingFiles>
                        <configOptions>
                            <interfaceOnly>true</interfaceOnly>
                            <dateLibrary>java8</dateLibrary>
                            <notNullJacksonAnnotation>true</notNullJacksonAnnotation>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
wzona
  • 126
  • 7