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(); }