OpenAPITools / OpenAPITools/openapi-generator

[BUG] [swift] oneOf handling for form-url encoded requestBodies

Open
#15,511 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Swift 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

When specifying a requestBody using application/x-www-form-urlencoded; charset=utf-8 encoding, oneOf does not work as expected. Instead of generating and using a model for each of the oneOf types as it is the case when using application/json, the generated API method just expects all parameters from all the oneOf types together and ignores the generated request models.
This was a problem even before 6.3.0, but with 6.4.0 and above it got worse as parameters that are only present in some and not in all of the oneOf types are now being generated non-optional which does not make sense.

Generated method signature with application/x-www-form-urlencoded; charset=utf-8

open class func asTokenOauth2Post(password: String, scope: String, username: String, clientId: String? = nil, grantType: APIGrantType_asTokenOauth2Post? = nil, refreshToken: String? = nil) -> AnyPublisher<APIAsTokenOauth2Post200Response, Error>

Generated method signature with application/json

open class func asTokenOauth2Post(aPIAsTokenOauth2PostRequest: APIAsTokenOauth2PostRequest) -> AnyPublisher<APIAsTokenOauth2Post200Response, Error>
openapi-generator version
  • broken in 6.4.0 - current
  • workaround with nilabel parameters in < 6.3.0
OpenAPI declaration file content or url

https://gist.github.com/Jonas1893/baf6076647cc302e4eb7014025977663

Generation Details

Used config

generatorName: swift5
outputDir: ./
library: alamofire
projectName: AuthClient
generateModelAdditionalProperties: false
additionalProperties:
  responseAs: Combine
  readonlyProperties: true
  nonPublicApi: false
  modelNamePrefix: API
  removeMigrationProjectNameClass: true

Steps to reproduce

Using the provided spec and config perform the generation. Also try a second generation run with application/json content-type for the requestBody and observe the behavior described above

Related issues/PRs
Suggest a fix

Content-Type application/x-www-form-urlencoded; charset=utf-8 should use the same approach of handling oneOfs in generated APIs as application/json

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 linked OpenAPI declaration and the swift5 generator using the provided YAML configuration, then compare generation for application/x-www-form-urlencoded; charset=utf-8 and application/json. Inspect how requestBody oneOf schemas become generated method parameters and models; done means form-urlencoded uses the same oneOf model handling as JSON without incorrectly required parameters.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.