From 7e4cd5338354abff209828ee9c2ced24ecf01581 Mon Sep 17 00:00:00 2001 From: Eric Samelson Date: Thu, 22 Mar 2018 10:47:40 -0700 Subject: [PATCH] fix ReadableNativeMap.toHashMap() for nested maps and arrays Summary: Commit https://github.com/facebook/react-native/commit/7891805d22e3fdc821961ff0ccc5c450c3d625c8 broke the previous behavior of `ReadableNativeMap.toHashMap()` for nested maps and arrays. Previously, all nested `ReadableNativeMap`s and `ReadableNativeArray`s were recursively converted to `HashMap`s and `ArrayList`s, but this is lost when only `getLocalMap()` is returned. Call `ReadableNativeMap.toHashMap()` on a map with values of type `ReadableNativeMap` and `ReadableNativeArray`. Verify the returned hash map has these converted to `HashMap` and `ArrayList`, respectively. [ANDROID] [BUGFIX] [ReadableNativeMap] - Fix toHashMap() for nested maps and arrays Closes https://github.com/facebook/react-native/pull/18455 Reviewed By: kathryngray Differential Revision: D7347344 Pulled By: mdvacca fbshipit-source-id: af2bca9dec6c0cb8a7da099b6757434fcc3ac785 --- .../react/bridge/ReadableNativeMap.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java index ea1d04d0738fb1..88289ad71cd49b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java @@ -13,6 +13,7 @@ import com.facebook.proguard.annotations.DoNotStrip; import java.util.HashMap; +import java.util.Iterator; import com.facebook.infer.annotation.Assertions; import javax.annotation.Nullable; @@ -250,7 +251,31 @@ public HashMap toHashMap() { } return hashMap; } - return getLocalMap(); + + // we can almost just return getLocalMap(), but we need to convert nested arrays and maps to the + // correct types first + HashMap hashMap = new HashMap<>(getLocalMap()); + Iterator iterator = hashMap.keySet().iterator(); + + while (iterator.hasNext()) { + String key = (String) iterator.next(); + switch (getType(key)) { + case Null: + case Boolean: + case Number: + case String: + break; + case Map: + hashMap.put(key, Assertions.assertNotNull(getMap(key)).toHashMap()); + break; + case Array: + hashMap.put(key, Assertions.assertNotNull(getArray(key)).toArrayList()); + break; + default: + throw new IllegalArgumentException("Could not convert object with key: " + key + "."); + } + } + return hashMap; } /**