Skip to content

Commit 4c139a7

Browse files
committed
path params - allow underscores in path param names
This fixes #170
1 parent 6a56329 commit 4c139a7

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

restx-core/src/main/java/restx/StdRestxRequestMatcher.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ private static interface PathParserCharProcessor {
112112
void end(PathPatternParser pathPatternParser);
113113
}
114114

115+
private static boolean isValidPathParamNameChar(int curChar) {
116+
//only letters/digit/underscore are authorized in path param name
117+
return Character.isLetterOrDigit(curChar) || curChar == '_';
118+
}
119+
115120
private static final class CurlyBracesPathParamPathParserCharProcessor implements PathParserCharProcessor {
116121
private int openBr = 1;
117122
private boolean inRegexDef;
@@ -166,8 +171,8 @@ public void handle(int curChar, PathPatternParser pathPatternParser) {
166171
inRegexDef = true;
167172
pathParamRegex.append("(");
168173
} else {
169-
if (!Character.isLetterOrDigit(curChar)) {
170-
//only letters are authorized in path param name
174+
if (!isValidPathParamNameChar(curChar)) {
175+
//only letters/digit/underscore are authorized in path param name
171176
throw new IllegalArgumentException(String.format(
172177
"illegal path parameter definition '%s' at offset %d" +
173178
" - only letters and digits are authorized in path param name",
@@ -188,7 +193,7 @@ private static final class SimpleColumnBasedPathParamParserCharProcessor impleme
188193
private StringBuilder pathParamName = new StringBuilder();
189194
@Override
190195
public void handle(int curChar, PathPatternParser pathPatternParser) {
191-
if (!Character.isLetterOrDigit(curChar)) {
196+
if (!isValidPathParamNameChar(curChar)) {
192197
pathPatternParser.patternBuilder.append("([^\\/]+)");
193198
pathPatternParser.stdPathPatternBuilder.append("{").append(pathParamName).append("}");
194199
pathPatternParser.groupNamesBuilder.add(pathParamName.toString());

restx-samplest/src/main/java/samplest/core/ParametersResource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public void setVal(String val) {
4646
}
4747
}
4848

49-
@GET("/params/path/{a}/:b/{c:\\d+}:d")
50-
public String pathparams(String a, String b, String c, String d) {
51-
return "a=" + a + " b=" + b + " c=" + c + " d=" + d;
49+
@GET("/params/path/{a}/:_b/{c:\\d+}:d")
50+
public String pathparams(String a, String _b, String c, String d) {
51+
return "a=" + a + " b=" + _b + " c=" + c + " d=" + d;
5252
}
5353

5454
@GET("/params/query/withOptionalString")

0 commit comments

Comments
 (0)