Skip to content

Fix encoding of OpenAPI{Object,Value}Container to allow multiple encodings in anyOf/allOf #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

czechboy0
Copy link
Contributor

@czechboy0 czechboy0 commented Aug 15, 2025

Motivation

Fixes apple/swift-openapi-generator#808.

More background: We used to use singleValueContainer(), provided by Codable, to encode not just primitive values, but also composite values like objects and arrays. This works in most cases, but not when you have an anyOf (or an allOf), which contains more than 1 of these types (OpenAPIValueContainer or OpenAPIObjectContainer). When it hits that case, it crashes at runtime, because you can't encode multiple composite types into a single encoder using singleValueContainer().

However, you can do that when using the proper container(keyedBy:) and unkeyedContainer() APIs, as that way you're not overwriting the value that's already there, but amending it.

Modifications

Improve the encoding and decoding logic of OpenAPIValueContainer and OpenAPIObjectContainer to use the more appropriate Codable APIs when coding composite types, like objects and arrays.

Result

Now we can have an anyOf/allOf with multiple of these types, and they encode/decode correctly. I also verified that the original example reported by the user works with this patch.

Test Plan

Added more unit tests and ensured all still pass, also verified that Swift OpenAPI Generator's tests pass when pointed at this patched version of Runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to encode multiple OpenAPIObjectContainer values into a single anyOf
2 participants