Skip to content

Commit

Permalink
fix(RCTBlobManager): Prevent RCTConvert error for allowed null blob t…
Browse files Browse the repository at this point in the history
…ypes (facebook#32397)

Summary:
While reworking our media picker, I ended up replacing the RNFetchBlob library to use `fetch()`, leveraging React Native's file resolver (especially to interpret `photos://` URI paths).

The migration process was pretty straightforward, however I kept getting RCTConvert errors when calling `fetch` with a `PUT` method and a blob body:

```
JSON value '<null>' of type NSNull cannot be converted to NSString

+[RCTConvert NSString:]
    RCTConvert.m:59
-[RCTBlobManager handleNetworkingRequest:]
-[RCTNetworking processDataForHTTPQuery:callback:]
-[RCTNetworking buildRequest:completionBlock:]
-[RCTNetworking sendRequest:callback:]
__invoking___
-[NSInvocation invoke]
-[NSInvocation invokeWithTarget:]
-[RCTModuleMethod invokeWithBridge:module:arguments:]

Test Plan:
I'll put my `fetch` snippet here. This is using a third-party library in order to get a signed URL, but it is otherwise pretty straightforward.

This snippet would trigger an error before the changes introduced by this PR.

```js
// Setup (specific to third-party lib)
const key = `file/path/in/bucket`
const params = {
  Key: key,
  Bucket: 'my.awesome.bucket',
}
const s3url = await s3.getSignedUrl('putObject', params)

// Letting RN handle how to retrieve a platform-specific resource URI
const localFetch = await fetch(localPath)
// Getting the blob to upload as octet-stream
const blob = await localFetch.blob()

// Actually uploading
await fetch(s3url, {
  method: 'PUT',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/octet-stream',
  },
  body: blob,
})
```

Reviewed By: RSNara

Differential Revision: D31625636

Pulled By: sota000

fbshipit-source-id: a7770018bc3ff2f41321b1bd26ec145b86b18784
  • Loading branch information
habovh authored and facebook-github-bot committed Oct 16, 2021
1 parent f319c1a commit e1b698c
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion Libraries/Blob/RCTBlobManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ - (NSDictionary *)handleNetworkingRequest:(NSDictionary *)data
NSDictionary *blob = [RCTConvert NSDictionary:data[@"blob"]];

NSString *contentType = @"application/octet-stream";
NSString *blobType = [RCTConvert NSString:blob[@"type"]];
NSString *blobType = [RCTConvert NSString:RCTNilIfNull(blob[@"type"])];
if (blobType != nil && blobType.length > 0) {
contentType = blob[@"type"];
}
Expand Down

0 comments on commit e1b698c

Please sign in to comment.