MvcUriComponentsBuilder respects optional params

Issue: SPR-14405
This commit is contained in:
Rossen Stoyanchev 2016-06-27 16:02:54 -04:00
parent e38623df87
commit 2cdcf752ba
2 changed files with 29 additions and 11 deletions

View File

@ -211,6 +211,11 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
parameter.getParameterName() : requestParam.name());
if (value == null) {
if (requestParam != null) {
if (!requestParam.required() || !requestParam.defaultValue().equals(ValueConstants.DEFAULT_NONE)) {
return;
}
}
builder.queryParam(name);
}
else if (value instanceof Collection) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@ -221,6 +221,14 @@ public class MvcUriComponentsBuilderTests {
assertThat(uriComponents.toUriString(), is("http://localhost/input"));
}
@Test // SPR-14405
public void testFromMappingNameWithOptionalParam() throws Exception {
UriComponents uriComponents = fromMethodName(ControllerWithMethods.class,
"methodWithOptionalParam", new Object[] {null}).build();
assertThat(uriComponents.toUriString(), is("http://localhost/something/optional-param"));
}
@Test
public void testFromMethodCall() {
UriComponents uriComponents = fromMethodCall(on(ControllerWithMethods.class).myMethod(null)).build();
@ -369,25 +377,25 @@ public class MvcUriComponentsBuilderTests {
}
class PersonControllerImpl implements PersonController {
private class PersonControllerImpl implements PersonController {
}
@RequestMapping("/people/{id}/addresses")
static class PersonsAddressesController {
private static class PersonsAddressesController {
@RequestMapping("/{country}")
public HttpEntity<Void> getAddressesForCountry(@PathVariable String country) {
HttpEntity<Void> getAddressesForCountry(@PathVariable String country) {
return null;
}
}
@RequestMapping({ "/persons", "/people" })
class InvalidController {
private class InvalidController {
}
class UnmappedController {
private class UnmappedController {
@RequestMapping
public void unmappedMethod() {
@ -424,15 +432,20 @@ public class MvcUriComponentsBuilderTests {
@RequestParam List<Integer> items, @RequestParam Integer limit) {
return null;
}
@RequestMapping("/optional-param")
HttpEntity<Void> methodWithOptionalParam(@RequestParam(defaultValue = "") String q) {
return null;
}
}
@RequestMapping("/extended")
@RequestMapping("/extended") @SuppressWarnings("WeakerAccess")
static class ExtendedController extends ControllerWithMethods {
}
@RequestMapping("/user/{userId}/contacts")
static class UserContactController {
private static class UserContactController {
@RequestMapping("/create")
public String showCreate(@PathVariable Integer userId) {
@ -445,7 +458,7 @@ public class MvcUriComponentsBuilderTests {
abstract T get(ID id);
}
static class PersonCrudController extends AbstractCrudController<Person, Long> {
private static class PersonCrudController extends AbstractCrudController<Person, Long> {
@RequestMapping(path = "/{id}", method = RequestMethod.GET)
public Person get(@PathVariable Long id) {
@ -454,7 +467,7 @@ public class MvcUriComponentsBuilderTests {
}
@Controller
static class MetaAnnotationController {
private static class MetaAnnotationController {
@RequestMapping
public void handle() {
@ -472,7 +485,7 @@ public class MvcUriComponentsBuilderTests {
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface PostJson {
private @interface PostJson {
String[] path() default {};
}