Search code examples
javaspring-bootswagger-uiopenapi

SwaggerUI: show/hide a hidden schema field


I am trying to set-up an OpenApi documentation within a project. Currently, I am documenting an endpoint which receives a DTO and returns a DTO as a response.

My entity DTO has a private field hidden (via @Schema annotation: the id). I used this anno. for not having that field included in the example JSON for a POST..

All good so far, but I want to have the id displayed in the response example value JSON within swagger-ui.

The example is same for response and request: { "title": "string", "subtitle": "string", "publisher": "string", "publishedDate": "string", "description": "string", "pageCount": 0, "language": "string", "previewLink": "string", "numberOfItems": 0 }

But for the sake of documentation of the "response", I want the below JSON to contain also the hidden schema field. Can I do this somehow? [Image example] (https://i.sstatic.net/PsXfJ.jpg)

My approach towards was the following:

@ApiResponse(description = "Successful response", responseCode = "200",content = @Content(schema = @Schema(implementation = BookDto.class)))

But I am expecting to be able without making a custom @Content schema, to include in the API response documentation example the hidden field.


Solution

  • Ok I found a solution for my encounter.

    Instead of using @Schema(hidden=true) for the id field within my DTO. I switched to using: @Schema(accessMode = Schema.AccessMode.READ_ONLY).

    Which changed the behavior of Swagger as I wanted.