From a43a3d68be34d18d030ce9c514d6b16ebc1a0e92 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Fri, 24 Sep 2021 16:19:18 +0200 Subject: [PATCH] =?UTF-8?q?#1653=20-=20Tighten=20contracts=20for=20paramet?= =?UTF-8?q?er=20values=20in=20MethodLinkBuilderFactory.linkTo(=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a parameter array is given to MethodLinkBuilderFactory.linkTo(…) methods, we now verify its correct length in correspondence to the method given. If no parameters are given at all, we automatically create a parameter value array of the required length. --- .../server/MethodLinkBuilderFactory.java | 6 ++++-- .../hateoas/server/mvc/WebMvcLinkBuilder.java | 6 ++++++ .../server/mvc/WebMvcLinkBuilderFactory.java | 18 +++++++++--------- .../server/mvc/WebMvcLinkBuilderUnitTest.java | 14 ++++++++++++++ 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/springframework/hateoas/server/MethodLinkBuilderFactory.java b/src/main/java/org/springframework/hateoas/server/MethodLinkBuilderFactory.java index 863f26df..ed2ceb7e 100644 --- a/src/main/java/org/springframework/hateoas/server/MethodLinkBuilderFactory.java +++ b/src/main/java/org/springframework/hateoas/server/MethodLinkBuilderFactory.java @@ -30,7 +30,8 @@ public interface MethodLinkBuilderFactory extends LinkBui /** * Returns a {@link LinkBuilder} pointing to the URI mapped to the given {@link Method} and expanding this mapping - * using the given parameters. + * using the given parameters. The number of parameter values has to match the length of the given method's expected + * parameters. * * @param method must not be {@literal null}. * @param parameters @@ -40,7 +41,8 @@ public interface MethodLinkBuilderFactory extends LinkBui /** * Returns a {@link LinkBuilder} pointing to the URI mapped to the given {@link Method} assuming it was invoked on an - * object of the given type. + * object of the given type. The number of parameter values has to match the length of the given method's expected + * parameters. * * @param type must not be {@literal null}. * @param method must not be {@literal null}. diff --git a/src/main/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilder.java b/src/main/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilder.java index 455d8e20..1a7144ab 100644 --- a/src/main/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilder.java +++ b/src/main/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilder.java @@ -136,6 +136,12 @@ public class WebMvcLinkBuilder extends TemplateVariableAwareLinkBuilderSupport String.format("Incorrect number of parameter values given. Expected %s, got %s!", expected, given)); + return linkTo(DummyInvocationUtils.getLastInvocationAware(controller, method, parameters)); } diff --git a/src/main/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilderFactory.java b/src/main/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilderFactory.java index 1f966a6a..dd7e09f0 100644 --- a/src/main/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilderFactory.java +++ b/src/main/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilderFactory.java @@ -99,6 +99,15 @@ public class WebMvcLinkBuilderFactory implements MethodLinkBuilderFactory getConversionService() { return () -> { diff --git a/src/test/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilderUnitTest.java b/src/test/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilderUnitTest.java index 9007d776..e3b9e5c8 100644 --- a/src/test/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilderUnitTest.java +++ b/src/test/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilderUnitTest.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.Optional; import java.util.stream.Stream; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; import org.junit.jupiter.api.Test; import org.springframework.hateoas.IanaLinkRelations; import org.springframework.hateoas.Link; @@ -669,6 +670,19 @@ class WebMvcLinkBuilderUnitTest extends TestUtils { assertThat(linkTo(method, new Object[] { null }).withSelfRel().getHref()).endsWith("?id={id}"); } + @Test // #1653 + void buildsLinkWithoutParameterValuesGiven() throws Exception { + + Method method = ControllerWithMethods.class.getDeclaredMethod("myMethod", Object.class); + + Stream. of( // + () -> linkTo(method, new Object[0]), // + () -> linkTo(ControllerWithMethods.class, method, new Object[0]) // + ) // + .map(assertThatIllegalArgumentException()::isThrownBy) // + .forEach(it -> it.withMessageContaining("Expected 1, got 0")); + } + private static UriComponents toComponents(Link link) { return UriComponentsBuilder.fromUriString(link.expand().getHref()).build(); }