Fix infinite recursion when casting `AnySerializable` to wrong type (#147)

This commit is contained in:
Leo Mehlig 2023-09-03 08:37:52 +02:00 committed by GitHub
parent bd14dae265
commit 03d5386e58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View File

@ -194,7 +194,7 @@ extension Defaults.Serializable {
return anyObject
}
guard let nextType = T.Serializable.self as? any Defaults.Serializable.Type else {
guard let nextType = T.Serializable.self as? any Defaults.Serializable.Type, nextType != T.self else {
// This is a special case for the types which do not conform to `Defaults.Serializable` (for example, `Any`).
return T.bridge.deserialize(anyObject as? T.Serializable) as? T
}

View File

@ -466,4 +466,10 @@ final class DefaultsAnySerializableTests: XCTestCase {
waitForExpectations(timeout: 10)
}
func testWrongCast() {
let value = Defaults.AnySerializable(false)
XCTAssertEqual(value.get(Bool.self), false)
XCTAssertNil(value.get(String.self))
}
}