Springfox Swagger-UI - hide or filter fields for different requests not using additional models
Asked Answered
N

1

1

Is that possible to filter (hide) specific fields for different requests using Spring Boot and Springfox Swagger 2.9.2?

I got model "Genres":

{
  "id": 0,
  "type": "string"
}

And another one "Book" with the first included:

{
  "id": 0,
  "genres": [
    {
      "id": 0,
      "type": "string"
    }
  ],
  "isbn": "string",
  "title": "string",
  "author": "string",
  "publishedYear": "string",
  "price": "string",
  "quantity": 0
}

I want to show all fields for the first model "Genres" in Swagger-UI GET /genres but hide "type": "string" for "Book" model POST /books since it's not mandatory and basically duplicate info about book genre.

Is there any possibility to do that? I don't want to use DTO or any separate models to do this, but to my astonishment, it's seems there is no other choice since Swagger developers didn't leave such a possibility.

I tried:

@ApiModelProperty(readOnly = true)
private String type;

And:

@ApiModelProperty(accessMode = ApiModelProperty.AccessMode.READ_ONLY)
private String type;

But it didn't help.

@JsonIgnore hides field for every request which is not what I'm looking for:

@JsonIgnore
private String type;

I viewed this and this, but it seems like there is no solution. Do I miss something? Googling this issue I found a lot of people who look for this feature for years :(

Node answered 29/5, 2020 at 8:41 Comment(0)
I
0

This is an old question, but using SpringFox 3 I've got this to work by annotating my field @JsonProperty(access = JsonProperty.Access.READ_ONLY).

Impure answered 21/9, 2021 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.