Skip to content

Commit 5faf985

Browse files
gregturnodrotbohm
authored andcommitted
#617 - AnnotatedParametersParameterAccessor now considers @AliasFor declarations.
We're now using SynthesizingMethodParameter to correctly support annotation attributes that use @AliasFor. Original pull request: #621.
1 parent 4ad8c3b commit 5faf985

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/main/java/org/springframework/hateoas/core/MethodParameters.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.core.DefaultParameterNameDiscoverer;
2525
import org.springframework.core.MethodParameter;
2626
import org.springframework.core.ParameterNameDiscoverer;
27+
import org.springframework.core.annotation.SynthesizingMethodParameter;
2728
import org.springframework.util.Assert;
2829
import org.springframework.util.ConcurrentReferenceHashMap;
2930

@@ -151,7 +152,7 @@ public List<MethodParameter> getParametersWith(Class<? extends Annotation> annot
151152
*
152153
* @author Oliver Gierke
153154
*/
154-
private static class AnnotationNamingMethodParameter extends MethodParameter {
155+
private static class AnnotationNamingMethodParameter extends SynthesizingMethodParameter {
155156

156157
private final AnnotationAttribute attribute;
157158
private String name;

src/test/java/org/springframework/hateoas/mvc/ControllerLinkBuilderUnitTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,16 @@ public void considersOptionalWithValueMethodParameterOptional() {
580580
assertThat(link.getHref(), endsWith("?value=1"));
581581
}
582582

583+
/**
584+
* @see #617
585+
*/
586+
@Test
587+
public void alternativePathVariableParameter() {
588+
589+
Link link = linkTo(methodOn(ControllerWithMethods.class).methodWithAlternatePathVariable("bar")).withSelfRel();
590+
assertThat(link.getHref(), is("http://localhost/something/bar/foo"));
591+
}
592+
583593
private static UriComponents toComponents(Link link) {
584594
return UriComponentsBuilder.fromUriString(link.expand().getHref()).build();
585595
}
@@ -647,6 +657,11 @@ HttpEntity<Void> methodWithMultiValueRequestParams(@PathVariable String id, @Req
647657
return null;
648658
}
649659

660+
@RequestMapping(value = "/{id}/foo")
661+
HttpEntity<Void> methodWithAlternatePathVariable(@PathVariable(name = "id") String otherId) {
662+
return null;
663+
}
664+
650665
@RequestMapping(value = "/foo")
651666
HttpEntity<Void> methodForOptionalNextPage(@RequestParam(required = false) Integer offset) {
652667
return null;

src/test/java/org/springframework/hateoas/mvc/DummyInvocationUtilsUnitTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
*/
1616
package org.springframework.hateoas.mvc;
1717

18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.hamcrest.MatcherAssert.*;
20+
1821
import org.junit.Test;
22+
import org.springframework.hateoas.Link;
1923
import org.springframework.hateoas.TestUtils;
2024
import org.springframework.hateoas.core.DummyInvocationUtils;
2125
import org.springframework.http.HttpEntity;
@@ -30,10 +34,17 @@
3034
public class DummyInvocationUtilsUnitTest extends TestUtils {
3135

3236
@Test
33-
public void test() {
37+
public void pathVariableWithDefaultParameter() {
38+
39+
Link link = ControllerLinkBuilder.linkTo(DummyInvocationUtils.methodOn(SampleController.class).someMethod(1L)).withSelfRel();
40+
assertThat(link.getHref(), is("http://localhost/sample/1/foo"));
41+
}
3442

35-
ControllerLinkBuilder.linkTo(DummyInvocationUtils.methodOn(SampleController.class).someMethod(1L));
43+
@Test
44+
public void pathVariableWithNameParameter() {
3645

46+
Link link = ControllerLinkBuilder.linkTo(DummyInvocationUtils.methodOn(SampleController.class).someOtherMethod(2L)).withSelfRel();
47+
assertThat(link.getHref(), is("http://localhost/sample/2/bar"));
3748
}
3849

3950
@RequestMapping("/sample")
@@ -43,5 +54,10 @@ static class SampleController {
4354
HttpEntity<Void> someMethod(@PathVariable("id") Long id) {
4455
return new ResponseEntity<Void>(HttpStatus.OK);
4556
}
57+
58+
@RequestMapping("/{otherName}/bar")
59+
HttpEntity<Void> someOtherMethod(@PathVariable(name = "otherName") Long id) {
60+
return new ResponseEntity<Void>(HttpStatus.OK);
61+
}
4662
}
4763
}

0 commit comments

Comments
 (0)