Search code examples
javaspring-bootmavenswagger-uispringdoc-openapi-ui

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


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


Solution

  • 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);
    }