Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MvcUriComponentsBuilder adds trailing slash if path is defined at controller level only #29897

Closed
torstenmandry opened this issue Jan 30, 2023 · 0 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@torstenmandry
Copy link

Affects: spring-webmvc 6.0.4

I use MvcUriComponentsBuilder.fromMethodCall(...) to compute paths to controller endpoints and include them in view models. In my controllers, the path usually is configured on the controller level (via @RequestMapping annotation). The controller methods are annotated with empty @GetMapping, @PostMapping, or similar annotations.

After upgrading to spring-boot 3 (spring 6) the computed paths do not work anymore, because they contain a trailing slash, which is no longer accepted due to the deactivated/deprecated trailing slash matching configuration in spring 6.

Debugging into the MvcUriComponentsBuilder I found the following method, which seems to cause this (now incompatible) behavior:

	private static String getMethodMapping(Method method) {
		Assert.notNull(method, "'method' must not be null");
		RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
		if (requestMapping == null) {
			throw new IllegalArgumentException("No @RequestMapping on: " + method.toGenericString());
		}
		String[] paths = requestMapping.path();
		if (ObjectUtils.isEmpty(paths) || !StringUtils.hasLength(paths[0])) {
			return "/";
		}
		if (paths.length > 1 && logger.isTraceEnabled()) {
			logger.trace("Using first of multiple paths on " + method.toGenericString());
		}
		return paths[0];
	}

In my case, requestMapping.path() returns an empty array (which seems to be correct because the method level request mapping contains no path). But this leads to the method returning a single slash, which is added to the path that is taken from the controller level annotation (in the fromMethodInternal method).

When I move the path configuration into the method level annotations, everything works fine.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 30, 2023
@poutsma poutsma added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Jan 30, 2023
@rstoyanchev rstoyanchev self-assigned this Jan 31, 2023
@rstoyanchev rstoyanchev added this to the 6.0.5 milestone Jan 31, 2023
@rstoyanchev rstoyanchev removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 31, 2023
@rstoyanchev rstoyanchev changed the title MvcUriComponentsBuilder adds trailing slash if path is defined at controller level MvcUriComponentsBuilder adds trailing slash if path is defined at controller level only Jan 31, 2023
@rstoyanchev rstoyanchev added the type: bug A general bug label Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants