Properly expand URI vars with regex

Before this commit UriComponents was capable of expanding URI vars that
may have contained a regular expressions (as supported with
@RequestMapping for example). However if the regular expressions
contained any nested "{}" the expand did not work correctly.

This commit sanitizes a URI template source removing any content
between nested "{}" prior to expanding. This works since we only care
about the URI variable name.

Issue: SPR-13311
This commit is contained in:
Rossen Stoyanchev
2015-08-25 21:48:02 -04:00
parent 1a9e42b49d
commit 2e79a30fed
2 changed files with 42 additions and 4 deletions

View File

@@ -16,10 +16,6 @@
package org.springframework.web.util;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.web.util.UriComponentsBuilder.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
@@ -27,9 +23,17 @@ import java.io.ObjectOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.springframework.web.util.UriComponentsBuilder.fromUriString;
/**
* @author Arjen Poutsma
* @author Phillip Webb
@@ -82,6 +86,16 @@ public class UriComponentsTests {
assertEquals("http://example.com/1 2 3 4", uriComponents.toUriString());
}
// SPR-13311
@Test
public void expandWithRegexVar() {
String template = "/myurl/{name:[a-z]{1,5}}/show";
UriComponents uriComponents = UriComponentsBuilder.fromUriString(template).build();
uriComponents = uriComponents.expand(Collections.singletonMap("name", "test"));
assertEquals("/myurl/test/show", uriComponents.getPath());
}
// SPR-12123
@Test