Skip to content

Commit c18077a

Browse files
committed
fix: update version logic
1 parent 1b4b5e9 commit c18077a

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ public Set<String> vars() {
298298

299299
/**
300300
* Returns the set of resource literals. A resource literal is a literal followed by a binding.
301-
* For example, projects/{project} is a literal/binding pair and projects is a resource literal.
301+
* For example, projects/&#123;project&#125; is a literal/binding pair and projects is a resource
302+
* literal.
302303
*/
303304
public Set<String> getResourceLiterals() {
304305
Set<String> canonicalSegments = new java.util.LinkedHashSet<>();
@@ -311,7 +312,7 @@ public Set<String> getResourceLiterals() {
311312
inBinding = false;
312313
} else if (seg.kind() == SegmentKind.LITERAL) {
313314
String value = seg.value();
314-
if (value.matches("^v\\d+.*") || value.matches("^u\\d+.*")) { // just in case
315+
if (value.matches("^v\\d+[a-zA-Z0-9]*$")) { // just in case
315316
continue;
316317
}
317318
if (inBinding) {
@@ -325,16 +326,18 @@ public Set<String> getResourceLiterals() {
325326
}
326327

327328
/**
328-
* Returns the canonical resource name string. A canonical resource name is extracted from the template by finding the version literal,
329-
* then finding the last binding that is a literal/binding pair or named binding,
330-
* and then extracting the segments between the version literal and the last binding.
331-
* For examplem, projects/{project} is a literal/binding pair. {bar=projects/*/locations/*/bars/*} is a named binding.
329+
* Returns the canonical resource name string. A canonical resource name is extracted from the
330+
* template by finding the version literal, then finding the last binding that is a
331+
* literal/binding pair or named binding, and then extracting the segments between the version
332+
* literal and the last binding.
332333
*/
334+
// For example, projects/{project} is a literal/binding pair. {bar=projects/*/locations/*/bars/*}
335+
// is a named binding.
333336
public String getCanonicalResourceName(Set<String> knownResources) {
334337
if (knownResources == null) {
335338
return "";
336339
}
337-
340+
338341
int firstBindingIndex = -1;
339342
for (int i = 0; i < segments.size(); i++) {
340343
if (segments.get(i).kind() == SegmentKind.BINDING) {
@@ -352,7 +355,7 @@ public String getCanonicalResourceName(Set<String> knownResources) {
352355
Segment seg = segments.get(i);
353356
if (seg.kind() == SegmentKind.LITERAL) {
354357
String value = seg.value();
355-
if (value.matches("^v\\d+.*") || value.matches("^u\\d+.*")) {
358+
if (value.matches("^v\\d+[a-zA-Z0-9]*$")) {
356359
startIndex = i + 1;
357360
break;
358361
}
@@ -373,7 +376,7 @@ public String getCanonicalResourceName(Set<String> knownResources) {
373376
} else if (seg.kind() == SegmentKind.END_BINDING) {
374377
inBinding = false;
375378
boolean isValidPair = false;
376-
379+
377380
if (literalCountInBinding > 1) {
378381
// Named bindings are unconditionally considered pairs
379382
isValidPair = true;
@@ -395,14 +398,14 @@ public String getCanonicalResourceName(Set<String> knownResources) {
395398
}
396399
}
397400
}
398-
401+
399402
if (isValidPair) {
400403
lastValidEndBindingIndex = i;
401404
}
402405
} else if (seg.kind() == SegmentKind.LITERAL) {
403406
if (inBinding) {
404407
String value = seg.value();
405-
if (!value.matches("^v\\d+.*") && !value.matches("^u\\d+.*")) {
408+
if (!value.matches("^v\\d+[a-zA-Z0-9]*$")) {
406409
literalCountInBinding++;
407410
}
408411
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,16 @@ void testGetCanonicalResourceName_simplePath() {
960960
.isEqualTo("projects/{project}/locations/{location}/widgets/{widget}");
961961
}
962962

963+
@Test
964+
void testGetCanonicalResourceName_v1beta1WithSimplePath() {
965+
Set<String> knownResources = ImmutableSet.of("projects", "locations", "instances", "widgets");
966+
PathTemplate template =
967+
PathTemplate.create(
968+
"/compute/v1beta1/projects/{project}/locations/{location}/widgets/{widget}");
969+
Truth.assertThat(template.getCanonicalResourceName(knownResources))
970+
.isEqualTo("projects/{project}/locations/{location}/widgets/{widget}");
971+
}
972+
963973
@Test
964974
void testGetCanonicalResourceName_regexVariables() {
965975
Set<String> knownResources = ImmutableSet.of("projects", "locations", "instances", "widgets");

0 commit comments

Comments
 (0)