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.45k stars 6.49k forks source link

[BUG][Swift5] Parameters unexpectedly base64-encoded. #12338

Open reubits opened 2 years ago

reubits commented 2 years ago

Bug Report Checklist

Description

With the swift5 generator version5.4.0, JSONEncodables default to Data.JSONEncodable which uses base64EncodedString. It seems like this should not be intentional given that we also have these extensions which apparently are not being used as a result.

extension Bool: JSONEncodable {}
extension Float: JSONEncodable {}
extension Int: JSONEncodable {}
// etc

extension JSONEncodable {
    func encodeToJSON() -> Any { self }
}

The API we are using does not expect base64-encoded params and this is leading to bad request errors (400). This problem was introduced in 5.4.0 after upgrading from 5.3.0.

openapi-generator version

5.4.0. This appears to be a regression.

Steps to reproduce

Generate against a spec with an integer query parameter and observe that it gets base64-encoded.

e.g. expected: limit=100, got: limit=MTAw

Related issues/PRs

I believe this PR introduced the issue:

https://github.com/RitualCo/ios/pull/2725

Suggest a fix

It can be patched by doing

extension JSONEncodable where Self == Int {
  func encodeToJSON() -> Any { return self as Any }
}
// etc
schroepf commented 2 years ago

I can confirm this issue. My fix was to downgrade to openapi-generator version 5.3.1 (last stable version). Seems like the issue was introduced with this change: https://github.com/OpenAPITools/openapi-generator/pull/11202/files#diff-107652f98524df152f1ec2dba250090381397e99b5e243b603a509e84dcfa882