Handle nested braces in UriTemplates.expand

This allows replacement of JSON-like payloads without using hacks like
percent-encoding braces.
This commit is contained in:
Andrew Gaul 2016-05-20 09:24:02 -07:00
parent f46b38dd89
commit 981b882f59
2 changed files with 18 additions and 5 deletions

View File

@ -44,17 +44,26 @@ public class UriTemplates {
for (char c : Lists.charactersOf(template)) {
switch (c) {
case '{':
if (inVar) {
builder.append('{');
builder.append(var);
var.setLength(0);
}
inVar = true;
break;
case '}':
inVar = false;
String key = var.toString();
Object value = variables.get(var.toString());
if (value != null)
builder.append(value);
else
builder.append('{').append(key).append('}');
if (inVar) {
if (value != null)
builder.append(value);
else
builder.append('{').append(key).append('}');
} else {
builder.append('}');
}
var.setLength(0);
inVar = false;
break;
default:
if (inVar)

View File

@ -51,4 +51,8 @@ public class UriTemplatesTest {
public void testMissingParamProceeds() {
assertEquals(expand("/{user-dir}", ImmutableMap.of("user_dir", "foo")), "/{user-dir}");
}
public void testJson() {
assertEquals(expand("{\"key\":\"{variable}\"}", ImmutableMap.of("variable", "value")), "{\"key\":\"value\"}");
}
}