OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] Arrays in path parameters are encoded in the wrong way, leading to HTTP Bad Request
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
The provided OpenAPI specification has array paramters as path parameters and the current generator generates wrong code for serialization by calling Array.toString() which leads to a string representation containing charaters ("[" and "]") that are not allowed within request path by spec.
openapi-generator version
openapi-generator-cli-4.3.1.jar
OpenAPI declaration file content or url
[...]
"/boards/boards/public/departure/{evaNumbers}": {
"get": {
"tags": [
"Boards Public"
],
"description": "Returns a departure board [Abfahrtstafel] for all public transports [Oeffentliche Verkehre].",
"operationId": "boardDeparture",
"parameters": [
{
"name": "evaNumbers",
"in": "path",
"description": "list of eva numbers of stations [Bahnhoefe] to get board for",
"required": true,
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
},
[...]
Command line used for generation
java -jar openapi-generator-cli-4.3.1.jar generate -g java -i swagger.yml --library native --api-package innoapi.api --invoker-package innoapi.invoker --model-package innoapi.model
Steps to reproduce
Call this.boardsPublicAPI.boardDeparture(Arrays.asList("Test1", "Test2"), null, null, null, null, null, null, null, true);
Related issues/PRs
/
Suggest a fix
The problem is the generated code:
String localVarPath = "/boards/boards/public/departure/{evaNumbers}"
.replace("{evaNumbers}", ApiClient.urlEncode(evaNumbers.toString()));
This leads to path parameters getting serialized as
[val1, val2]
but "[" and "]" are not allowed in path, hence a HTTP 400 = Bad Request is thrown.
Working solution and fix suggestion:
String localVarPath = "/boards/boards/public/departure/{evaNumbers}"
.replace("{evaNumbers}", ApiClient.urlEncode(evaNumbers.stream().collect(Collectors.joining(","))));
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
Start by reproducing the Java client generated with openapi-generator-cli-4.3.1 using the supplied OpenAPI path parameter and the native library. Inspect the generated boardDeparture path serialization, then verify that array values no longer produce bracketed Array.toString() output and that the request succeeds without HTTP 400.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100