Skip to content

Fix JSONDecoder.decode(_,from:) behavior #4807

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 2 commits into from
Aug 11, 2023
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
22 changes: 11 additions & 11 deletions Sources/Foundation/JSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ open class JSONDecoder {
do {
var parser = JSONParser(bytes: Array(data))
let json = try parser.parse()
return try JSONDecoderImpl(userInfo: self.userInfo, from: json, codingPath: [], options: self.options).unwrap(as: T.self)
return try JSONDecoderImpl(userInfo: self.userInfo, from: json, codingPath: [], options: self.options).unwrap(as: type)
} catch let error as JSONError {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: error))
} catch {
Expand Down Expand Up @@ -292,11 +292,11 @@ extension JSONDecoderImpl: Decoder {
if type == Decimal.self {
return try self.unwrapDecimal() as! T
}
if T.self is _JSONStringDictionaryDecodableMarker.Type {
return try self.unwrapDictionary(as: T.self)
if type is _JSONStringDictionaryDecodableMarker.Type {
return try self.unwrapDictionary(as: type)
}

return try T(from: self)
return try type.init(from: self)
}

private func unwrapDate() throws -> Date {
Expand Down Expand Up @@ -611,8 +611,8 @@ extension JSONDecoderImpl {
try decodeFixedWidthInteger()
}

func decode<T>(_: T.Type) throws -> T where T: Decodable {
try self.impl.unwrap(as: T.self)
func decode<T>(_ type: T.Type) throws -> T where T: Decodable {
try self.impl.unwrap(as: type)
}

@inline(__always) private func decodeFixedWidthInteger<T: FixedWidthInteger>() throws -> T {
Expand Down Expand Up @@ -746,9 +746,9 @@ extension JSONDecoderImpl {
try decodeFixedWidthInteger(key: key)
}

func decode<T>(_: T.Type, forKey key: K) throws -> T where T: Decodable {
func decode<T>(_ type: T.Type, forKey key: K) throws -> T where T: Decodable {
let newDecoder = try decoderForKey(key)
return try newDecoder.unwrap(as: T.self)
return try newDecoder.unwrap(as: type)
}

func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: K) throws
Expand Down Expand Up @@ -926,9 +926,9 @@ extension JSONDecoderImpl {
try decodeFixedWidthInteger()
}

mutating func decode<T>(_: T.Type) throws -> T where T: Decodable {
let newDecoder = try decoderForNextElement(ofType: T.self)
let result = try newDecoder.unwrap(as: T.self)
mutating func decode<T>(_ type: T.Type) throws -> T where T: Decodable {
let newDecoder = try decoderForNextElement(ofType: type)
let result = try newDecoder.unwrap(as: type)

// Because of the requirement that the index not be incremented unless
// decoding the desired result type succeeds, it can not be a tail call.
Expand Down
19 changes: 19 additions & 0 deletions Tests/Foundation/Tests/TestJSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,24 @@ class TestJSONEncoder : XCTestCase {
XCTFail("Caught error during decoding empty super decoder: \(error)")
}
}

func test_childTypeDecoder() {
class BaseTestType: Decodable { }
class ChildTestType: BaseTestType { }

func dynamicTestType() -> BaseTestType.Type {
return ChildTestType.self
}

let decoder = JSONDecoder()
do {
let testType = dynamicTestType()
let instance = try decoder.decode(testType, from: Data(#"{}"#.utf8))
XCTAssertTrue(instance is ChildTestType)
} catch {
XCTFail("Caught error during decoding empty super decoder: \(error)")
}
}

// MARK: - Test encoding and decoding of built-in Codable types
func test_codingOfBool() {
Expand Down Expand Up @@ -1562,6 +1580,7 @@ extension TestJSONEncoder {
("test_nestedContainerCodingPaths", test_nestedContainerCodingPaths),
("test_superEncoderCodingPaths", test_superEncoderCodingPaths),
("test_notFoundSuperDecoder", test_notFoundSuperDecoder),
("test_childTypeDecoder", test_childTypeDecoder),
("test_codingOfBool", test_codingOfBool),
("test_codingOfNil", test_codingOfNil),
("test_codingOfInt8", test_codingOfInt8),
Expand Down