How to hide certain schemas from 'Schemas' section in Swagger UI (OpenAPI 3, Spring Boot)

Viewed 2937

I'm using Springdoc to document my REST API made in Spring Boot. I need to hide some models/schemas from Schemas section in Swagger UI which are used only internally in API, so there is no need to display them in Schemas section.

This is one of the models I'm trying to hide:

@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table
public class EventRole extends AbstractEntity implements Serializable {
    @Column(nullable = false, length = 25)
    private String descriptor;
}

Superclass of model shown above:

@Data
@RequiredArgsConstructor
@SuperBuilder
@MappedSuperclass
public abstract class AbstractEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @CreationTimestamp
    @Column(nullable = false, updatable = false)
    private LocalDateTime creationDate;

    @UpdateTimestamp
    @Column(nullable = false)
    private LocalDateTime modificationDate;
}

Most of annotations in these examples are from JPA or Lombok. To be clear: AbstractEntity is not visible in Schemas section – I included it here just in case.

I've tried so far:

  • using @Hidden annotation on classes I want to hide
  • using @Schema(hidden = true) on these classes
  • adding SpringDocUtils.getConfig().addAnnotationsToIgnore(EventRole.class, AbstractEntity.class); to my OpenAPI bean configuration

Also I've tested @Hidden on controller methods and it works fine. @Schema(hidden = true) hides properly model properties. Unofrtunately, none of them hide whole model. Am I using wrong annotations or there might be other reason why this doesn't work? I'm new to OpenAPI 3.x and Springdoc and it is very likely that I misunderstood something.

2 Answers

Set value for the property springdoc.swagger-ui.defaultModelsExpandDepth=-1 in application.properties as described here.

It takes me for hours to get the solution

Use ignoredParameterTypes in the Docket setting

This one works!

Docket()...
.ignoredParameterTypes(ModleName.class)
Related