mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
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 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. <!-- 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 https://github.com/facebook/react-native/pull/18455 Reviewed By: kathryngray Differential Revision: D7347344 Pulled By: mdvacca fbshipit-source-id: af2bca9dec6c0cb8a7da099b6757434fcc3ac785
This commit is contained in:
committed by
Mike Grabowski
parent
69c277051e
commit
7e4cd53383
@@ -13,6 +13,7 @@ import com.facebook.jni.HybridData;
|
||||
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 class ReadableNativeMap extends NativeMap implements ReadableMap {
|
||||
}
|
||||
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<String, Object> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user