-
Notifications
You must be signed in to change notification settings - Fork 2
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
Quick fix to bug with push token request equality operator #117
Conversation
// At this time, decoding from JSON does not preserve equality check for push token requests | ||
// But at least it should not crash to compare them | ||
val bRequestDecoded = KlaviyoApiRequestDecoder.fromJson(bRequest.toJson()) | ||
assertNotEquals(aRequest, bRequestDecoded) |
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.
What's different between these?
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.
The cause of the bug was that we use a different constructor when decoding this class from JSON. So the first is just create 2 objects, test their equality. The second is encode and decode it, and re-test equality
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.
So previously this test would have reproduced the crash?
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.
Previously, testing equality failed on a decoded json object because the lateinit var was not initialized. With this patch, equality check should not crash, though expected behavior is that it will not be equal anymore, because the payload body has been mutated
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.
Exactly, this was my failing-test repro.
sdk/analytics/src/main/java/com/klaviyo/analytics/networking/requests/PushTokenApiRequest.kt
Outdated
Show resolved
Hide resolved
sdk/analytics/src/main/java/com/klaviyo/analytics/networking/requests/PushTokenApiRequest.kt
Outdated
Show resolved
Hide resolved
…it keeps the idea of having up to date metadata at send time.
} | ||
|
||
// Update body to include Device metadata whenever the body is retrieved (typically during sending) so the latest data is included | ||
override val requestBody: String? |
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.
requestBody
is invoked at send time to format the body JSON for the network request. but it is not invoked when json encoding. This way, decoding from json will expect the same initial body payload.
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.
Nice!
@@ -11,7 +11,7 @@ import com.klaviyo.core.Registry | |||
import com.klaviyo.core.networking.NetworkMonitor | |||
|
|||
/** | |||
* Exception that is thrown when the the Klaviyo API token is missing from the config | |||
* Exception that is thrown when the Klaviyo API token is missing from the config |
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.
can't help fixing grammar things when i see them
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.
is there a linter that helps with this?
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.
Good Q, grammar problems are highlighted in a shade of red in my IDE color theme, so it always catches my eye.
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.
we should find a way to share some IDE configs
override fun equals(other: Any?): Boolean { | ||
return when (other) { | ||
is PushTokenApiRequest -> initialBody == other.initialBody | ||
is PushTokenApiRequest -> body.toString() == other.body.toString() |
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.
Sorry if I missed it, what's the reasoning behind using toString() vs comparing body to other.body?
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 didn't actually try that, let me see. My assumption was separate object instances won't appear equal
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.
Ahh might compare references?
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.
one difficult thing is order:
JSONObject("{'keyA': 'value', 'keyB': 1}").toString() == JSONObject("{'keyB': 1, 'keyA': 'value'}").toString()
// false
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.
but yeah, without to string, its a reference check:
JSONObject("{'keyA': 'value'}") == JSONObject("{'keyA': 'value'}")
// false
JSONObject("{'keyA': 'value'}").toString() == JSONObject("{'keyA': 'value'}").toString()
// true
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.
Thanks for the check kenny. Yeah I was surprised the order thing didn't cause my test to fail actually. Unless its just coincidentally passing. I had to add a json compare function to the unit tests for that same reason.
Description
Fixes #116 by removing the lateinit property. Instead, keep
body
itself as the initial value, and userequestBody
to append metadata at send time. This way the equality operator will work (and not crash) the same after decoding from json.Check List
Changelog / Code Overview
requestBody
method that is invoked only at send time.Test Plan
Related Issues/Tickets
#116