Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Parameter 'required' property is always ignored #252

Closed
glockbender opened this issue Dec 11, 2019 · 7 comments
Closed

@Parameter 'required' property is always ignored #252

glockbender opened this issue Dec 11, 2019 · 7 comments

Comments

@glockbender
Copy link

glockbender commented Dec 11, 2019

Code example:

@GetMapping(produces = [MediaType.APPLICATION_JSON_VALUE])
    fun getOrders(
        @RequestLimit @Parameter(name = "limit", required = false) limit: Int?,
        @RequestOffset @Parameter(required = false) offset: Int,
        @HeaderUserId userId: UUID,
        @HeaderVisitorId visitorId: UUID
    )

But always parameter marks as 'required' in ui and json schema

Снимок экрана 2019-12-11 в 14 54 35

@glockbender glockbender changed the title @Parameter 'required' property always ignored @Parameter 'required' property is always ignored Dec 11, 2019
@springdoc
Copy link
Collaborator

Hi,

This an expected bahaviour. You can have a look at: #14

If you need @GetMapping parameters to be optional, you can use: @RequestParam(required = false)

@glockbender
Copy link
Author

glockbender commented Dec 12, 2019

@springdoc Its really correct behaviour. But why @parameter annotation data ignored? As i understood, swagger-annotations used for implicit declaration or overriding default parser behaviour. If this concept ignored, the usefulness of this library tends to zero. As you see in my code snippet, used my own parameter annotations. This an important case to override default request parameter handling.

@little-fish
Copy link

little-fish commented May 14, 2020

Hi there.
It seems to me that @glockbender 's arguments are right. Explicit swagger annotaion should override default parser behavior. In case Spring Data's Pageable is defined as an parameter of a controller's method, then springdoc marks it as required even though it is not. If @RequestParam(required = false) is specified for given Pageable parameter, Spring doesn't use registered PageableHandlerMethodArgumentResolver so the Pageable object is always null even though the url contains necessary parameters. Without @RequestParam annotation, it works well. There could be some misconfiguration in my project, but I am not able to locate it.
@springdoc Do you think it will be changed in the future?

@bnasslahsen
Copy link
Contributor

Hi @little-fish,

For Pageable, are you declaring using ?

  • @ParameterObject Pageable pageable

@little-fish
Copy link

little-fish commented May 14, 2020

Hi @bnasslahsen,
No, I am not. And that solved my issue! Thank you very much. I missed that in the documentation.

Anyway, I still thinks that @Parameter should override default parser behavior.

Back to my "issue". The problem with nullable Pageable object when using @RequestParam annotation is because the org.springframework.web.method.annotation.RequestParamMethodArgumentResolver is used before any other non-annotation resolvers. See the documentation. This is probably the reason why the Pageable parameter is always used without that annotation (in the Spring documentation and all materials I examined).
What a greate day, I have learnt two new things today ,)

@W1zzard
Copy link

W1zzard commented Jan 25, 2023

Problem described by @glockbender still exist, and @RequestParam is not an option, because it override custom resolver by annotation completely, i need to make parameter with custom annotation to be optional and looks like it is impossible.

@abccbaandy
Copy link

abccbaandy commented May 24, 2024

Hi there. It seems to me that @glockbender 's arguments are right. Explicit swagger annotaion should override default parser behavior. In case Spring Data's Pageable is defined as an parameter of a controller's method, then springdoc marks it as required even though it is not. If @RequestParam(required = false) is specified for given Pageable parameter, Spring doesn't use registered PageableHandlerMethodArgumentResolver so the Pageable object is always null even though the url contains necessary parameters. Without @RequestParam annotation, it works well. There could be some misconfiguration in my project, but I am not able to locate it. @springdoc Do you think it will be changed in the future?

+1 with this
@bnasslahsen
This is another lib which does not allow to use @RequestParam(required = false)
turkraft/springfilter#381

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants