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

Multiple template variables using regular expression break link creation #1450

Closed
jenny1976 opened this issue Feb 10, 2021 · 4 comments
Closed
Assignees
Labels
in: core Core parts of the project type: bug
Milestone

Comments

@jenny1976
Copy link

spring-boot 2.4.2, spring-hateoas 1.2.3

Since the current spring-boot update (see above) the link building via WebMvcLinkBuilder produces broken urls when the request mapping contains regular expressions in PathVariables. I could reproduce this issue with a mapping example from spring-docs:

Here's a snippet from my DemoController.java

@GetMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{extension:\\.[a-z]+}")
public ModelAndView handle(@PathVariable String symbolicName, @PathVariable String version, @PathVariable String extension) {
    return new ModelAndView();
}

and a unittest that shows the current bug:

@Test
void aSpringDocExampleThatIsBroken() {
    WebMvcLinkBuilder linkBuilder = linkTo(methodOn(DemoController.class).handle("artifact", "1.1.1",".jar"));

    String url = linkBuilder.withSelfRel().getTemplate().expand().toString();

    // fails
    assertThat(url).isEqualTo("http://localhost/spring-web/artifact-1.1.1.jar");
    // the result is http://localhost/spring-web/artifact
}

I tested this bug in different variations. It's always the first PathVariable with a regular expression, that is evaluated correctly
but the following PathVars are ignored then and the url gets cut.

@odrotbohm
Copy link
Member

odrotbohm commented Feb 10, 2021

Likely to be a side effect of the fix for #1412, #1413, #1414. Looking into it. One thing: you say "since the current Boot upgrade" which seems to imply that the expansion had properly worked before. #1412 was actually fixed because the regular expressions would not have been removed at all before. Can you confirm? You should be able to manually downgrade Spring HATEAOS to 1.2.2 by setting the spring-hateoas.version property to that version in your POM.

@odrotbohm odrotbohm added this to the 1.3 M2 milestone Feb 10, 2021
@odrotbohm
Copy link
Member

Got it. The regex cleaning up the template variables was using a greedy capture and thus ate all but the first match. Fix coming in a bit.

odrotbohm added a commit that referenced this issue Feb 10, 2021
…ressions.

Switched to manual parsing of template variables as regular expressions can contain { and } characters which makes matching variables using these as delimiters impossible.

Related ticket: #1412.
@odrotbohm odrotbohm changed the title link creation with WebMvcLinkBuilder uncomplete when PathVariables need regex evaluation Multiple template variables using regular expression break link creation Feb 10, 2021
@odrotbohm odrotbohm self-assigned this Feb 10, 2021
@odrotbohm odrotbohm added in: core Core parts of the project type: bug labels Feb 10, 2021
@odrotbohm
Copy link
Member

This is fixed and back-ported. Should be available in the latest snapshots.

@jenny1976
Copy link
Author

thank you @odrotbohm for taking care so quickly! Helps us a ton 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Core parts of the project type: bug
Projects
None yet
Development

No branches or pull requests

2 participants