PostHog / posthog-ios

PostHog iOS SDK
https://posthog.com/docs/libraries/ios
MIT License
29 stars 37 forks source link

Sanitize dictionaries before serialization #82

Closed marandaneto closed 8 months ago

marandaneto commented 8 months ago

What does this PR do?

Sanitize dictionaries before serialization

Where should the reviewer start?

How should this be manually tested?

Any background context you want to provide?

I've tried multiple approaches for proper sanitization but all of them have edge cases or get overly complex. Similar thoughts as here https://github.com/PostHog/posthog-ios/blob/19064a4f25e74e50604b9954d2e0a18f779d7397/PostHog/Models/PostHogEvent.swift#L33-L34

Codable types only would be awesome but the interoperability with ObjC is suboptimal.

Using Generics would be ideal as well but again bad interoperability with ObjC. I guess we can improve this in the future if we see it but I'd not write tons of code right now, the older version relies onJSONSerialization as well.

I checked https://github.com/SwiftyJSON/SwiftyJSON/blob/master/Source/SwiftyJSON/SwiftyJSON.swift It just fails if there's a nonserializable type. Also, https://swiftunboxed.com/stdlib/json-decoder-decodable/ but generics are not well played. Tried to implement my own version of AnyCodable as well but entered the rabbit whole of https://github.com/asensei/AnyCodable/tree/master

We came to the conclusion that there are too many edge cases, and we have since then completely moved away from fuzzy decoding of Any type.

JSONSerialization.isValidJSONObject always requires an Object or Array, so I cannot just use it for each value of the dict.

What are the relevant tickets?

Screenshots or screencasts (if UI/UX change)

Questions: