FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: parse collection wildcards and _deleted-topic_ patterns (#150) · googleapis/api-common-java@8fb8068 · GitHub

This repository was archived by the owner on Sep 27, 2023. It is now read-only.
/ api-common-java Public archive

Commit 8fb8068

Browse files
authored
fix: parse collection wildcards and _deleted-topic_ patterns (#150)
1 parent 4caf2ca commit 8fb8068

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

‎src/main/java/com/google/api/pathtemplate/PathTemplate.java‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,17 @@ private static ImmutableList<Segment> parseTemplate(String template) {
869869
int pathWildCardBound = 0;
870870

871871
for (String seg : Splitter.on('/').trimResults().split(template)) {
872-
if (COMPLEX_DELIMITER_PATTERN.matcher(seg.substring(0, 1)).find()
873-
|| COMPLEX_DELIMITER_PATTERN.matcher(seg.substring(seg.length() - 1)).find()) {
872+
// Handle _deleted-topic_ for PubSub.
873+
if (seg.equals("_deleted-topic_")) {
874+
builder.add(Segment.create(SegmentKind.LITERAL, seg));
875+
continue;
876+
}
877+
878+
boolean isLastSegment = (template.indexOf(seg) + seg.length()) == template.length();
879+
boolean isCollectionWildcard = !isLastSegment && (seg.equals("-") || seg.equals("-}"));
880+
if (!isCollectionWildcard
881+
&& (COMPLEX_DELIMITER_PATTERN.matcher(seg.substring(0, 1)).find()
882+
|| COMPLEX_DELIMITER_PATTERN.matcher(seg.substring(seg.length() - 1)).find())) {
874883
throw new ValidationException("parse error: invalid begin or end character in '%s'", seg);
875884
}
876885
// Disallow zero or multiple delimiters between variable names.
@@ -896,7 +905,7 @@ private static ImmutableList<Segment> parseTemplate(String template) {
896905
}
897906

898907
Matcher complexPatternDelimiterMatcher = END_SEGMENT_COMPLEX_DELIMITER_PATTERN.matcher(seg);
899-
complexDelimiterFound = complexPatternDelimiterMatcher.find();
908+
complexDelimiterFound = !isCollectionWildcard && complexPatternDelimiterMatcher.find();
900909

901910
// Look for complex resource names.
902911
// Need to handle something like "{user_a}~{user_b}".
@@ -915,6 +924,8 @@ private static ImmutableList<Segment> parseTemplate(String template) {
915924
throw new ValidationException(
916925
"parse error: invalid binding syntax in '%s'", template);
917926
}
927+
} else if (seg.indexOf('-') <= 0 && isCollectionWildcard) {
928+
implicitWildcard = true;
918929
} else {
919930
// Looking at something like "{name=wildcard}".
920931
varName = seg.substring(0, i).trim();
@@ -957,6 +968,10 @@ private static ImmutableList<Segment> parseTemplate(String template) {
957968
}
958969
// If the wildcard is implicit, seg will be empty. Just continue.
959970
break;
971+
case "-":
972+
builder.add(Segment.WILDCARD);
973+
implicitWildcard = false;
974+
break;
960975
default:
961976
builder.add(Segment.create(SegmentKind.LITERAL, seg));
962977
}

‎src/test/java/com/google/api/pathtemplate/PathTemplateTest.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,31 @@ public void complexResourceIdMixedSeparators() {
317317
Truth.assertThat(match.get("zone_d")).isEqualTo("europe-west2-b");
318318
}
319319

320+
@Test
321+
public void collectionWildcardMatchingInParent() {
322+
PathTemplate template = PathTemplate.create("v1/publishers/-/books/{book}");
323+
Map<String, String> match =
324+
template.match(
325+
"https://example.googleapis.com/v1/publishers/publisher-abc/books/blockchain_for_babies");
326+
Truth.assertThat(match).isNotNull();
327+
328+
template = PathTemplate.create("/v1/{parent=rooms/-}/blurbs/{blurb}");
329+
match = template.match("https://example.googleapis.com/v1/rooms/den/blurbs/asdf");
330+
Truth.assertThat(match).isNotNull();
331+
}
332+
333+
@Test
334+
public void collectionWildcardMatchingInvalid() {
335+
thrown.expect(ValidationException.class);
336+
PathTemplate.create("v1/publishers/{publisher}/books/-");
337+
}
338+
339+
@Test
340+
public void complexResourceIdPubSubDeletedTopic() {
341+
PathTemplate template = PathTemplate.create("_deleted-topic_");
342+
Truth.assertThat(template).isNotNull();
343+
}
344+
320345
@Test
321346
public void complexResourceIdInParent() {
322347
// One parent has a complex resource ID.

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL