From 6cf9afd83c05548a8674ed5629516e81e456bc04 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 31 Jan 2022 10:27:17 +0000 Subject: [PATCH] fix(reactnative): deserialize the apiKey from react native events if they are present --- CHANGELOG.md | 3 +++ .../com/bugsnag/android/EventDeserializer.kt | 7 ++++- .../bugsnag/android/EventDeserializerTest.kt | 27 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68a2e1ef42..393df3648c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ * Fixed an issue where feature-flags were not always sent if an OnSendCallback was configured [#1589](https://github.com/bugsnag/bugsnag-android/pull/1589) +* Fix a bug where api keys set in React Native callbacks were ignored + [#1592](https://github.com/bugsnag/bugsnag-android/pull/1592) + ## 5.19.1 (2022-01-21) ### Bug fixes diff --git a/bugsnag-plugin-react-native/src/main/java/com/bugsnag/android/EventDeserializer.kt b/bugsnag-plugin-react-native/src/main/java/com/bugsnag/android/EventDeserializer.kt index c005ef3061..069be57cb5 100644 --- a/bugsnag-plugin-react-native/src/main/java/com/bugsnag/android/EventDeserializer.kt +++ b/bugsnag-plugin-react-native/src/main/java/com/bugsnag/android/EventDeserializer.kt @@ -37,6 +37,10 @@ internal class EventDeserializer( event.context = map["context"] as String? event.groupingHash = map["groupingHash"] as String? + // apiKey if it exists in the map and is not empty + val apiKey = (map["apiKey"] as? String)?.takeIf { it.isNotEmpty() } + apiKey?.let { event.apiKey = apiKey } + // app/device event.app = appDeserializer.deserialize(map["app"] as MutableMap) event.device = deviceDeserializer.deserialize(map["device"] as MutableMap) @@ -62,7 +66,8 @@ internal class EventDeserializer( if (map.containsKey("nativeStack") && event.errors.isNotEmpty()) { runCatching { val jsError = event.errors.first() - val nativeStackDeserializer = NativeStackDeserializer(projectPackages, client.config) + val nativeStackDeserializer = + NativeStackDeserializer(projectPackages, client.config) val nativeStack = nativeStackDeserializer.deserialize(map) jsError.stacktrace.addAll(0, nativeStack) } diff --git a/bugsnag-plugin-react-native/src/test/java/com/bugsnag/android/EventDeserializerTest.kt b/bugsnag-plugin-react-native/src/test/java/com/bugsnag/android/EventDeserializerTest.kt index 4471177f24..23019c2216 100644 --- a/bugsnag-plugin-react-native/src/test/java/com/bugsnag/android/EventDeserializerTest.kt +++ b/bugsnag-plugin-react-native/src/test/java/com/bugsnag/android/EventDeserializerTest.kt @@ -1,6 +1,7 @@ package com.bugsnag.android import com.bugsnag.android.internal.DateUtils +import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNotNull @@ -115,4 +116,30 @@ class EventDeserializerTest { assertFalse(event.isUnhandled) assertTrue(TestHooks.getUnhandledOverridden(event)) } + + @Test + fun deserializeApiKeyOverridden() { + val map: MutableMap = hashMapOf( + "apiKey" to "abc123", + "severity" to "info", + "user" to mapOf("id" to "123"), + "unhandled" to false, + "severityReason" to hashMapOf( + "type" to "unhandledException", + "unhandledOverridden" to true + ), + "breadcrumbs" to listOf(breadcrumbMap()), + "threads" to listOf(threadMap()), + "errors" to listOf(errorMap()), + "metadata" to metadataMap(), + "app" to mapOf("id" to "app-id"), + "device" to mapOf( + "id" to "device-id", + "runtimeVersions" to hashMapOf() + ) + ) + + val event = EventDeserializer(client, emptyList()).deserialize(map) + assertEquals("abc123", event.apiKey) + } }