microsoft / react-native-code-push

React Native module for CodePush
http://appcenter.ms
Other
8.87k stars 1.45k forks source link

JsonObject converter unexpectedly crashes with "Unrecognized object: null" #2683

Open vfedosieievdish opened 3 months ago

vfedosieievdish commented 3 months ago

Hello!

Recently I've encountered an issue with react-native-code-push@8.2.1 in my Android project. I've tried to use the CodePushUtils :: convertJsonObjectToWritable() method for easy conversion my objects before passing those data to React.

As I've noticed, there is a problem with conversion of the null values. The root cause is:

  1. The JSONObject.get() method is defined as @NonNull, so never return a null value.
  2. Converter method still makes the obj == null comparison before putting null into a map.
  3. As obj is never null, this comparison is always false, so leads to throwing an exception "Unrecognized object: null"

A demo project to try on: https://github.com/vfedosieievdish/CodepushBugDemoJsonParser

The MainApplication :: testCodePushJSONParser() will be triggered at startup to convert the {"intParam":5,"nullParam":null,"strParam":"my string"} JSON string and crash on conversion the "nullParam":null.

          String jsonStr = "{\"intParam\":5,\"nullParam\":null,\"strParam\":\"my string\"}";
          WritableMap m = CodePushUtils.convertJsonObjectToWritable(new JSONObject(jsonStr));

Apply a proposed patch (see below), and the conversion logic will be executed correctly (the obj will remain null for null values).

diff --git a/node_modules/react-native-code-push/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java b/node_modules/react-native-code-push/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java
index eb099bd..b0eb0c4 100644
--- a/node_modules/react-native-code-push/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java
+++ b/node_modules/react-native-code-push/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java
@@ -67,7 +67,9 @@ public class CodePushUtils {
             String key = it.next();
             Object obj = null;
             try {
-                obj = jsonObj.get(key);
+                if (!jsonObj.isNull(key)) {
+                    obj = jsonObj.get(key);
+                }
             } catch (JSONException jsonException) {
                 // Should not happen.
                 throw new CodePushUnknownException("Key " + key + " should exist in " + jsonObj.toString() + ".", jsonException);
vfedosieievdish commented 3 months ago

react-native-code-push+8.2.1.patch

MikhailSuendukov commented 1 week ago

Hi! A pull request with a fix for this issue has been merged into the master branch. The fix will be included in the next release of react-native-code-push. Therefore, I'm closing this issue.