When using UriComponentsBuilder
along with URI variables, some fragments with curly braces are silently removed from the URI, even if they don't follow the variable naming convention, nor contain any variable name to be replaced. I have suffered this problem when trying to pass in a query param formatted as JSON.
The following unit test illustrates the problem well:
@Test
public void testHierarchicalUriComponents_OK() {
String uriString = UriComponentsBuilder
.fromHttpUrl("http://localhost")
.path("/12345")
.queryParam("json", "{\"role\":{\"xxxx\": \"yyyy\"}}")
.build()
.toUriString();
System.out.println(uriString); // http://localhost/12345?json={"role":{"xxxx": "yyyy"}}
assertThat(uriString).contains("xxxx");
}
@Test
public void testHierarchicalUriComponents_KO() {
Map<String, Object> variables = new HashMap<>();
variables.put("id", "12345");
String uriString = UriComponentsBuilder
.fromHttpUrl("http://localhost")
.path("/{id}")
.queryParam("json", "{\"role\":{\"xxxx\": \"yyyy\"}}")
.uriVariables(variables)
.build()
.toUriString();
System.out.println(uriString); // http://localhost/12345?json={"role":}
assertThat(uriString).contains("xxxx");
}
I guess this is a bug, because the documentation does not refer to any limitation about using URI variables and sections with curly braces together. Note that the uriString shown in the example is not encoded, for illustration purposes.
Affects: 5.2.12
Comment From: bclozel
This is the expected behavior as you're using the {variable}
template expression reserved for expansion. If you wish to build such an URL, please review the various URI building options in the reference documentation or use another utility that better fits your needs.