question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@RequestParam Field With Default Value Marked Required

See original GitHub issue

If I have a @RequestParam field with a default value like:

@RequestParam(value = "start", defaultValue = "0") int start

Springfox will still mark this field as required in the swagger.json. Per the contract in @RequestParam:

Supplying a default value implicitly sets required() to false.

It looks like the ParameterRequiredReader should also check if the @RequestParam has a defaultValue on it and if so, not set the field to be required.

Happy to submit a PR for this if you wish.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Mumi-aboayacommented, Mar 13, 2018

Please read the first post. Also this is taken from the documentation:

String org.springframework.web.bind.annotation.RequestParam.defaultValue() : “\n\t\t\n\t\t\n\uE000\uE001\uE002\n\t\t\t\t\n” The default value to use as a fallback when the request parameter is not provided or has an empty value. Supplying a default value implicitly sets required to false.

Basically the code should go from: boolean missingDefaultValue = ValueConstants.DEFAULT_NONE.equals(defaultValue) || (defaultValue == null || defaultValue.length() == 0);

to: boolean missingDefaultValue = ValueConstants.DEFAULT_NONE.equals(defaultValue);

Anything other than ValueConstants.DEFAULT_NONE should be considered set by code, hence required should be set to false.

0reactions
Mumi-aboayacommented, Mar 13, 2018

See #2296

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring RequestParam default value Integer - TedBlob
We will discuss setting a default integer value for Spring RequestParam. This annotation binds a request parameter with a method parameter.
Read more >
Is it possible to have empty RequestParam values use the ...
You can keep primitive type by setting default value, in the your case just add "required = false" property: @RequestParam(value = "i", required...
Read more >
Spring @RequestParam Annotation - Baeldung
A detailed guide to Spring's @RequestParam annotation. ... a look at the annotation's attributes: name, value, required, and defaultValue.
Read more >
RequestParam Example in Spring Boot REST
If the @RequestParam annotation is used and the default value is not provided, then this request parameter becomes required by default.
Read more >
How to bind @RequestParam to object in Spring - Dev in Web
@RequestParam (required = false , defaultValue = "0" ) int offset, ... It should have a list of fields which match with request...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found