Fix encoding of OpenAPI{Object,Value}Container to allow multiple encodings in anyOf/allOf #156
+203
−41
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 ananyOf
(or anallOf
), which contains more than 1 of these types (OpenAPIValueContainer
orOpenAPIObjectContainer
). When it hits that case, it crashes at runtime, because you can't encode multiple composite types into a single encoder usingsingleValueContainer()
.However, you can do that when using the proper
container(keyedBy:)
andunkeyedContainer()
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
andOpenAPIObjectContainer
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.