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

Optional parameter in web method now raises error, breaks compatibility with the past [SPR-10584] #15213

Closed
spring-projects-issues opened this issue May 24, 2013 · 4 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented May 24, 2013

Alex opened SPR-10584 and commented

A sample method defined as follows:

@RequestMapping(value = "/test")
public String testMethod(Integer n) {
if (n == null)
// do something
else
// do something else
}

stopped working in Spring 3.2.3 (works fine up to 3.2.2) because calling the url like:
http://hostname/context/test?n=
raises a MissingServletRequestParameterException while it should not (it should put a null in n) breaking completely the compatibility with existing apps.


Affects: 3.2.3

Issue Links:

0 votes, 5 watchers

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented May 24, 2013

Rossen Stoyanchev commented

This is most likely the same issue as #15207.

A couple of quick questions. The title says "optional" but in the example it looks like it is required (otherwise it would need to be @RequestParam(required=false)). Also I presume that you have a PropertyEditor or Converter that turns the empty parameter value into null? Neither of these questions changes the fact this is a bug. I'm just confirming.

@spring-projects-issues
Copy link
Collaborator Author

Alex commented

As I understand the specs (and as I always used, like in the provided example), when I declare a parameter without adding the @RequestParam annotation it implies required=false, while if I use it then it implies required=true.

And that's very neat because it leaves the code cleaner.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

The default is required actually if you look in the annotation source:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {

	// ... 

	/**
	 * Whether the parameter is required.
	 * <p>Default is {@code true}, leading to an exception thrown in case
	 * of the parameter missing in the request. Switch this to {@code false}
	 * if you prefer a {@code null} in case of the parameter missing.
	 * <p>Alternatively, provide a {@link #defaultValue() defaultValue},
	 * which implicitly sets this flag to {@code false}.
	 */
	boolean required() default true;


	// ... 

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jun 20, 2013

Rossen Stoyanchev commented

This is fixed with #15207 and will be in 3.2.4 and 4.0.M2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

2 participants