Skip to content

Inconsistency between toUriString and toUri of UriComponents #35552

@anaconda875

Description

@anaconda875

Hi Spring Team,

		String addr = "[email protected]";
		String encodedAddr = URLEncoder.encode(addr, StandardCharsets.UTF_8);
		UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://localhost:8080/emails/{email}")
				.uriVariables(Map.of("email", encodedAddr))
				.build();

		System.out.println(uriComponents.toUriString());
		System.out.println(uriComponents.toUri());

Above code will print

http://localhost:8080/emails/a%40b.com
http://localhost:8080/emails/a%2540b.com

This is because, the UriComponentsBuilder.buildInternal(EncodingHint hint) was called with the hint was EncodingHint.NONE. After that, HierarchicalUriComponents.expand(UriTemplateVariables uriVariables) called and the a%40b.com was placed to the {email} placeholder without any encoding. Finally, uriComponents.toUriString() did nothing, but just concaternate scheme, host, path, etc together to produced http://localhost:8080/emails/a%40b.com.
In case of uriComponents.toUri(), the constructor java.net.URI#URI(java.lang.String, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String, java.lang.String) called, which encoded the a%40b.com again, so we got http://localhost:8080/emails/a%2540b.com
Proposal: Consider if EncodingHint#NONE provided, then call new URI(toUriString()); instead

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: feedback-providedFeedback has been providedstatus: waiting-for-triageAn issue we've not yet triaged or decided on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions