-
Notifications
You must be signed in to change notification settings - Fork 22
Open
Description
I believe I may have found a possible bug and may also have a fix for it.
Automatic encoding / decoding of enums that use structs for one or more associated values always fails
@Test
func nonOptionalStructInEnum() throws {
struct SimpleStruct: Codable, Equatable {
let name: String
}
enum TestEnum: Codable, Equatable {
case withStruct(value: Int, item: SimpleStruct)
}
let testValue = TestEnum.withStruct(value: 10, item: SimpleStruct(name: "test"))
let doc = Document()
try AutomergeEncoder(doc: doc).encode(testValue)
let decoded = try AutomergeDecoder(doc: doc).decode(TestEnum.self)
#expect(decoded == testValue)
}That test will fail:
.SchemaMissing("Nothing in schema exists at [CodingKeys(stringValue: \"withStruct\", intValue: nil), WithStructCodingKeys(stringValue: \"item\", intValue: nil)] - look u returns nil")
After digging around a bit- I think I know why as well.
// AutomergeKeyedEncodingContainer.swift:400
mutating func nestedContainer<NestedKey>(keyedBy _: NestedKey.Type, forKey key: Self.Key) -> ... {
// 🔥 I believe we shoud be using self.codingPath, not impl.codingPath here.
// 🔥 impl.codingPath provides the wrong result here.
let newPath = impl.codingPath + [key]
let nestedContainer = AutomergeKeyedEncodingContainer<NestedKey>(
impl: impl, // Reuses parent's impl
codingPath: newPath,
doc: document
)
return KeyedEncodingContainer(nestedContainer)
}The decoder expected:
// what we would get from codingPath + [key]
path: ["value", "simple", "_0"]
But the data was at:
// what we get from impl.codingPath + [key]
path: ["value", "_0"]
I've got a fork ready to roll where I fix this issue in 6 places, and it adds new test coverage without regressing existing ones. Will link a PR for it soon.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels