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 should resolve property placeholders in request mapping paths #26795

Closed
mauromol opened this issue Apr 13, 2021 · 4 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@mauromol
Copy link

I'm using Spring Framework 5.3.4 with Spring Boot 2.4.3.

I have a controller declared in this way:

@Controller
@RequestMapping("${saml.sp.controller-base-mapping}")
public class ServiceProviderController {
  /...
}

So, the property saml.sp.controller-base-mapping is externalized in application.properties file: if its value is sp, this should be equivalent to: @RequestMapping("sp"). And indeed Spring Web MVC handles this well.

However, if I then use MvcUriComponentsBuilder to retrieve the corresponding URL:

MvcUriComponentsBuilder.fromController(ServiceProviderController.class).build().toURL();

This returns a UriComponents of: http://localhost:8081/${saml.sp.controller-base-mapping}

First problem: I would have expected the externalized property to be already resolved (so to get http://localhost:8081/sp), because this should be an expansion mechanism that comes BEFORE path variable expansion.

Indeed (and here comes the second problem), if I try to do:

MvcUriComponentsBuilder.fromController(ServiceProviderController.class).buildAndExpand("sp");

I then get a UriComponents of: http://localhost:8081/$sp

This is somewhat expected, since path variable expansion is another story and does not use the "$" marker.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 13, 2021
@rstoyanchev
Copy link
Contributor

MvcUriComponentsBuilder looks up the annotation values and so it needs to replace variables, but currently it doesn't do that and we would need to make a change to support it. In the mean time, as a workaround you can have a StringValueResolver injected via EmbeddedValueResolverAware and then apply it like this:

String s = MvcUriComponentsBuilder.fromController(MyController.class).build().toUriString();
s = valueResolver.resolveStringValue(s);
UriComponents uriComponents = UriComponentsBuilder.fromUriString(s)...

@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 28, 2021
@rstoyanchev rstoyanchev added this to the 5.x Backlog milestone Apr 28, 2021
@rstoyanchev rstoyanchev changed the title MvcUriComponentsBuilder does not play well with property expansion MvcUriComponentsBuilder does not resolve property placeholders Apr 28, 2021
@mauromol
Copy link
Author

In the mean time, as a workaround you can have a StringValueResolver injected via EmbeddedValueResolverAware and then apply it like this:

Thank you for the suggested workaround!

@lower-case
Copy link

Hi @rstoyanchev I'd like to work on this. I'm going through the code, will raise a PR soon.

@lower-case
Copy link

Have raised this Draft PR. Please review the approach and provide your feedback. Working on test cases.
Thanks!

snicoll added a commit to snicoll/spring-framework that referenced this issue Jan 5, 2024
This commit makes sure to resolve placeholders in request mappings
using the EmbeddedValueResolver of the current WebApplicationContext.

To avoid retrieving the context too often, we check for the presence of
the standard placeholder prefix.

Closes spring-projectsgh-26795
@snicoll snicoll self-assigned this Jan 5, 2024
@snicoll snicoll modified the milestones: 6.x Backlog, 6.1.3 Jan 5, 2024
@snicoll snicoll changed the title MvcUriComponentsBuilder does not resolve property placeholders MvcUriComponentsBuilder should resolve property placeholders in request mapping paths Jan 5, 2024
@snicoll snicoll closed this as completed in a8273a3 Jan 5, 2024
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: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants