Repository: googleapis/sdk-platform-java
Issue: PathTemplate.toString() not reducing wildcards

`PathTemplate.toString()` does not pretty print the template pattern correctly. It does not reduce wildcard bindings `{name=*}` to `{name}` as mentioned in the code.

#### Environment details

OS type and version: MacOS
Java version: Java 19
api-common-java version(s): 2.6.3

#### Steps to reproduce

```java
Assertions.assertEquals("libraries/{library}/books/{book}", PathTemplate.create("libraries/{library}/books/{book}").toString());
```

####  Stack trace
```
org.opentest4j.AssertionFailedError: 
Expected :libraries/{library}/books/{book}
Actual   :libraries/{library=*}/books/{book=*}
```

I believe the problem is in `PathTemplate.peek()`. It looks like the `success` flag is incorrectly initialized to `false`.

https://github.com/googleapis/gapic-generator-java/blob/17d133bd625a9fe203019514aedf63e9fdad97f8/api-common-java/src/main/java/com/google/api/pathtemplate/PathTemplate.java#L1120-L1134


Changed files:
api-common-java/src/main/java/com/google/api/pathtemplate/PathTemplate.java
api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java
gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/HttpRuleParser.java

diff --git a/api-common-java/src/main/java/com/google/api/pathtemplate/PathTemplate.java b/api-common-java/src/main/java/com/google/api/pathtemplate/PathTemplate.java
index cc9f4c110..f43ad7ab8 100644
--- a/api-common-java/src/main/java/com/google/api/pathtemplate/PathTemplate.java
+++ b/api-common-java/src/main/java/com/google/api/pathtemplate/PathTemplate.java
@@ -1119,7 +1119,7 @@ public class PathTemplate {
   // the list iterator in its state.
   private static boolean peek(ListIterator<Segment> segments, SegmentKind... kinds) {
     int start = segments.nextIndex();
-    boolean success = false;
+    boolean success = true;
     for (SegmentKind kind : kinds) {
       if (!segments.hasNext() || segments.next().kind() != kind) {
         success = false;
diff --git a/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java b/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java
index 47747a115..c6c0e29e5 100644
--- a/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java
+++ b/api-common-java/src/test/java/com/google/api/pathtemplate/PathTemplateTest.java
@@ -840,6 +840,27 @@ public class PathTemplateTest {
     Truth.assertThat(url).isEqualTo("v1/shelves/s1/books/b1");
   }
 
+  @Test
+  public void testTemplateStringConversionWithUnbound() {
+    PathTemplate template = PathTemplate.create("v1/shelves/*/books/**");
+    Truth.assertThat(template.toRawString()).isEqualTo("v1/shelves/{$0=*}/books/{$1=**}");
+    Truth.assertThat(template.toString()).isEqualTo("v1/shelves/*/books/**");
+  }
+
+  @Test
+  public void testTemplateStringConversionWithBinding() {
+    PathTemplate template = PathTemplate.create("v1/shelves/{shelf=*}/books/{book=**}");
+    Truth.assertThat(template.toRawString()).isEqualTo("v1/shelves/{shelf=*}/books/{book=**}");
+    Truth.assertThat(template.toString()).isEqualTo("v1/shelves/{shelf}/books/{book=**}");
+  }
+
+  @Test
+  public void testSubTemplate() {
+    PathTemplate template = PathTemplate.create("v1/shelves/{shelf}/books/{book=books/*/**}");
+    Truth.assertThat(template.subTemplate("shelf").toString()).isEqualTo("*");
+    Truth.assertThat(template.subTemplate("book").toString()).isEqualTo("books/*/**");
+  }
+
   private static void assertPositionalMatch(Map<String, String> match, String... expected) {
     Truth.assertThat(match).isNotNull();
     int i = 0;
diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/HttpRuleParser.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/HttpRuleParser.java
index 87bcd76ab..b26363330 100644
--- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/HttpRuleParser.java
+++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/HttpRuleParser.java
@@ -217,7 +217,7 @@ public class HttpRuleParser {
     if (pattern == null || pattern.isEmpty()) {
       return varPattern;
     }
-    Matcher m = TEMPLATE_VALS_PATTERN.matcher(PathTemplate.create(pattern).toString());
+    Matcher m = TEMPLATE_VALS_PATTERN.matcher(PathTemplate.create(pattern).toRawString());
 
     while (m.find()) {
       varPattern.put(m.group("var"), m.group("template"));