Reconsider closed enums for string values decoded from API responses
Maintainers usually reply within 3 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Needs clarification
- Activity status
- Active
- Tech stack
- openapi, swift
- Domain
- api, backend-api-design
Research direction
Start by reading the response-side enum examples and the four options in the issue, especially the 16 generated StatusPayload enums and Batch.StatusPayload. Compare how the Swift generator handles the proposed anyOf pattern with the Java and Python examples cited. The issue is done when a direction is chosen for existing and future response enums, including whether it applies generally or only to selected fields.
Written by the indexing model from the issue text.
Description
swift-openapi-generator turns every string enum in the spec into a closed @frozen Swift enum. Many of these types are public, through Components.Schemas or typealiases such as ResponseObject.Status. That has different consequences for values we send and values we decode.
Values we send (request side)
When OpenAI adds a value, a closed enum costs two things:
- The API-breakage check flags the new case, and we allowlist it with a changelog entry (as in
c5d14f7). CONTRIBUTING already lists new enum cases as an accepted additive change. - Users can't send the new value until we regenerate and release.
Neither is a real problem.
Values we decode (response side)
If the API returns a value the enum doesn't know, decoding throws and the whole call fails. That would hit already-shipped apps, which no SDK release can fix. Examples:
ResponseObject.Status(Components.Schemas.Response.Value3Payload.StatusPayload)Components.Schemas.Batch.StatusPayload, exposed directly by the Batch API (MacPaw/OpenAI#401)- 16 generated
StatusPayloadenums in total, plus other response-side enums
Java models every string enum as an open type: known constants plus an _UNKNOWN member, including Batch.Status. Python uses Literal types, which don't fail on unknown values at runtime.
Options
- Keep closed enums. This is what happens today. The risk is low per field, but a failure takes down every call that returns the field.
- Hand-written open types in
Edited/orFacade/. For example, aRawRepresentablestruct with static constants for the fields that matter most, like statuses. - Decode unknown values tolerantly. For example, a
ParsingOptionsflag or a custominit(from:)that maps unknown raw values to a fallback. This needs anunknown-style case, which generated enums don't have. - Use the generator's open-enum pattern. Rewrite selected schemas in
prepare_openapi.pyasanyOf: [<enum>, {type: string}], which swift-openapi-generator turns into a struct with a known-value field and a raw-string fallback.
To decide
- Whether response-side enums should be open in general, or only for selected fields such as statuses.
- Which option to use, and whether it applies to existing types (a public API change) or only to new ones.
- Dominant language
- Swift
- Stars
- 2.9k
- Forks
- 519
- Avg merge
- 4d 9h
- Merged PRs (30d)
- 13
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from MacPaw/OpenAI
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 3 days
-
Enable batch promptsOpen
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
MacPaw/OpenAI#73 · 1 comment · 1 reaction ·
Maintainers usually reply within 3 days
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
Maintainers usually reply within 3 days
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
Maintainers usually reply within 3 days
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
Maintainers usually reply within 3 days
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
freestylefly/WeChatBridge#44 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 Half a day Newbie friendliness 62/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
capawesome-team/capacitor-plugins#1134 ·
Maintainers usually reply within 1 day
-
OpenAILanguageModel replays tool-call arguments as GeneratedContent's Codable form, not JSONPossibly taken @gtokman claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
huggingface/AnyLanguageModel#324 ·
Maintainers usually reply within 1 day
-
enhancement help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
kabirvashisht4-glitch/Vet#22 ·