react-native-code-push icon indicating copy to clipboard operation
react-native-code-push copied to clipboard

JsonObject converter unexpectedly crashes with "Unrecognized object: null"

Open vfedosieievdish opened this issue 11 months ago • 2 comments

Hello!

Recently I've encountered an issue with [email protected] 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 avatar Mar 26 '24 07:03 vfedosieievdish

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.

MikhailSuendukov avatar Jun 19 '24 14:06 MikhailSuendukov

The fix has been released.

DmitriyKirakosyan avatar Jul 11 '24 06:07 DmitriyKirakosyan