spring-projects / spring-projects/spring-hateoas

Breaking change in semantics of `WebMvcLinkBuilder.linkTo(Class<?>, Method, Object…)` in 1.3.4

Open
#1,720 3 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Nov 22, 2021.

in: core process: waiting for feedback type: bug
Dominant language
Java
Stars
1.1k
Forks
476
PR merge metrics
No merged PRs in 30d

Description

When upgrading from Spring Boot 2.5.4 to 2.5.6, the version of Spring HATEOAS pulled in changes from 1.3.3 to 1.3.5. This broke our code using the WebMvcLinkBuilder method:

public static WebMvcLinkBuilder linkTo(Class<?> controller, Method method, Object... parameters) {

As can be seen below, the changes introduced in 1.3.4 are not backwards compatible with 1.3.3. The 1.3.3 version of the method only expects the URL template variables to be given, whereas starting from 1.3.4 is expecting all parameters from the actual controller method.

1.3.3:

	public static WebMvcLinkBuilder linkTo(Class<?> controller, Method method, Object... parameters) {

		Assert.notNull(controller, "Controller type must not be null!");
		Assert.notNull(method, "Method must not be null!");

		String mapping = SpringAffordanceBuilder.DISCOVERER.getMapping(controller, method);
		UriTemplate template = UriTemplateFactory.templateFor(mapping);
		URI uri = template.expand(parameters);

		return new WebMvcLinkBuilder(UriComponentsBuilderFactory.getComponents()).slash(uri);
	}

1.3.4:

	public static WebMvcLinkBuilder linkTo(Class<?> controller, Method method, Object... parameters) {

		Assert.notNull(controller, "Controller type must not be null!");
		Assert.notNull(method, "Method must not be null!");
		Assert.notNull(parameters, "Parameters must not be null!");

		return linkTo(DummyInvocationUtils.getLastInvocationAware(controller, method, parameters));
	}

1.3.5:

	public static WebMvcLinkBuilder linkTo(Class<?> controller, Method method, Object... parameters) {

		Assert.notNull(controller, "Controller type must not be null!");
		Assert.notNull(method, "Method must not be null!");
		Assert.notNull(parameters, "Parameters must not be null!");

		int expected = method.getParameterTypes().length;
		int given = parameters.length;

		Assert.isTrue(expected == given,
				() -> String.format("Incorrect number of parameter values given. Expected %s, got %s!", expected, given));

		return linkTo(DummyInvocationUtils.getLastInvocationAware(controller, method, parameters));
	}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.