Fix issue where struct associated values in enums couldn't be encoded/decoded automatically#224
Conversation
…decoded automatically
…potentially resolve issues with complex mangled type names which break tests in the swift wasm runtime
|
@alexjg - before kicking off the workflow again for this one- CI is blocked by two failures- one of which is an issue with rust toolchain versioning. Here's a separate PR with a fix for that: |
|
Apologies for the slow response here. I'll try and find time to review this tomorrow. |
|
Merged in main with fix for updated rust toolchain #225 . |
…cope to potentially resolve issues with complex mangled type names which break tests in the swift wasm runtime" This reverts commit fa441e3.
|
I can't do much more to mimic the CI environment without installing old Xcodes at this point (and I'm lazy). My current version ships with Swift 6.2, so the macOS SDK itself is compiled against 6.2... The 6.1.3 toolchain can't even parse Package.swift because it can't import Foundation. IMO I think we should consider just bumping the CI environment to use a newer version of the swift wasm sdk- perhaps will wait for your feedback @alexjg before I open another PR to tackle the proposed CI changes. |
|
I'm comfortable bumping the CI version, if you have time to put together a PR doing so I'll approve it |
|
I've merged that PR (and thank you!) if you rebase this one we can hopefully get it merged |
|
Nice, happy to help @alexjg. Thanks for your eyes on this stuff. edit* success! 😮💨 |
|
@alexjg - should be ready to roll for your review + merge if and when you have time this week. Thanks again! |

Summary
Fixes encoding and decoding of enums with
structassociated values. Previously, these would fail withSchemaMissingerrors or silently lose data (That silent data loss is what I've been experiencing in my own project).Root Cause
When Swift encodes enums with associated values, it uses
nestedContainer(keyedBy:forKey:)to create a container nested inside the enum case name. The bug was that nested containers were using their parent'simpl.codingPathinstead of their owncodingPathwhen encoding child values.This caused the struct data to be written one level too high in the document hierarchy:
Before (broken):
After (fixed):
This PR assumes that when working in a nested container, we should always trust the container's own
codingPath, not the sharedimpl.codingPath, because the container knows where it actually is in the tree.Changes
Updated path calculation in 6 locations across 2 files to use the container's own
codingPathinstead ofimpl.codingPath. These changes were made in:AutomergeKeyedEncodingContainer.swiftAutomergeUnkeyedEncodingContainer.swift.Testing
Added comprehensive test suite (
EnumAssociatedValueTests.swift) with coverage for a variety of enum + associated value scenarios.All tests now pass.
Impact
I don't believe this is a breaking change.
Enables new functionality:
fixes #223