Skip to content

fix: update the SSE message parsing to not cause number exception#289

Open
suthar26 wants to merge 2 commits intomainfrom
ICP-3942-update-sse-message-parsing
Open

fix: update the SSE message parsing to not cause number exception#289
suthar26 wants to merge 2 commits intomainfrom
ICP-3942-update-sse-message-parsing

Conversation

@suthar26
Copy link
Copy Markdown
Contributor

fix to combat the SSE read error when parsing the message

@suthar26 suthar26 requested a review from a team as a code owner April 16, 2026 15:13
Copilot AI review requested due to automatic review settings April 16, 2026 15:13
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the Android SDK’s Server-Sent Events (SSE) message parsing to prevent runtime number/parsing exceptions when handling realtime update messages, improving robustness of the realtime-config refresh path.

Changes:

  • Extracted SSE message parsing into a dedicated handleSSEMessage function and added JSONException handling.
  • Made parsing tolerant to different SSE payload shapes (outer envelope with "data" vs direct inner payload) and to mixed field types.
  • Updated initEventSource to use the new handler reference.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 165 to 169
private fun handleSSEMessage(messageEvent: MessageEvent?) {
if (messageEvent == null) return

try {
val data = JSONObject(messageEvent.data)
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

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

Add unit tests for the new SSE parsing behavior (envelope with "data" as JSON string vs object vs missing "data", and malformed JSON) to ensure we don't regress back to runtime parsing exceptions and that "type":"refetchConfig" still triggers refetchConfig with lastModified/etag.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

see if u can add a test for this

Comment on lines +201 to +203
} catch (e: JSONException) {
DevCycleLogger.w(e, "SSE Message: Error parsing SSE message data: ${messageEvent.data}")
}
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

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

Avoid logging the full raw SSE payload on JSONException. messageEvent.data may be large and can include config values; logging it can leak sensitive data and create noisy logs. Consider logging only the exception plus a truncated payload, payload length, or an event id/type extracted safely via opt* calls.

Copilot uses AI. Check for mistakes.
Comment on lines 185 to 191
val lastModified = if (innerData.has("lastModified")) {
(innerData.get("lastModified") as Long)
innerData.optLong("lastModified", 0L).takeIf { it > 0 }
} else null

val type = if (innerData.has("type")) {
(innerData.get("type") as String).toLong()
innerData.optString("type", "")
} else ""
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

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

The has(...) checks around optLong/optString are redundant (opt* already handles missing keys/defaults). Simplifying this parsing logic would reduce branching and make future SSE changes easier to maintain.

Copilot uses AI. Check for mistakes.
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