7

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.

Paweł Raglis
  • 91
  • 2
  • 5
  • A possible reason could be one of the API is using the model, thus even if you try to hide it, it's still resolved cause the endpoint needs it. Can you check your controller to see if the model is being used or not. – Debargha Roy Jan 27 '21 at 10:26
  • @DebarghaRoy I'm sure that the model I'm trying to hide isn't used in any controller. I double checked it after reading your comment. – Paweł Raglis Jan 27 '21 at 13:51
  • Can you share a reproducible example, maybe on GitHub, BitBucket etc.? – Debargha Roy Jan 27 '21 at 13:55
  • @Paweł Raglis, you should add reproducible example as proposed by Debargha Roy. – brianbro May 23 '21 at 14:34

2 Answers2

2

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

ajesh
  • 310
  • 5
  • 7
1

It takes me for hours to get the solution

Use ignoredParameterTypes in the Docket setting

This one works!

Docket()...
.ignoredParameterTypes(ModleName.class)
W Kenny
  • 1,855
  • 22
  • 33