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

Add toJSONObject method to OSInAppMessage #1461

Merged
merged 4 commits into from
Nov 5, 2021

Conversation

nan-li
Copy link
Contributor

@nan-li nan-li commented Oct 18, 2021

Description

One Line Summary

Add JSON stringifier to the public OSInAppMessage class and refactor JSON stringifier for internal OSInAppMessageInternal class to return JSON key of "messageId" instead of "id".

Details

Motivation

We want the public OSInAppMessage to have a toJSONObject method for the wrappers to use. This JSON Object only has one item of "messageId". Since IAMs have the property messageId and our documentation refer to messageId, this key was chosen over "id".

The need for this method first arose when adding the InAppMessage Lifecycle Handler to React Native.
We also modified the OSInAppMessageInternal class's toJSONObject to return a JSON key of "messageId" instead of "id" for clarity and consistency to the public class.

Background Context

Previously, the current OSInAppMessageInternal class was named OSInAppMessage before being refactored into an internal InAppMessage class and a public InAppMessage class.

OSInOSInAppMessageInternal extends OSInAppMessage

Worth noting, "id" is expected instead of "messageId" when parsing JSON from the backend, so it is used in the constructor.

Scope

We didn't use any InAppMessage toJSONObject in SDK code, except in unit tests (see below) and those are removed now. The existing OSInAppMessageInternal toJSONObject method was package-private (made public in this PR) so users couldn't have used this method prior to this PR. This means changing the JSON key from "id" to "messageId" has no public API changes (but introduces a new method to the public).

Currently, all of the OSInAppMessage objects are actually OSInAppMessageInternal objects. We don't create any superclass OSInAppMessage objects in the existing codebase. This means when a user calls OSInAppMessage.toJSONObject(), they will receive the results of OSInAppMessageInternal.toJSONObject().

Testing

Unit testing

Some unit tests were creating an IAM based off the JSON of another IAM by calling its toJSONObject().
With the above changes, some tests in InAppMessageIntegrationTests broke because IAM constructors expect "id" instead of "messageId" when parsing the JSON, so we need to replace that with "id" for creating an IAM.

Added method:

convertIAMtoJSONObject(OSInAppMessageInternal inAppMessage)

  • just replaces key "messageId" with "id"
  • refactored unit test code to use this method instead of toJSONObject()

Removed a constructor:

OSTestInAppMessageInternal(OSInAppMessageInternal inAppMessage)

  • this constructor passed the JSON from inAppMessage.toJSONObject()
  • instead, use convertIAMtoJSONObject and pass this JSON into constructor

Added test:

testInAppMessageInternalToJSONObject_messageId

  • tests OSInAppMessageInternal (not OSInAppMessage)
  • only tests for "messageId", the primary focus

Manual testing

✅ Tested by calling OSInAppMessage.toJSONObject() in demo app:

  • Android 11 Emulator
  • Android 12 Emulator

Example of the JSON

{
  "messageId": "abe479ed-dc54-4446-898a-a82b67cfd5aa",
  "variants": {"all":{"default":"2a8934de-d3c1-4734-8b7c-796cd691456b"}},
  "displayDuration": 0,
  "redisplay": {"limit":1,"delay":0},"triggers":[],
  "has_liquid": false
}

✅ Tested in react-native-onesignal via the InAppMessageLifecycleHandler by logging the message and message.messageId

  • When a user logs the message object, they receive the entire JSON object above, not just the messageId

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes - exposes a new method (will update documentation)

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
  • I have reviewed this PR myself, ensuring it meets each checklist item

This change is Reviewable

@nan-li nan-li added the WIP Work In Progress label Oct 19, 2021
@nan-li nan-li force-pushed the feat/OSInAppMessage-toJSONObject branch from 4031da6 to 3efad9c Compare October 19, 2021 21:14
Copy link
Member

@jkasten2 jkasten2 left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r1, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @nan-li)

@nan-li nan-li removed the WIP Work In Progress label Oct 26, 2021
- Add this method for the wrappers to use
- `OSInAppMessageInternal` extends `OSInAppMessage` so its `toJSONObject` method must be `public` because we can't override to reduce visibility
@nan-li nan-li force-pushed the feat/OSInAppMessage-toJSONObject branch from 3efad9c to 3c59786 Compare November 1, 2021 03:57
- Update JSON stringifier to use key of "messageId" instead of "id"
- "id" is expected instead of "messageId" when parsing JSON from the backend, so it is used in the constructor for `OSInAppMessageInternal`
- some unit tests will create an IAM based off JSON of another IAM by calling toJSONObject()
- toJSONObject uses key of "messageId" so we need to replace that with "id" for creating an IAM

- added convertIAMtoJSONObject() that replaces "messageId" with "id"
- refactored code to use this method instead of toJSONObject()
- removed OSTestInAppMessageInternal constructor that took argument of OSInAppMessageInternal in favor of passing in JSON only
@nan-li nan-li force-pushed the feat/OSInAppMessage-toJSONObject branch from 527c4b0 to 94c7523 Compare November 1, 2021 04:25
- tests OSInAppMessageInternal (not OSInAppMessage)
- only tests "messageId" as this test was added when modifying the JSON key from "id" to "messageId"
@nan-li nan-li added WIP Work In Progress and removed WIP Work In Progress labels Nov 1, 2021
Copy link
Contributor Author

@nan-li nan-li left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 5 files reviewed, 1 unresolved discussion (waiting on @Jeasmine and @jkasten2)


OneSignalSDK/onesignal/src/main/java/com/onesignal/OSInAppMessage.java, line 55 at r1 (raw file):

Previously, nan-li (Nan) wrote…

Ah ok! I have seen that practice but didn't realize it is preferred.

Done!

@nan-li nan-li requested review from a team, jkasten2, Jeasmine, rgomezp and emawby November 3, 2021 18:54
Copy link
Member

@jkasten2 jkasten2 left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 5 of 5 files at r2, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @emawby, @Jeasmine, and @rgomezp)

@nan-li nan-li merged commit bbd8be1 into main Nov 5, 2021
@nan-li nan-li deleted the feat/OSInAppMessage-toJSONObject branch November 5, 2021 17:54
@jkasten2 jkasten2 mentioned this pull request Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants