[SR-9385] Encoded data was lost when trying to encode with 2 separated nested keys container with 2 difference Coding Keys on the same key with JSONEncoder and PListEncoder #51851
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Codable
Area → standard library: `Codable` and co.
standard library
Area: Standard library umbrella
Attachment: Download
Environment
Xcode 10.1 Swift 4.2 macOS 10.14.1
Additional Detail from JIRA
md5: f5f48b9464d016e83e4a5e7f0faf4821
Issue Description:
I think I found an edge case bug of the JSONEncoder and the PListEncoder in Swift Standard Library
I have a model for an API of our service which will return a JSON data in a specific format. I'm trying to design a Swift model to conform to the Swift API Design guideline. In order to achieve that, we want to flatten the properties up to the type. We can use the nestedKey variant for that. However since we have some specific properties based on the type of the data, we decide to have another type for holding those information. The bug is that we need to call nestedKey variant on the same key but for 2 Coding Keys types while encoding the type, the second container will always replace the data encoding by the first container. I believe that it's due to This line of code where the JSONEncoder will create a new container when the nestedKeys method is called.
However the decoding works correctly
We want to convert the above JSON data to be something like this
I think we should fix the Encoders logic not to replace the value encoded by the prior container, this will match with the behavior of the Decoder
The text was updated successfully, but these errors were encountered: