Skip to content

Commit e23c49a

Browse files
authored
Merge pull request #1240 from johnno1962e/archiver2
Fix to NSKeyedArchiver when object not AnyHashable
2 parents 58add20 + a573de7 commit e23c49a

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

Foundation/NSKeyedArchiver.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,11 @@ open class NSKeyedArchiver : NSCoder {
553553
}
554554

555555
// check replacement cache
556-
objectToEncode = self._replacementMap[object as! AnyHashable]
557-
if objectToEncode != nil {
558-
return objectToEncode
556+
if let hashable = object as? AnyHashable {
557+
objectToEncode = self._replacementMap[hashable]
558+
if objectToEncode != nil {
559+
return objectToEncode
560+
}
559561
}
560562

561563
// object replaced by NSObject.replacementObject(for:)

TestFoundation/TestNSKeyedArchiver.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class TestNSKeyedArchiver : XCTestCase {
106106
("test_archive_user_class", test_archive_user_class),
107107
("test_archive_uuid_bvref", test_archive_uuid_byref),
108108
("test_archive_uuid_byvalue", test_archive_uuid_byvalue),
109+
("test_archive_unhashable", test_archive_unhashable),
109110
]
110111
}
111112

@@ -312,4 +313,30 @@ class TestNSKeyedArchiver : XCTestCase {
312313
let uuid = UUID()
313314
return test_archive(uuid, classes: [NSUUID.self])
314315
}
316+
317+
func test_archive_unhashable() {
318+
let data = """
319+
{
320+
"args": {},
321+
"headers": {
322+
"Accept": "*/*",
323+
"Accept-Encoding": "deflate, gzip",
324+
"Accept-Language": "en",
325+
"Connection": "close",
326+
"Host": "httpbin.org",
327+
"User-Agent": "TestFoundation (unknown version) curl/7.54.0"
328+
},
329+
"origin": "0.0.0.0",
330+
"url": "https://httpbin.org/get"
331+
}
332+
""".data(using: .utf8)!
333+
do {
334+
let json = try JSONSerialization.jsonObject(with: data)
335+
_ = NSKeyedArchiver.archivedData(withRootObject: json)
336+
XCTAssert(true, "NSKeyedArchiver.archivedData handles unhashable")
337+
}
338+
catch {
339+
XCTFail("test_archive_unhashable, de-serialization error \(error)")
340+
}
341+
}
315342
}

0 commit comments

Comments
 (0)