#331 - Fixed handling of @RequestParam with default value set.

ControllerLinkBuilderFactory now checks the default value of an @RequestParam annotated handler method parameter before triggering strict validation.

Original pull request: #332.
This commit is contained in:
Oemer Yildiz
2015-04-13 14:41:08 +02:00
committed by Oliver Gierke
parent 22488e0c05
commit 312fa3ead9
2 changed files with 22 additions and 2 deletions

View File

@@ -41,6 +41,7 @@ import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ValueConstants;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriTemplate;
@@ -53,6 +54,7 @@ import org.springframework.web.util.UriTemplate;
* @author Dietrich Schulten
* @author Kamill Sokol
* @author Ross Turner
* @author Oemer Yildiz
*/
public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<ControllerLinkBuilder> {
@@ -233,7 +235,8 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
protected Object verifyParameterValue(MethodParameter parameter, Object value) {
RequestParam annotation = parameter.getParameterAnnotation(RequestParam.class);
return annotation.required() ? super.verifyParameterValue(parameter, value) : value;
return annotation.required() && annotation.defaultValue().equals(ValueConstants.DEFAULT_NONE) ? super
.verifyParameterValue(parameter, value) : value;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@ import org.springframework.web.util.UriComponentsBuilder;
* @author Oliver Gierke
* @author Dietrich Schulten
* @author Kamill Sokol
* @author Oemer Yildiz
*/
public class ControllerLinkBuilderUnitTest extends TestUtils {
@@ -423,6 +424,17 @@ public class ControllerLinkBuilderUnitTest extends TestUtils {
assertThat(link.getHref(), startsWith("bar://"));
}
/**
* @see #331
*/
@Test
public void linksToMethodWithRequestParamImplicitlySetToFalse() {
Link link = linkTo(methodOn(ControllerWithMethods.class).methodForOptionalSizeWithDefaultValue(null)).withSelfRel();
assertThat(link.getHref(), endsWith("/bar"));
}
private static UriComponents toComponents(Link link) {
return UriComponentsBuilder.fromUriString(link.getHref()).build();
}
@@ -494,6 +506,11 @@ public class ControllerLinkBuilderUnitTest extends TestUtils {
HttpEntity<Void> methodForOptionalNextPage(@RequestParam(required = false) Integer offset) {
return null;
}
@RequestMapping(value = "/bar")
HttpEntity<Void> methodForOptionalSizeWithDefaultValue(@RequestParam(defaultValue = "10") Integer size) {
return null;
}
}
@RequestMapping("/parent")