Kamil Witkowski
Kamil Witkowski

Reputation: 2083

How to show only required parametrs in Swagger-UI "Value" field

I have Post method. Swagger-Ui by default generates example query in JSON with all fields from Class.

I want to show there by default only none-optional fields.

In Swagger-Ui Model i clearly see that:

x (integer, optional),
y (string), 

Some fields are optional and some are not.

How can i achieve that in Spring MVC Spring Boot app ?

My Swagger Config:

@Configuration
@EnableSwagger2
public class SwaggerConfig
{
    @Bean
    public Docket api()
    {

        return new Docket(DocumentationType.SWAGGER_2).

                select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.ant("/api/**")).build();

    }
}

@ApiModelProperty(hidden = true)

hides the field - but i do not want that. I only want to make Swagger generate JSON when i click Model Schema only based on None-optional fields.

Upvotes: 1

Views: 2795

Answers (2)

fehguy
fehguy

Reputation: 6824

This is not a feature of Swagger UI and therefore there's no way to configure it to behave like you're asking. So consider modifying the source code to do what you're asking for in the swagger-ui project

Upvotes: 1

Rae Burawes
Rae Burawes

Reputation: 912

Mark your model with Jackson's @JsonIgnoreProperties e.g.

@JsonIgnoreProperties(value = {"field1", ...})
public class YourModel {

    private String field1;

    private String field2;

    //getters and setters here
}

to test, assuming field1 is null

ObjectMapper objectMapper = new ObjectMapper();
YourModel model = new YourModel();

String strField1 = objectMapper.writeValueAsString(objectMapper);

assertThat(strField1, not(containsString("field1")));

See @JsonIgnoreProperties.

Upvotes: 0

Related Questions