OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.58k stars 6.52k forks source link

[BUG][SWIFT5] Boolean arguments in GET request get JsonEncoded #12449

Open kanduvisla opened 2 years ago

kanduvisla commented 2 years ago
Description

If a request builder builds a GET URL with query parameters, these parameters get JSON Encoded. This also happens with Boolean values. For example, if I have a property called includeRead=false, it's not added to the URL as ?includeRead=false, but as ?includeRead=ZmFsc2U%3D. This of course makes no sense to the middleware.

openapi-generator version
openapi-generator-cli 5.4.0
  commit : 4a36be7
  built  : 2022-01-31T05:31:36Z
OpenAPI declaration file content or url
{
  "swagger" : "2.0",
  "info" : {
    "description" : "Example",
    "version" : "v1",
    "title" : "Example",
    "termsOfService" : "None"
  },
  "host" : "example.com",
  "basePath" : "/Foo",
  "schemes" : [ "https" ],
  "produces" : [ "application/json" ],
  "paths" : {
    "/api/{country}/v1/messages" : {
      "get" : {
        "tags" : [ "Messages" ],
        "operationId" : "Messages_GetMessages",
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "name" : "includeRead",
          "in" : "query",
          "required" : false,
          "type" : "boolean",
          "default" : false
        }, {
          "name" : "country",
          "in" : "path",
          "required" : true,
          "type" : "string"
        } ],
        "responses" : {
          "200" : {
            "description" : "OK",
            "schema" : {
              "$ref" : "#/definitions/GetMessagesOutput"
            }
          }
        }
      }
    }
  },
  "definitions" : {
    "Message" : {
      "type" : "object",
      "required" : [ "isRead" ],
      "properties" : {
        "body" : {
          "type" : "string"
        },
        "from" : {
          "type" : "string"
        },
        "isRead" : {
          "type" : "boolean"
        },
        "messageId" : {
          "type" : "string"
        },
        "sentTimestamp" : {
          "type" : "string"
        },
        "subject" : {
          "type" : "string"
        },
        "userId" : {
          "type" : "string"
        }
      }
    },
    "GetMessagesOutput" : {
      "type" : "object",
      "properties" : {
        "messages" : {
          "type" : "array",
          "items" : {
            "$ref" : "#/definitions/Message"
          }
        }
      }
    }
  }
}
Generation Details

Run the most basic generate command:

$ openapi-generator generate -i ./test.json -g swift5

If you now look at OpenAPIClient/Classes/OpenAPIs/APIs/MessagesAPI.swift you'll see:

        localVariableUrlComponents?.queryItems = APIHelper.mapValuesToQueryItems([
            "includeRead": includeRead?.encodeToJSON(),
        ])

This line creates a JSON string representation of the boolean value.

gshinnar-sh commented 2 years ago

Seeing similar issues for an array of integers [Int] Each element is encoded as base64 data.