Punter Vicky
Punter Vicky

Reputation: 16992

SpringFox Swagger - Optional and Mandatory fields in model

I used SpringFox library for rest documentation of my spring boot app. When I click on model , all the elements are being returned as optional. Is there a way to display required elements as mandatory? Is there any additional configuration that needs to be added?

Upvotes: 16

Views: 26793

Answers (4)

marisa79
marisa79

Reputation: 23

I was with the same problem but with @etech tips I was able to see the required fields marked in swagger. All I did was upgrading springfox-swagger.version to 2.9.2 (from 2.4.0) and guava.version to 20.0 (from 15) plus the import at the application configuration class. Thank you.

Upvotes: 1

etech
etech

Reputation: 2761

Support for bean validation annotations was added, specifically for @NotNull, @Min, @Max, and @Size in Springfox v2.3.2.

You can place those annotations on any of your API models.

In order to use it add the springfox-bean-validators dependency:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-bean-validators</artifactId>
</dependency>

Add to your application's configuration class:

@Import({springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class})

See: https://springfox.github.io/springfox/docs/current/#springfox-support-for-jsr-303

Upvotes: 3

Ganesh
Ganesh

Reputation: 6127

Yes by default All the fields will be optional. To mark a field as required you can use following annotation.

@ApiModelProperty(required = true)

On the getter method of the field which should be required. This won't show the field as "mandatory". But the optional tag will be removed for this field in the documentation.

Hope this helps.

Upvotes: 24

Hasson
Hasson

Reputation: 1914

Try the a similar code in Swagger Configuration:

    @Bean
public Docket api() {

    List<ResponseMessage> list = new java.util.ArrayList<>();
    list.add(new ResponseMessageBuilder().code(500).message("500 message")
            .responseModel(new ModelRef("JSONResult«string»")).build());
    list.add(new ResponseMessageBuilder().code(401).message("Unauthorized")
            .responseModel(new ModelRef("JSONResult«string»")).build());

    return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any()).build().securitySchemes(Collections.singletonList(securitySchema()))
            .securityContexts(Collections.singletonList(securityContext())).pathMapping("/")
            .directModelSubstitute(LocalDate.class, String.class).genericModelSubstitutes(ResponseEntity.class)
            .alternateTypeRules(newRule(
                    typeResolver.resolve(DeferredResult.class,
                    typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
                    typeResolver.resolve(WildcardType.class)))
            .useDefaultResponseMessages(false).apiInfo(apiInfo()).globalResponseMessage(RequestMethod.GET, list)
            .globalResponseMessage(RequestMethod.POST, list);
}

And in the controller mapping add @RequestBody @Valid MyRequestClass req for example if you are passing objects in the request body, and if you are passing parameters add something like @RequestParam(value = "email", required = true, defaultValue = "") String email

Also, see how in the config code how to reference a class with generic type, i.e "JSONResult«string»" which is referencing JSONResult<String>

Upvotes: 2

Related Questions