openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][JAVA] Arrays in path parameters are encoded in the wrong way, leading to HTTP Bad Request

Open TimFaehrtBahn opened this issue 6 years ago • 0 comments

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(","))));

TimFaehrtBahn avatar Jun 25 '20 08:06 TimFaehrtBahn