Skip to content

Commit 048dec1

Browse files
committed
refactor(api-common): unify dot-segment validation message and remove hasPathWildcard
1 parent 3618d3e commit 048dec1

2 files changed

Lines changed: 11 additions & 32 deletions

File tree

‎sdk-platform-java/api-common-java/src/main/java/com/google/api/pathtemplate/PathTemplate.java‎

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -888,16 +888,14 @@ private String instantiate(Map<String, String> values, boolean allowPartial) {
888888
|| nextNext.kind() != SegmentKind.END_BINDING;
889889
restore(iterator, iterator.nextIndex() - 2);
890890
if (!pathEscape) {
891-
validatePathSegment(var, value, value, false);
891+
validatePathSegment(var, value);
892892
result.append(encodeUrl(value));
893893
} else {
894-
boolean hasPathWildcard =
895-
next.kind() == SegmentKind.PATH_WILDCARD || hasPathWildcard(iterator);
896894
// For a path wildcard or path of length greater 1, split the value and escape
897895
// every sub-segment.
898896
boolean first = true;
899897
for (String subSeg : SLASH_SPLITTER.split(value)) {
900-
validatePathSegment(var, value, subSeg, hasPathWildcard);
898+
validatePathSegment(var, subSeg);
901899
if (!first) {
902900
result.append('/');
903901
}
@@ -1214,31 +1212,10 @@ private static List<Segment> parseComplexResourceId(String seg) {
12141212

12151213
private static final PercentEscaper URL_ESCAPER = new PercentEscaper("-_.~", false);
12161214

1217-
private static boolean hasPathWildcard(ListIterator<Segment> iterator) {
1218-
int start = iterator.nextIndex();
1219-
boolean found = false;
1220-
while (iterator.hasNext()) {
1221-
Segment seg = iterator.next();
1222-
if (seg.kind() == SegmentKind.END_BINDING) {
1223-
break;
1224-
}
1225-
if (seg.kind() == SegmentKind.PATH_WILDCARD) {
1226-
found = true;
1227-
break;
1228-
}
1229-
}
1230-
restore(iterator, start);
1231-
return found;
1232-
}
1233-
1234-
private static void validatePathSegment(
1235-
String var, String fullValue, String segment, boolean hasPathWildcard) {
1215+
private static void validatePathSegment(String var, String segment) {
12361216
if (".".equals(segment) || "..".equals(segment)) {
1237-
if (hasPathWildcard) {
1238-
throw new ValidationException(
1239-
"Value for %s must not contain segments that are exactly . or ..", var);
1240-
}
1241-
throw new ValidationException("Invalid value %s for %s.", fullValue, var);
1217+
throw new ValidationException(
1218+
"Value for %s must not contain segments that are exactly . or ..", var);
12421219
}
12431220
}
12441221

‎sdk-platform-java/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,8 @@ void instantiateRejectsDotInSingleWildcard() {
650650
Assertions.assertThrows(
651651
ValidationException.class,
652652
() -> template.instantiate("project", "my-project", "region", "."));
653-
Truth.assertThat(dotException.getMessage()).isEqualTo("Invalid value . for region.");
653+
Truth.assertThat(dotException.getMessage())
654+
.isEqualTo("Value for region must not contain segments that are exactly . or ..");
654655
}
655656

656657
@Test
@@ -661,7 +662,8 @@ void instantiateRejectsDoubleDotInSingleWildcard() {
661662
Assertions.assertThrows(
662663
ValidationException.class,
663664
() -> template.instantiate("project", "my-project", "region", ".."));
664-
Truth.assertThat(dotDotException.getMessage()).isEqualTo("Invalid value .. for region.");
665+
Truth.assertThat(dotDotException.getMessage())
666+
.isEqualTo("Value for region must not contain segments that are exactly . or ..");
665667
}
666668

667669
@Test
@@ -673,7 +675,7 @@ void instantiateRejectsDotInMultiSegmentWildcard() {
673675
ValidationException.class,
674676
() -> template.instantiate("parent", "projects/my-project/locations/."));
675677
Truth.assertThat(dotException.getMessage())
676-
.isEqualTo("Invalid value projects/my-project/locations/. for parent.");
678+
.isEqualTo("Value for parent must not contain segments that are exactly . or ..");
677679
}
678680

679681
@Test
@@ -685,7 +687,7 @@ void instantiateRejectsDoubleDotInMultiSegmentWildcard() {
685687
ValidationException.class,
686688
() -> template.instantiate("parent", "projects/my-project/locations/.."));
687689
Truth.assertThat(dotDotException.getMessage())
688-
.isEqualTo("Invalid value projects/my-project/locations/.. for parent.");
690+
.isEqualTo("Value for parent must not contain segments that are exactly . or ..");
689691
}
690692

691693
@Test

0 commit comments

Comments
 (0)