#475 - Support all variable formats.
RFC-6570 specifies percent-encoding as a valid way to encode characters. By expanding the regex for UriTemplate to include '%', it's possible to support all variants. See: https://tools.ietf.org/html/rfc6570#section-2.3 Original issue: #246
This commit is contained in:
committed by
Oliver Drotbohm
parent
0db2bb551f
commit
975f1ff7fd
@@ -51,7 +51,7 @@ import org.springframework.web.util.UriUtils;
|
||||
*/
|
||||
public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
|
||||
|
||||
private static final Pattern VARIABLE_REGEX = Pattern.compile("\\{([\\?\\&#/]?)([\\w\\,*]+)\\}");
|
||||
private static final Pattern VARIABLE_REGEX = Pattern.compile("\\{([\\?\\&#/]?)([\\w%\\,*]+)\\}");
|
||||
private static final long serialVersionUID = -1007874653930162262L;
|
||||
|
||||
private final TemplateVariables variables;
|
||||
|
||||
@@ -39,7 +39,7 @@ import org.apache.commons.io.output.ByteArrayOutputStream;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.springframework.hateoas.TemplateVariable.VariableType;
|
||||
import org.springframework.hateoas.TemplateVariable.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link UriTemplate}.
|
||||
@@ -348,6 +348,20 @@ class UriTemplateUnitTest {
|
||||
assertThat(UriTemplate.isTemplate("http://localhost/api/rest/v1/userGroups/50/functions/{?id*}")).isTrue();
|
||||
}
|
||||
|
||||
@Test // #475
|
||||
void variablesWithPercentEncodingShouldWork() {
|
||||
|
||||
assertThat(UriTemplate.of("http://localhost/foo/bar/{%24filter}").expand("value"))
|
||||
.isEqualTo(URI.create("http://localhost/foo/bar/value"));
|
||||
}
|
||||
|
||||
@Test // #475
|
||||
void variablesWithUnderscoresShouldWork() {
|
||||
|
||||
assertThat(UriTemplate.of("http://localhost/foo/bar/{_filter}").expand("value"))
|
||||
.isEqualTo(URI.create("http://localhost/foo/bar/value"));
|
||||
}
|
||||
|
||||
private static void assertVariables(UriTemplate template, TemplateVariable... variables) {
|
||||
assertVariables(template, Arrays.asList(variables));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user