Skip to content

Commit 8a6ab14

Browse files
esamelsongrabbou
authored andcommitted
fix ReadableNativeMap.toHashMap() for nested maps and arrays
Summary: <!-- Required: Write your motivation here. If this PR fixes an issue, type "Fixes #issueNumber" to automatically close the issue when the PR is merged. --> Commit 7891805 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. <!-- Required: Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Bonus points for screenshots and videos! --> 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. <!-- Does this PR require a documentation change? Create a PR at https://github.com/facebook/react-native-website and add a link to it here. --> <!-- Required. Help reviewers and the release process by writing your own release notes. See below for an example. --> [ANDROID] [BUGFIX] [ReadableNativeMap] - Fix toHashMap() for nested maps and arrays <!-- **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.** CATEGORY [----------] TYPE [ CLI ] [-------------] LOCATION [ DOCS ] [ BREAKING ] [-------------] [ GENERAL ] [ BUGFIX ] [ {Component} ] [ INTERNAL ] [ ENHANCEMENT ] [ {Filename} ] [ IOS ] [ FEATURE ] [ {Directory} ] |-----------| [ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} | [----------] [-------------] [-------------] |-----------| EXAMPLES: [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see --> Closes #18455 Reviewed By: kathryngray Differential Revision: D7347344 Pulled By: mdvacca fbshipit-source-id: af2bca9dec6c0cb8a7da099b6757434fcc3ac785
1 parent b208272 commit 8a6ab14

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.facebook.proguard.annotations.DoNotStrip;
1212

1313
import java.util.HashMap;
14+
import java.util.Iterator;
1415

1516
import com.facebook.infer.annotation.Assertions;
1617
import javax.annotation.Nullable;
@@ -248,7 +249,31 @@ public HashMap<String, Object> toHashMap() {
248249
}
249250
return hashMap;
250251
}
251-
return getLocalMap();
252+
253+
// we can almost just return getLocalMap(), but we need to convert nested arrays and maps to the
254+
// correct types first
255+
HashMap<String, Object> hashMap = new HashMap<>(getLocalMap());
256+
Iterator iterator = hashMap.keySet().iterator();
257+
258+
while (iterator.hasNext()) {
259+
String key = (String) iterator.next();
260+
switch (getType(key)) {
261+
case Null:
262+
case Boolean:
263+
case Number:
264+
case String:
265+
break;
266+
case Map:
267+
hashMap.put(key, Assertions.assertNotNull(getMap(key)).toHashMap());
268+
break;
269+
case Array:
270+
hashMap.put(key, Assertions.assertNotNull(getArray(key)).toArrayList());
271+
break;
272+
default:
273+
throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
274+
}
275+
}
276+
return hashMap;
252277
}
253278

254279
/**

0 commit comments

Comments
 (0)