Skip to content

[BUG] [JAVA] Generated methode ApiClient.parameterToPairs failed to handle empty collections #17460

@conleos-hoppermann

Description

@conleos-hoppermann

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The generated methode ApiClient.parameterToPairs will create a StringIndexOutOfBoundsException if the methode was called with an empty list:

java.lang.StringIndexOutOfBoundsException: Range [1, 0) out of bounds for length 0

	at java.base/java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:1086)
	at java.base/java.lang.StringBuilder.substring(StringBuilder.java:91)
	at java.base/java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:1038)
	at java.base/java.lang.StringBuilder.substring(StringBuilder.java:91)
	at org.openapitools.client.ApiClient.parameterToPairs(ApiClient.java:513)
	at org.openapitools.client.api.DefaultApiTest.yourEndpointGetTest(DefaultApiTest.java:46)

The generated methode:

  public List<Pair> parameterToPairs(String collectionFormat, String name, Collection value) {
    List<Pair> params = new ArrayList<Pair>();

    // preconditions
    if (name == null || name.isEmpty() || value == null) {
      return params;
    }

    // create the params based on the collection format
    if ("multi".equals(collectionFormat)) {
      for (Object item : value) {
        params.add(new Pair(name, escapeString(parameterToString(item))));
      }
      return params;
    }

    // collectionFormat is assumed to be "csv" by default
    String delimiter = ",";

    // escape all delimiters except commas, which are URI reserved
    // characters
    if ("ssv".equals(collectionFormat)) {
      delimiter = escapeString(" ");
    } else if ("tsv".equals(collectionFormat)) {
      delimiter = escapeString("\t");
    } else if ("pipes".equals(collectionFormat)) {
      delimiter = escapeString("|");
    }

    StringBuilder sb = new StringBuilder() ;
    for (Object item : value) {
      sb.append(delimiter);
      sb.append(escapeString(parameterToString(item)));
    }

    params.add(new Pair(name, sb.substring(delimiter.length())));

    return params;
  }

The exception will be thrown at:

    params.add(new Pair(name, sb.substring(delimiter.length())));

because of trying to skip the first delimiter but at empty list there is no first delimiter.

openapi-generator version

7.2.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Simple API
  version: 1.0.0
paths:
  /your-endpoint:
    get:
      summary: Endpoint that accepts a list of strings as a query parameter
      parameters:
        - name: items
          in: query
          description: A list of strings
          required: false
          schema:
            type: array
            nullable: true
            items:
              type: string
      responses:
        '200':
          description: Successful response
Generation Details

openapi-generator-cli generate -g java --library apache-httpclient -i simple-api.yaml

Steps to reproduce

Related issues/PRs

Suggest a fix

Skip the creation of the params in case of an empty value list, as already happens with name.

    // preconditions
    if (name == null || name.isEmpty() || value == null || value.isEmpty()) {
      return params;
    }

( I will provide a pull request)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions