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

Fix PaywallEvents failing to deserialize #4520

Merged
merged 7 commits into from
Nov 27, 2024
Merged

Fix PaywallEvents failing to deserialize #4520

merged 7 commits into from
Nov 27, 2024

Conversation

vegaro
Copy link
Contributor

@vegaro vegaro commented Nov 25, 2024

It looks like PaywallEvents are broken since 5.8.0

image

The issue is that paywall_revision is being serialized as a boolean in the file, because AnyEncodable is interpreting it the 0 as a boolean

@vegaro vegaro added the pr:fix A bug fix label Nov 25, 2024
@vegaro vegaro marked this pull request as ready for review November 26, 2024 12:24
@@ -52,6 +52,34 @@ class PaywallEventSerializerTests: TestCase {
expect(try event.encodeAndDecode()) == event
}

func testEncodingBooleans() throws {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails in main

@@ -57,6 +57,33 @@ class PaywallEventsRequestTests: TestCase {
assertSnapshot(matching: requestEvent, as: .formattedJson)
}

func testCanInitFromDeserializedEvent() throws {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails in main. It's very similar to the other test, but it uses the EventsRequest.PaywallEvent(storedEvent: initializer

@@ -66,7 +93,7 @@ class PaywallEventsRequestTests: TestCase {

private static let eventData: PaywallEvent.Data = .init(
offeringIdentifier: "offering",
paywallRevision: 5,
paywallRevision: 0,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to 0 since the decoder will have more trouble understanding it's an int and not a boolean

@@ -16,17 +16,16 @@ import Foundation
/// Contains the necessary information for storing and sending events.
struct StoredEvent {

private(set) var encodedEvent: AnyEncodable
private(set) var encodedEvent: String
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to just store a string since it's just simpler, easier to understand and less bug prone

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my understanding, does this change what the event looks like on disk? Wasn't it a string before?

Copy link
Contributor Author

@vegaro vegaro Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct. It will be an encoded String though. Before it was an encoded dictionary which fails to decode as a String.

Example of before:

{"user_id":"$RCAnonymousID:5b2741807e1b4d939767b3d2ed4fbe9a","event":{"impression":{"_0":{"date":"2024-11-25T20:30:42Z","id":"0B593A2F-1553-41DC-9CFD-16A12D89C418"},"_1":{"offering_identifier":"offering","locale_identifier":"en_ES","session_identifier":"2E57D4B8-55E3-4B24-93B1-55EA5F46DE38","display_mode":"full_screen","dark_mode":false,"paywall_revision":0}}}}

Example of after:

{"feature":"paywalls","event":"{\"impression\":{\"_1\":{\"paywall_revision\":0,\"session_identifier\":\"AF96931D-2A9F-4BA3-BBD2-DB255F2DCF0F\",\"display_mode\":\"full_screen\",\"offering_identifier\":\"offering\",\"locale_identifier\":\"en_ES\",\"dark_mode\":false},\"_0\":{\"date\":\"2024-11-26T10:20:23Z\",\"id\":\"BD9897AE-2C6D-4824-A9FE-7DBF20B8E313\"}}}","user_id":"$RCAnonymousID:5b2741807e1b4d939767b3d2ed4fbe9a"}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright thanks! So does this mean we won't be able to decode whatever events were already stored on disk (encoded with the previous version)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet, thanks for explaining!

return lhs.userID == rhs.userID &&
lhs.feature == rhs.feature &&
(lhsValue as NSDictionary).isEqual(to: rhsValue)
return NSDictionary(dictionary: lhsDict).isEqual(rhsDict)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mainly used for tests assertions so not super worried about it decoding events. It's the only way to be honest, otherwise the order of the keys in the JSON would make it fail

@vegaro vegaro requested a review from a team November 26, 2024 13:00
@vegaro vegaro requested review from MarkVillacampa, fire-at-will and a team November 26, 2024 14:44
Copy link
Member

@JayShortway JayShortway left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to have this fixed! Just a question.

@@ -16,17 +16,16 @@ import Foundation
/// Contains the necessary information for storing and sending events.
struct StoredEvent {

private(set) var encodedEvent: AnyEncodable
private(set) var encodedEvent: String
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my understanding, does this change what the event looks like on disk? Wasn't it a string before?

@vegaro vegaro merged commit fd4af48 into main Nov 27, 2024
7 checks passed
@vegaro vegaro deleted the fix-events branch November 27, 2024 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants