Skip to content

Expanding UriTemplate with empty collections or empty arrays fails with Java 21 #77

@ckumpe

Description

@ckumpe

We tried to use the UriTemplate in projects using Java 21.
E.g. in https://github.com/wcm-io-caravan/caravan-rhyme

The error can reproduced in this project itself by just building it with Java 21:

  1. Update the source code level to 1.8 for the maven-compiler-plugin:
  <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.1</version>
      <configuration>
          <source>1.8</source>
          <target>1.8</target>
      </configuration>
  </plugin>
  1. Run maven package:
$ export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64/
$ ./mvnw clean package
  1. This makes the following tests to fail:
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   TestExtendedSpecTests>AbstractUriTemplateConformanceTest.test:156 Additional Examples 3: Empty Variables->  Template: {/empty_list*} returned java.lang.reflect.InvocationTargetException and did not match any combination
[ERROR]   TestExtendedSpecTests>AbstractUriTemplateConformanceTest.test:156 Additional Examples 3: Empty Variables->  Template: {/empty_list} returned java.lang.reflect.InvocationTargetException and did not match any combination
[ERROR]   TestExtendedSpecTests>AbstractUriTemplateConformanceTest.test:156 Additional Examples 3: Empty Variables->  Template: {?empty_list*} returned java.lang.reflect.InvocationTargetException and did not match any combination
[ERROR]   TestExtendedSpecTests>AbstractUriTemplateConformanceTest.test:156 Additional Examples 3: Empty Variables->  Template: {?empty_list} returned java.lang.reflect.InvocationTargetException and did not match any combination
[ERROR]   TestSpecExamplesBySection>AbstractUriTemplateConformanceTest.test:156 3.2.5 Label Expansion with Dot-Prefix->  Template: X{.empty_keys*} returned java.lang.reflect.InvocationTargetException and did not match any combination
[ERROR]   TestSpecExamplesBySection>AbstractUriTemplateConformanceTest.test:156 3.2.5 Label Expansion with Dot-Prefix->  Template: X{.empty_keys} returned java.lang.reflect.InvocationTargetException and did not match any combination
[INFO] 
[ERROR] Tests run: 461, Failures: 6, Errors: 0, Skipped: 7

The issue is caused by new methods added to Java collections in Java 21:

These methods look like bean properties (named first and last) but throwing a NoSuchElementException if the collection is empty.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions