Wikimmax
Wikimmax

Reputation: 79

@Schema(hidden=true) and @Hidden Annotations Not Working at Class Level in Springdoc

I'm currently working on a Spring Boot project with Springdoc for API documentation. I have a need to hide certain classes from appearing in the Swagger UI schema. I've tried using the @Schema(hidden=true) and @Hidden annotations from the OpenAPI 3 specification on the class level, but these do not seem to have any effect.

Here's an example of how I'm using these annotations:

@Getter
@Setter
@Entity
@Hidden
@Schema(hidden = true)
@Table(name = "difficulty")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Difficulty {
    
    @Id
    @Column(name = "id", nullable = false)
    private Integer id;

    @Column(name = "name", nullable = false, length = 10)
    private String name;

}

swagger screenshot with class

However, when I use these annotations at the field level within a class, they work as expected, and the annotated fields do not appear in the schema.

@Getter
@Setter
@Entity
@Table(name = "difficulty")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Difficulty {

    @Id
    @Hidden
    @Column(name = "id", nullable = false)
    private Integer id;

    @Column(name = "name", nullable = false, length = 10)
    private String name;

}

field level hidden

here are dependecies

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-security</artifactId>
            <version>1.7.0</version>
        </dependency>

Upvotes: 0

Views: 2208

Answers (2)

Steve Kombo
Steve Kombo

Reputation: 1

Do the following

  1. 1.Remove the springdoc-openapi-security 1.7.0 dependency from your dependency list.Check the official documentation on Spring Security dependency requirements https://springdoc.org/
  2. If the Difficulty class is a field in a response class, apply the @Hidden annotaiton on the field

Upvotes: 0

Amit kumar
Amit kumar

Reputation: 2724

To hide specific classes from appearing in the Swagger UI schema, you can leverage below annotations provided by Swagger :

  1. @ApiIgnore
  2. @ApiOperation(hidden = true)

Both annotations helps us control which classes/methods appear in the Swagger UI schema.

Note : Please add springfox-swagger2 and springfox-swagger-ui in project's build configuration to enable Swagger integration.

Upvotes: 0

Related Questions