diff --git a/Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift b/Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift index a9710354..b40eae07 100644 --- a/Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift +++ b/Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift @@ -144,6 +144,13 @@ struct XMLKeyedDecodingContainer: KeyedDecodingContainerProtocol { referencing: decoder, wrapping: SharedBox(keyedContainer) ) + } else if let singleBox = value as? SingleKeyedBox { + let element = (singleBox.key, singleBox.element) + let keyedContainer = KeyedBox(elements: [element], attributes: []) + container = XMLKeyedDecodingContainer( + referencing: decoder, + wrapping: SharedBox(keyedContainer) + ) } else { throw DecodingError.typeMismatch( at: codingPath, diff --git a/Tests/XMLCoderTests/AdvancedFeatures/SimpleChoiceTests.swift b/Tests/XMLCoderTests/AdvancedFeatures/SimpleChoiceTests.swift index 8ecba95a..30b28333 100644 --- a/Tests/XMLCoderTests/AdvancedFeatures/SimpleChoiceTests.swift +++ b/Tests/XMLCoderTests/AdvancedFeatures/SimpleChoiceTests.swift @@ -85,4 +85,23 @@ final class SimpleChoiceTests: XCTestCase { let decoded = try XMLDecoder().decode([[IntOrString]].self, from: encoded) XCTAssertEqual(original, decoded) } + + func testMixedChoice() throws { + enum Choice: Equatable, Codable { + case one + case two(Int) + case three(String, value: Int) + } + + struct Foo: Equatable, Codable { + var one = Choice.one + var two = Choice.two(10) + var three = Choice.three("qq", value: 20) + } + + let original = Foo() + let encoded = try XMLEncoder().encode(original, withRootKey: "container") + let decoded = try XMLDecoder().decode(Foo.self, from: encoded) + XCTAssertEqual(original, decoded) + } }