Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.

fix: Support delimiters(_-.~) as start or end characters for a segment. #336

Merged
merged 2 commits into from
Feb 4, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Simplify validation logic. Add more comments.
  • Loading branch information
blakeli0 committed Feb 4, 2022
commit edff9a674285a4d9013bf7e70404ae20f8b2a2a7
12 changes: 6 additions & 6 deletions src/main/java/com/google/api/pathtemplate/PathTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -1014,16 +1014,16 @@ private static ImmutableList<Segment> parseTemplate(String template) {
}

private static boolean isSegmentBeginOrEndInvalid(String seg) {
// A segment is invalid if it contains only one character and the character is a delimiter
if (seg.length() == 1 && COMPLEX_DELIMITER_PATTERN.matcher(seg).find()) {
return true;
}
//A segment like .{well}-{known} or {well}-{known}. is invalid.
//A segment like .well-known or .well-{known} will be considered a literal hence is valid
return (COMPLEX_DELIMITER_PATTERN.matcher(seg.substring(0, 1)).find()
&& seg.length() > 1
&& seg.charAt(1) == '{')
// A segment can start with a delimiter, as long as there is no { right after it.
// A segment can end with a delimiter, as long as there is no } right before it.
// e.g. Invalid: .{well}-{known}, {well}-{known}-
// Valid: .well-known, .well-{known}, .-~{well-known}, these segments are all considered as literals for matching
return (COMPLEX_DELIMITER_PATTERN.matcher(seg.substring(0, 1)).find() && seg.charAt(1) == '{')
|| (COMPLEX_DELIMITER_PATTERN.matcher(seg.substring(seg.length() - 1)).find()
&& seg.length() > 1
&& seg.charAt(seg.length() - 2) == '}');
}

Expand Down