OpenAPITools / OpenAPITools/openapi-generator
[BUG] [JAVA] Generated methode ApiClient.parameterToPairs failed to handle empty collections
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the provided openapi-generator-cli command and inspect the generated ApiClient.parameterToPairs entry point, especially the empty-collection path. Use the generated DefaultApiTest.java context to verify that empty collections complete without StringIndexOutOfBoundsException and produce no query parameter.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100