Skip to content

Introduce ability to NOT encode URIs with UriTemplate [SPR-16279] #20826

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

Closed
spring-projects-issues opened this issue Dec 7, 2017 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

Greg Turnquist opened SPR-16279 and commented

Spring HATEOAS leverage's Spring Framework's UriTemplate class. However, in certain scenarios, the user provides a URI Template that is already encoded (e.g. /foo/b%20ar{?x}). In this scenario, when creating a Spring Framework UriTemplate, the "%" gets encoded into%25. And because we wrap this some of our own goodness, it actually gets encoded twice when doing an expand (e.g. x=>1 in this case).

It would be useful if there was an optional to indicate "do not encode", possibly here?

public URI expand(Map<String, ?> uriVariables) {
     UriComponents expandedComponents = this.uriComponents.expand(uriVariables);
     UriComponents encodedComponents = expandedComponents.encode();
     return encodedComponents.toUri();
}

A flag here, or an overloaded method to side step encoding would make it possible.

Related issue


Reference URL: spring-projects/spring-hateoas#593

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

UriTemplate doesn't do the work itself but delegates to UriComponentsBuilder. Any reason not to use that directly giving you the control you need?

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Greg Turnquist, coming back to this, if you're looking for something like this:

UriTemplate template = new UriTemplate(uriString, true /*encoded*/);
URI uri = template.expand(uriVars);

The equivalent without UriTemplate is this:

URI uri = UriComponentsBuilder.fromUriString(uriString)
        .build(true)
        .expand(uriVars)
        .toUri();

Or you could also use the UriTemplateHandler contract which is what we use in the RestTemplate to customize UriTemplate handling:

DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory();
factory.setEncodingMode(EncodingMode.NONE);
URI uri = factory.expand(uriString, uriVars);

@spring-projects-issues spring-projects-issues added status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
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) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants