@Parameter(required = false) not working in swagger open api v3
Asked Answered
L

3

10

my project is java spring boot 2 with maven . I use springdoc-openapi-ui dependency. problem is @Parameter(required = false) not working on my api params. enter image description here

enter image description here

Lupus answered 28/4, 2021 at 9:9 Comment(5)
Have you tried using the Swagger-specific @ApiParam annotation in addition to the Spring annotations?Jarv
@Parameter isn't a spring annotation, so not sure how that should be handled. You should be using @RequestParam which is a Spring MVC annotation.Nonna
thank you . my problem is with annotation @Parameter that belongs to io.swagger.v3.oas.annotations.Parameter. by default should be required = false, but as in swagger-ui screen shot shows, labeled with * required!Lupus
dear @user991710.@ApiParam belongs to springfox-swagger , i use springdoc-openapi-ui.Lupus
thanks m-deinum problem resolved by add @RequestParam.Lupus
H
16

I don't know how much of swagger annotations Springdoc-openapi supports but according to its own example at PetApiDemo in /findByStatus or /findByTags endpoints you can see by applying @RequestParam(required = false) which is a Spring Annotation! a parameter has become optional.

import org.springframework.web.bind.annotation.RequestParam;
default ResponseEntity<List<Pet>> findPetsByStatus(
        @Parameter(
                explode = Explode.TRUE, 
                name = "status", 
                in = ParameterIn.QUERY, 
                description = "Status values that need to be considered for filter", 
                style = ParameterStyle.FORM, 
                schema = @Schema(
                        type = "string", defaultValue = "available", 
                        allowableValues = {"available", "pending", "sold"}
                )
        )
        @Valid @RequestParam(value = "status", required = false)
        List<String> status) {
    return getDelegate().findPetsByStatus(status);
}
Heavyladen answered 28/4, 2021 at 10:6 Comment(0)
S
2

I analyzed the options reported above and came to the following conclusions

Both @RequestParam(required = false) and @Nullable result in the same behavior in the swagger documentation by removing the *required, however the behavior is different in the application:

  • Using @RequestParam(required = false) makes the method more descriptive but if the object is not passed it will be null. This requires handling to avoid NullpointerException

  • When using @Nullable, if the object is not passed, an instance of the object is still received with all attributes as null, in this case not requiring treatment

In my case I chose to use @Nullable in the interface to keep the default behavior

Shellishellie answered 24/5, 2023 at 14:48 Comment(0)
G
1

I had the same problem. My solution is add jakarta.annotation.@Nullable for non required parameters.

public ResponseEntity<?> getAllByMonthAndYear(
        @Nullable @RequestParam("month") Integer month,
        @Nullable @RequestParam("year") Integer year
) {...
Greenberg answered 13/4, 2023 at 0:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.