Skip to content

Commit

Permalink
RegularExpression in Uri Template IS NOT optional
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <jan.supol@oracle.com>
  • Loading branch information
jansupol committed Nov 23, 2023
1 parent 1c329c8 commit 8105415
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Expand Up @@ -395,9 +395,14 @@ private int parseName(final CharacterIterator ci, int skipGroup) {
// groupCounts.add(1 + skipGroup);

if (variables.hasLength(0)) {
int len = TEMPLATE_VALUE_PATTERN.pattern().length() - 1;
String pattern = TEMPLATE_VALUE_PATTERN.pattern().substring(0, len) + '{' + variables.getLength(0) + '}';
namePattern = Pattern.compile(pattern);
if (variables.getLength(0) != 0) {
int len = TEMPLATE_VALUE_PATTERN.pattern().length() - 1;
String pattern = TEMPLATE_VALUE_PATTERN.pattern().substring(0, len)
+ '{' + variables.getLength(0) + '}';
namePattern = Pattern.compile(pattern);
} else {
namePattern = TEMPLATE_VALUE_PATTERN;
}
templateVariable.setLength(variables.getLength(0));
} else {
namePattern = (!variables.hasRegexp(0))
Expand Down Expand Up @@ -462,7 +467,10 @@ private int parseName(final CharacterIterator ci, int skipGroup) {
if (argIndex != 0) {
regexBuilder.append(")");
}
regexBuilder.append("{0,1}");

if (!variables.hasRegexp(argIndex)) {
regexBuilder.append("{0,1}");
}

argIndex++;
groupCounts.add(2);
Expand Down Expand Up @@ -571,6 +579,7 @@ private void parse(CharacterIterator ci, String template) {

StringBuilder regexBuilder = new StringBuilder();
State state = State.TEMPLATE;
State previousState;
boolean star = false;
boolean whiteSpace = false;
boolean ignoredLastComma = false;
Expand All @@ -579,6 +588,7 @@ private void parse(CharacterIterator ci, String template) {
int regExpRound = 0; // (
boolean reqExpSlash = false; // \
while ((state.value & (State.ERROR.value | State.EXIT.value)) == 0) {
previousState = state;
c = ci.next();
// "\\{(\\w[-\\w\\.]*)
if (Character.isLetterOrDigit(c)) {
Expand Down Expand Up @@ -702,8 +712,8 @@ private void parse(CharacterIterator ci, String template) {
regexps.add(regex);
}
} else {
regexps.add(null);
lengths.add(null);
regexps.add(previousState == State.REGEXP ? "" : null);
lengths.add(previousState == State.REGEXP ? 0 : null);
}

names.add(nameBuilder.toString());
Expand Down
Expand Up @@ -31,6 +31,8 @@

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -257,7 +259,7 @@ void _testMatching(final String template, final String uri, final String... valu
final Map<String, String> m = new HashMap<String, String>();

boolean isMatch = t.match(uri, m);
assertTrue(isMatch);
assertTrue(isMatch, "No match for '" + uri + "' & params '" + Arrays.toString(values) + "`");
assertEquals(values.length, t.getTemplateVariables().size());

final Iterator<String> names = t.getTemplateVariables().iterator();
Expand Down Expand Up @@ -982,13 +984,20 @@ void testRfc6570MultiplePathArgs() {
_testTemplateNames("/{a,b,c}", "a", "b", "c");
_testMatching("/uri/{a}", "/uri/hello", "hello");
_testMatching("/uri/{a,b}", "/uri/hello,world", "hello", "world");
_testMatching("/uri/{a,b}", "/uri/x", "x", null);
_testMatching("/uri{?a,b}", "/uri?a=hello&b=world", "hello", "world");
_testMatching("/uri/{a,b,c}", "/uri/hello,world,!", "hello", "world", "!");
_testMatching("/uri/{a,b,c}", "/uri/hello,world", "hello", "world", null);
_testMatching("/uri/{a,b,c}", "/uri/hello", "hello", null, null);
_testMatching("/uri/{a,b,c}", "/uri/", null, null, null);
}

@Test
public void testRegularExpressionIsNotOptional() {
Assertions.assertThrows(AssertionFailedError.class,
() -> _testMatching("/{name: [a-z0-9]{3,128}}", "/", new String[]{null}));
}

@Test
void testRfc6570PathLength() {
_testMatching("/uri/{a:5}", "/uri/hello", "hello");
Expand Down

0 comments on commit 8105415

Please sign in to comment.