Skip to content

Commit f21c8aa

Browse files
committed
#617 - Use SynthesizingMethodParameter to support AliasFor annotations
When parsing controller's method parameters, use Spring's SynthesizingMethodParameter to handle alternative attributes.
1 parent 05f687e commit f21c8aa

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/main/java/org/springframework/hateoas/mvc/AnnotatedParametersParameterAccessor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import java.util.List;
2525
import java.util.Map;
2626

27+
import org.springframework.core.DefaultParameterNameDiscoverer;
2728
import org.springframework.core.MethodParameter;
29+
import org.springframework.core.annotation.SynthesizingMethodParameter;
2830
import org.springframework.core.convert.ConversionService;
2931
import org.springframework.core.convert.TypeDescriptor;
3032
import org.springframework.format.support.DefaultFormattingConversionService;
@@ -47,6 +49,7 @@ class AnnotatedParametersParameterAccessor {
4749

4850
private static final Map<Method, MethodParameters> METHOD_PARAMETERS_CACHE = new ConcurrentReferenceHashMap<Method, MethodParameters>(
4951
16, ReferenceType.WEAK);
52+
private static final DefaultParameterNameDiscoverer DISCOVERER = new DefaultParameterNameDiscoverer();
5053

5154
private final @NonNull AnnotationAttribute attribute;
5255

@@ -70,7 +73,9 @@ public List<BoundMethodParameter> getBoundParameters(MethodInvocation invocation
7073
Object verifiedValue = verifyParameterValue(parameter, value);
7174

7275
if (verifiedValue != null) {
73-
result.add(createParameter(parameter, verifiedValue, attribute));
76+
SynthesizingMethodParameter synthesizingMethodParameter = new SynthesizingMethodParameter(invocation.getMethod(), parameter.getParameterIndex());
77+
synthesizingMethodParameter.initParameterNameDiscovery(DISCOVERER);
78+
result.add(createParameter(synthesizingMethodParameter, verifiedValue, attribute));
7479
}
7580
}
7681

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,16 @@ public void createsLinkRelativeToContextRoot() {
554554
assertThat(linkTo(PersonControllerImpl.class).withSelfRel().getHref(), endsWith("/ctx/people"));
555555
}
556556

557+
/**
558+
* @see #617
559+
*/
560+
@Test
561+
public void alternativePathVariableParameter() {
562+
563+
Link link = linkTo(methodOn(ControllerWithMethods.class).methodWithAlternatePathVariable("bar")).withSelfRel();
564+
assertThat(link.getHref(), is("http://localhost/something/bar/foo"));
565+
}
566+
557567
private static UriComponents toComponents(Link link) {
558568
return UriComponentsBuilder.fromUriString(link.expand().getHref()).build();
559569
}
@@ -621,6 +631,11 @@ HttpEntity<Void> methodWithMultiValueRequestParams(@PathVariable String id, @Req
621631
return null;
622632
}
623633

634+
@RequestMapping(value = "/{id}/foo")
635+
HttpEntity<Void> methodWithAlternatePathVariable(@PathVariable(name = "id") String otherId) {
636+
return null;
637+
}
638+
624639
@RequestMapping(value = "/foo")
625640
HttpEntity<Void> methodForOptionalNextPage(@RequestParam(required = false) Integer offset) {
626641
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)