Skip to content

Commit b7faa16

Browse files
committed
Fix issue where using capturing groups in RegexConstraints would throw off ParameterizedPattern groupings
1 parent cef8944 commit b7faa16

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎api/src/main/java/org/ocpsoft/rewrite/param/RegexParameterizedPatternParser.java‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ else if ((last == null) && (capture.getStart() > 0))
198198
if (constraint instanceof RegexConstraint)
199199
{
200200
parameterPatternBuilder.append("(?:");
201-
parameterPatternBuilder.append(((RegexConstraint) constraint).getPattern());
201+
parameterPatternBuilder.append(sanitizePattern(constraint));
202202
parameterPatternBuilder.append(")");
203203

204204
if (iterator.hasNext())
@@ -237,6 +237,25 @@ else if (last == null)
237237
return compiledPattern;
238238
}
239239

240+
private String sanitizePattern(Constraint<String> constraint)
241+
{
242+
StringBuilder result = new StringBuilder();
243+
String regex = ((RegexConstraint) constraint).getPattern();
244+
char[] pattern = regex.toCharArray();
245+
for (int i = 0; i < pattern.length; i++)
246+
{
247+
result.append(pattern[i]);
248+
if (pattern[i] == '(' && !ParseTools.isEscaped(pattern, i))
249+
{
250+
if (pattern[i + 1] != '?')
251+
{
252+
result.append("?:");
253+
}
254+
}
255+
}
256+
return result.toString();
257+
}
258+
240259
private String unescape(String literal)
241260
{
242261
String result = literal.replace("\\\\", "\\");

‎api/src/test/java/org/ocpsoft/rewrite/param/ParameterizedPatternTest.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,38 @@ public void testComplexMatchingWithMutipleAdjacentParameters() throws Exception
8888
}
8989
}
9090

91+
@Test
92+
public void testComplexMatchingWithGroupsInRegexConstraints() throws Exception
93+
{
94+
String url = "http://domain.com:8080/context/application/pathy?foo=bar&baz=bazaar";
95+
96+
ParameterizedPatternParser path = new RegexParameterizedPatternParser(
97+
"{prefix}/application/{seg}{suffix}");
98+
99+
ParameterStore store = DefaultParameterStore.getInstance(context);
100+
initialize(store, path);
101+
102+
((ConfigurableParameter<?>) store.get("prefix")).constrainedBy(new RegexConstraint("((.*)?)"));
103+
((ConfigurableParameter<?>) store.get("seg"))
104+
.constrainedBy(new RegexConstraint("([^/]+)"))
105+
.constrainedBy(new RegexConstraint("(pathy)"));
106+
((ConfigurableParameter<?>) store.get("suffix")).constrainedBy(new RegexConstraint("\\?.*"));
107+
108+
initialize(store, path);
109+
110+
Assert.assertTrue(path.parse(url).matches());
111+
112+
String[] expected = new String[] { "http://domain.com:8080/context", "pathy", "?foo=bar&baz=bazaar" };
113+
Map<Parameter<?>, String> parsed = path.parse(url).getParameters(context);
114+
115+
int index = 0;
116+
for (Entry<Parameter<?>, String> entry : parsed.entrySet())
117+
{
118+
String value = entry.getValue();
119+
Assert.assertEquals(expected[index++], value);
120+
}
121+
}
122+
91123
@Test
92124
public void testEscapingWindowsFilePathsNoParams()
93125
{

0 commit comments

Comments
 (0)