'how to exclude properties of super class when generating open API spec using swagger in java

We have a class which is inherited from third party library class. While generating open API spec using swagger, in schema all the properties of the third party class are also getting displayed along with our class properties.

Is there any way we can exclude all the properties from the third party library class in order to display only our class properties generated schema?



Solution 1:[1]

Include @ApiIgnore over the specific API to hide from Swagger Documentation.

Include @JsonIgnore over the specific property variable which needs to hide from Swagger Documentation.

Solution 2:[2]

I have a workaround for that I added the un-needed properties into the child class and then used the @ApiModelProperty(hidden=true) on them

public class ExternalClass {
    public String externalId;
    ...
}

public class OwnClass extends ExternalClass {

    @ApiModelProperty(hidden=true)
    public String externalId;
    ...
    //our properties
}

Solution 3:[3]

Subclass ExternalClass like this

@Schema(subTypes = { 
    Subclass1.class, Subclass2.class, ...
        })
public class OwnClass extends ExternalClass {
...

where Subclass1.class, Subclass2.class, ... extend from OwnClass

Solution 4:[4]

You should check the generated OpenAPI definition to understand which annotation to use. In my case, the superclass was included in the allOf property.

It was looking something like that:

    User:
      description: User
      allOf:
      - $ref: '#/components/schemas/AbstractResource_User_'
      - required:
        - name
        type: object
        properties:
           ...

To exclude it I had to explicitly add the @Schema(allOf = {}) annotation to the top of my User class definition:

@Schema(allOf = {})
public class User extends AbstractResource<User> {
    ...
}

This excluded the AbstractResource class from the OpenAPI schema.

Solution 5:[5]

You can pass Class type in ignoredParameterTypes method to exclude the classes you don’t want to include.

  @Configuration
  public class SpringFoxConfig {                                    

    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)  
        .select()                                  
        .apis(RequestHandlerSelectors.any())              
        .paths(PathSelectors.any())                          
        .build()
        .ignoredParameterTypes(ExcludeClass.class);                                           
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kirithigan A S
Solution 2 Melad Basilius
Solution 3 Lemme
Solution 4 Oresztesz
Solution 5 Chandra Prakash