-
-
Notifications
You must be signed in to change notification settings - Fork 121
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
Fixes infinite recursion when casting AnySerializable
to wrong type
#147
Conversation
If a values associated type `Serializable` , equals the values type, `toValue` would infinitely call itself. The test `testWrongCast` reproduces this and I fixed it by comparing the `nextType` with the current type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Many thanks for the fix!
And please also fix the comments below. Thanks 🙇 !
Sources/Defaults/Utilities.swift
Outdated
@@ -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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the indention, we are using tab indention.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed that. Sorry about that.
func testWrongCast() { | ||
let value = Defaults.AnySerializable(false) | ||
XCTAssertEqual(value.get(Bool.self), false) | ||
XCTAssertNil(value.get(String.self)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as the above comment, and please use XCTAssertFalse
instead of XCTAssertEqual
as linter suggests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially used that, but since get
returns on optional, I'd have to provide a default value. That feels less clean.
If a values associated type
Serializable
, equals the values type,toValue
would infinitely call itself.The test
testWrongCast
reproduces this and I fixed it by comparing thenextType
with the current type.