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()); parameter.getParameterName() : requestParam.name());
if (value == null) { if (value == null) {
if (requestParam != null) {
if (!requestParam.required() || !requestParam.defaultValue().equals(ValueConstants.DEFAULT_NONE)) {
return;
}
}
builder.queryParam(name); builder.queryParam(name);
} }
else if (value instanceof Collection) { 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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")); 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 @Test
public void testFromMethodCall() { public void testFromMethodCall() {
UriComponents uriComponents = fromMethodCall(on(ControllerWithMethods.class).myMethod(null)).build(); 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") @RequestMapping("/people/{id}/addresses")
static class PersonsAddressesController { private static class PersonsAddressesController {
@RequestMapping("/{country}") @RequestMapping("/{country}")
public HttpEntity<Void> getAddressesForCountry(@PathVariable String country) { HttpEntity<Void> getAddressesForCountry(@PathVariable String country) {
return null; return null;
} }
} }
@RequestMapping({ "/persons", "/people" }) @RequestMapping({ "/persons", "/people" })
class InvalidController { private class InvalidController {
} }
class UnmappedController { private class UnmappedController {
@RequestMapping @RequestMapping
public void unmappedMethod() { public void unmappedMethod() {
@ -424,15 +432,20 @@ public class MvcUriComponentsBuilderTests {
@RequestParam List<Integer> items, @RequestParam Integer limit) { @RequestParam List<Integer> items, @RequestParam Integer limit) {
return null; 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 { static class ExtendedController extends ControllerWithMethods {
} }
@RequestMapping("/user/{userId}/contacts") @RequestMapping("/user/{userId}/contacts")
static class UserContactController { private static class UserContactController {
@RequestMapping("/create") @RequestMapping("/create")
public String showCreate(@PathVariable Integer userId) { public String showCreate(@PathVariable Integer userId) {
@ -445,7 +458,7 @@ public class MvcUriComponentsBuilderTests {
abstract T get(ID id); 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) @RequestMapping(path = "/{id}", method = RequestMethod.GET)
public Person get(@PathVariable Long id) { public Person get(@PathVariable Long id) {
@ -454,7 +467,7 @@ public class MvcUriComponentsBuilderTests {
} }
@Controller @Controller
static class MetaAnnotationController { private static class MetaAnnotationController {
@RequestMapping @RequestMapping
public void handle() { public void handle() {
@ -472,7 +485,7 @@ public class MvcUriComponentsBuilderTests {
@Target({ElementType.METHOD, ElementType.TYPE}) @Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
@interface PostJson { private @interface PostJson {
String[] path() default {}; String[] path() default {};
} }