Skip to content
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

Codable+Sweeter Overloading Breaks Implementations #4

Closed
pschneider opened this issue Feb 22, 2020 · 4 comments
Closed

Codable+Sweeter Overloading Breaks Implementations #4

pschneider opened this issue Feb 22, 2020 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@pschneider
Copy link

Description of the problem:

Hi,

we just evaluated using your library "MultiSelectSegmentedControl". After implementing it we suddenly noticed some crashes which we could trace back to the extension included in this library Codable+Sweeter.

In this file you're overloading Decodable with a new init:

extension Decodable {
    /// Sweeter: Create object from a dictionary
    public init?(from: Any) {
        guard let data = try? JSONSerialization.data(withJSONObject: from, options: .prettyPrinted) else { return nil }
        guard let decodedSelf = try? JSONDecoder().decode(Self.self, from: data) else { return nil }
        self = decodedSelf
    }
}

As you're overloading the init with type from: Any this is being used unintentionally by other implementations.

For example, we're also using the library R.swift which generates the following line to use when passing arguments to translations:

fileprivate static let applicationLocale = hostingBundle.preferredLocalizations.first.flatMap(Locale.init) ?? Locale.current

After adding your Library this line (Locale.init in particular) is using your overloaded init instead of the default Codable one. And this causes the app to crash as, of course, this is not passing any valid JSON as an argument.

Normally it should use

extension Locale : Codable {
    /// Creates a new instance by decoding from the given decoder.
    ///
    /// This initializer throws an error if reading from the decoder fails, or
    /// if the data read is corrupted or otherwise invalid.
    ///
    /// - Parameter decoder: The decoder to read data from.
    public init(from decoder: Decoder) throws

    /// Encodes this value into the given encoder.
    ///
    /// If the value fails to encode anything, `encoder` will encode an empty
    /// keyed container in its place.
    ///
    /// This function throws an error if any values are invalid for the given
    /// encoder's format.
    ///
    /// - Parameter encoder: The encoder to write data to.
    public func encode(to encoder: Encoder) throws
}

I guess in general it is pretty dangerous to overload something with type Any but maybe this can also be fixed by using another Keyword instead of from:.

@pschneider pschneider added the bug Something isn't working label Feb 22, 2020
@yonat
Copy link
Owner

yonat commented Feb 22, 2020

Good point! If you change the constructor in your copy of Codable+Sweeter to

    public init?(dictionary: [String: Any]) {
        guard let data = try? JSONSerialization.data(withJSONObject: dictionary, options: .prettyPrinted) else { return nil }
        guard let decodedSelf = try? JSONDecoder().decode(Self.self, from: data) else { return nil }
        self = decodedSelf
    }

Does it solve the problem for you? If it does, I'll change it here in master.

@askiiRobotics
Copy link

Hi!
I've faced with the same problem on my project.

This is my podfile content:
- MultiSelectSegmentedControl (2.2.0): - SweeterSwift

@yonat Yes this solutions seems to fix the issue. I still not sure is it affects MultiSelectSegmentedControl.

@yonat yonat closed this as completed in be314e3 Feb 23, 2020
@yonat
Copy link
Owner

yonat commented Feb 23, 2020

Thanks @askiiRobotics I pushed the fix. I hope it helps @pschneider too.

@pschneider
Copy link
Author

@yonat @askiiRobotics
Sorry was busy last days. Yes, that fixes it for us as well. Thank you very much for the fast change 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants