Skip to content

[5.7] Fix JSONDecoder superDecoder darwin/linux discrepancy #4631

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 48 additions & 17 deletions Sources/Foundation/JSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,34 +227,46 @@ extension JSONDecoderImpl: Decoder {
@usableFromInline func container<Key>(keyedBy _: Key.Type) throws ->
KeyedDecodingContainer<Key> where Key: CodingKey
{
guard case .object(let dictionary) = self.json else {
switch self.json {
case .object(let dictionary):
let container = KeyedContainer<Key>(
impl: self,
codingPath: codingPath,
dictionary: dictionary
)
return KeyedDecodingContainer(container)
case .null:
throw DecodingError.valueNotFound([String: JSONValue].self, DecodingError.Context(
codingPath: self.codingPath,
debugDescription: "Cannot get keyed decoding container -- found null value instead"
))
default:
throw DecodingError.typeMismatch([String: JSONValue].self, DecodingError.Context(
codingPath: self.codingPath,
debugDescription: "Expected to decode \([String: JSONValue].self) but found \(self.json.debugDataTypeDescription) instead."
))
}

let container = KeyedContainer<Key>(
impl: self,
codingPath: codingPath,
dictionary: dictionary
)
return KeyedDecodingContainer(container)
}

@usableFromInline func unkeyedContainer() throws -> UnkeyedDecodingContainer {
guard case .array(let array) = self.json else {
switch self.json {
case .array(let array):
return UnkeyedContainer(
impl: self,
codingPath: self.codingPath,
array: array
)
case .null:
throw DecodingError.valueNotFound([String: JSONValue].self, DecodingError.Context(
codingPath: self.codingPath,
debugDescription: "Cannot get unkeyed decoding container -- found null value instead"
))
default:
throw DecodingError.typeMismatch([JSONValue].self, DecodingError.Context(
codingPath: self.codingPath,
debugDescription: "Expected to decode \([JSONValue].self) but found \(self.json.debugDataTypeDescription) instead."
))
}

return UnkeyedContainer(
impl: self,
codingPath: self.codingPath,
array: array
)
}

@usableFromInline func singleValueContainer() throws -> SingleValueDecodingContainer {
Expand Down Expand Up @@ -750,11 +762,11 @@ extension JSONDecoderImpl {
}

func superDecoder() throws -> Decoder {
try decoderForKey(_JSONKey.super)
return decoderForKeyNoThrow(_JSONKey.super)
}

func superDecoder(forKey key: K) throws -> Decoder {
try decoderForKey(key)
return decoderForKeyNoThrow(key)
}

private func decoderForKey<LocalKey: CodingKey>(_ key: LocalKey) throws -> JSONDecoderImpl {
Expand All @@ -770,6 +782,25 @@ extension JSONDecoderImpl {
)
}

private func decoderForKeyNoThrow<LocalKey: CodingKey>(_ key: LocalKey) -> JSONDecoderImpl {
let value: JSONValue
do {
value = try getValue(forKey: key)
} catch {
// if there no value for this key then return a null value
value = .null
}
var newPath = self.codingPath
newPath.append(key)

return JSONDecoderImpl(
userInfo: self.impl.userInfo,
from: value,
codingPath: newPath,
options: self.impl.options
)
}

@inline(__always) private func getValue<LocalKey: CodingKey>(forKey key: LocalKey) throws -> JSONValue {
guard let value = dictionary[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(
Expand Down
20 changes: 20 additions & 0 deletions Tests/Foundation/Tests/TestJSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,25 @@ class TestJSONEncoder : XCTestCase {
}
}

func test_notFoundSuperDecoder() {
struct NotFoundSuperDecoderTestType: Decodable {
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
_ = try container.superDecoder(forKey: .superDecoder)
}

private enum CodingKeys: String, CodingKey {
case superDecoder = "super"
}
}
let decoder = JSONDecoder()
do {
let _ = try decoder.decode(NotFoundSuperDecoderTestType.self, from: Data(#"{}"#.utf8))
} catch {
XCTFail("Caught error during decoding empty super decoder: \(error)")
}
}

// MARK: - Test encoding and decoding of built-in Codable types
func test_codingOfBool() {
test_codingOf(value: Bool(true), toAndFrom: "true")
Expand Down Expand Up @@ -1542,6 +1561,7 @@ extension TestJSONEncoder {
("test_encodeDecodeNumericTypesBaseline", test_encodeDecodeNumericTypesBaseline),
("test_nestedContainerCodingPaths", test_nestedContainerCodingPaths),
("test_superEncoderCodingPaths", test_superEncoderCodingPaths),
("test_notFoundSuperDecoder", test_notFoundSuperDecoder),
("test_codingOfBool", test_codingOfBool),
("test_codingOfNil", test_codingOfNil),
("test_codingOfInt8", test_codingOfInt8),
Expand Down