New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-6563] JSON decoder's internal state messed up after nested decoding failed #3773
Comments
After further inspection, this seems to be caused by the internal container stack not being popped again in e.g. this line in case an error occurs. |
@itaiferber, you have one of these already, right? |
Comment by Thorsten Karrer (JIRA) Having the same problem (filed as radar 36253570 before finding this JIRA). Pretty positive that this is caused by the code line @ffried identified. With Swift's exception-like error model, these mistakes are super easy to make. |
sune (JIRA User) thanks for the radar. I've added it to the SR. As a workaround, it worked for me not to ask the single value container to decode a nested object (lines 28 and 29 in s4_anycodable.swift), but instead call its initializer directly with the |
Comment by Thorsten Karrer (JIRA) @ffried, thanks for the workaround suggestion! Unfortunately, it does not work for me because there is more decoding going on in the calling function using the same decoder. There, I already use the initializer directly, but it still fails because of the broken container stack (needs a keyed container at this place but at the top of the stack is the single value container that never got removed). Addendum: For now, I will just ship a private fixed JSONDecoder implementation. Simply called the pop via defer before the try. |
Confirmed that this is indeed a dupe of SR-6408 and should be resolved with fixes there. |
Attachment: Download
Environment
Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
Target: x86_64-apple-macosx10.9
Additional Detail from JIRA
md5: 55d61821d711ce161aab03b2cca1221c
duplicates:
Issue Description:
When decoding an object's nested object fails, the decoder's internal state is messed up and decoding a valid object fails too.
Given the following code:
The
ModelWrapper
tries to first decodeBaseModel<ModelA>
which fails (due to the fact that the structure ofModelA
doesn't match). ButBaseModel<ModelB>
should work, sinceModelB
matches the JSONs structure. However, this also fails to decode because they key "model" is apparently not present (which it is!).In another example (which is bit more complex), this lead to messed up output for us. We have a setup where we need to decode and process only parts of a (rather huge) JSON structure, but re-encode everything. We've come up with
AnyCodable
which decodes any value in an opaque way and re-encodes it later. While this worked with Swift 4.0.2, it broke in Swift 4.0.3. In Swift 4.0.3, the output does not match the input structure. Nested objects move up the structure tree and end up on the top level.I've attached both, s4_codableError.swift which contains the sample above, and s4_anycodable.swift which contains a reproduction of the aforementioned more complex case.
The text was updated successfully, but these errors were encountered: