OpenAPITools / OpenAPITools/openapi-generator

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

Open
#12,338 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Swift5 generator output, especially JSONEncodable and the integer query-parameter serialization described in the report. Reproduce the 5.4.0 behavior with a generated client and an integer parameter; done means values such as 100 remain 100 rather than becoming MTAw, with regression coverage if the repository provides a suitable test entry point.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.