Skip to content

Ensure MvcUriComponentsBuilder generates correct URL from type-level mapping without leading slash#24143

Closed
yoshikawaa wants to merge 1 commit into
spring-projects:masterfrom
yoshikawaa:patch-1
Closed

Ensure MvcUriComponentsBuilder generates correct URL from type-level mapping without leading slash#24143
yoshikawaa wants to merge 1 commit into
spring-projects:masterfrom
yoshikawaa:patch-1

Conversation

@yoshikawaa

Copy link
Copy Markdown
Contributor

There seems to be a bug in MvcUriComponentsBuilder, so I fixed it.

affects

  • Spring 5.2.1 and older.
  • Using Spring WebMVC and JSP/Thymeleaf.

problem

MvcUriComponentsBuilder generates incorrect URLs from paths without slashes.

  • Context Path

/demo

  • Controller
@Controller
@RequestMapping("test") // non slash path
public class TestController {

    @GetMapping
    public String test() {
        return "test";
    }
}
  • JSP (The same problem will happen with Thymeleaf)
<a href="${spring:mvcUrl('TC#test').build()}">test</a>
  • Created HTML
<a href="/demotest">test</a>
<!-- expected '/demo/test' -->

Cause

At MvcUriComponentsBuilder#fromMethodInternal,
PathMatcher adds a slash between the path of class @RequestMapping and the path of method @RequestMapping. However, no slash is added at the beginning of the merged path.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 5, 2019
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Dec 5, 2019
@sbrannen sbrannen changed the title fix that MvcUriComponentsBuilder generates incorrect URLs from paths without slashes. Ensure MvcUriComponentsBuilder generates correct URLs from paths without slashes Dec 5, 2019
@rstoyanchev rstoyanchev self-assigned this Dec 9, 2019
@rstoyanchev rstoyanchev added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 9, 2019
@rstoyanchev rstoyanchev added this to the 5.2.3 milestone Dec 9, 2019
@rstoyanchev rstoyanchev changed the title Ensure MvcUriComponentsBuilder generates correct URLs from paths without slashes Ensure MvcUriComponentsBuilder generates correct URL from type-level mapping without leading slash Dec 9, 2019
rstoyanchev pushed a commit that referenced this pull request Jan 8, 2020
rstoyanchev added a commit that referenced this pull request Jan 8, 2020
@yoshikawaa yoshikawaa deleted the patch-1 branch January 9, 2020 01:20
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

Successfully merging this pull request may close these issues.

4 participants