Files
react-native/Libraries/TypeSafety/RCTConvertHelpers.mm
T
Alexander Kawrykow 15434c7c43 Guard against null values in object parameters for bridged methods
Summary:
Handles the case when a value in an object parameter of a turbo module spec is null (even if the type is nullable).

For example, given:
```
export interface Spec extends TurboModule {
  +myFunc: ({|
    foo: ?string,
  |}) => void;
}
```
and calling `NativeModule.myFunc({foo: null})`, we see an error like:
```
JSON value '<null>' of type NSNull cannot be converted to NSString
```
Guarding against this by converting NSNull's to nils

## Changelog:

[iOS] [Fixed] - Fix crash when passing null value in object parameter of bridged method

Reviewed By: fkgozali

Differential Revision: D20591590

fbshipit-source-id: fdb90f34131427a235f2e3c99147bf1e6a9c6732
2020-03-23 17:21:00 -07:00

46 lines
891 B
Plaintext

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTConvertHelpers.h"
#import <React/RCTConvert.h>
bool RCTBridgingToBool(id value)
{
return [RCTConvert BOOL:value] ? true : false;
}
folly::Optional<bool> RCTBridgingToOptionalBool(id value)
{
if (!RCTNilIfNull(value)) {
return {};
}
return RCTBridgingToBool(value);
}
NSString *RCTBridgingToString(id value)
{
return [RCTConvert NSString:RCTNilIfNull(value)];
}
folly::Optional<double> RCTBridgingToOptionalDouble(id value)
{
if (!RCTNilIfNull(value)) {
return {};
}
return RCTBridgingToDouble(value);
}
double RCTBridgingToDouble(id value)
{
return [RCTConvert double:value];
}
NSArray *RCTBridgingToArray(id value) {
return [RCTConvert NSArray:value];
}