Skip to content

Commit

Permalink
(iOS bugfix): Convert references to their path strings when fetching …
Browse files Browse the repository at this point in the history
…data from Firestore to avoid crashes due to circular references.

Fixes #617.
  • Loading branch information
Dave Alden committed Jun 16, 2021
1 parent b500935 commit 8f393e2
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/ios/FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,7 @@ - (void)fetchDocumentInFirestoreCollection:(CDVInvokedUrlCommand*)command {
if (error != nil) {
[self sendPluginErrorWithMessage:error.localizedDescription:command];
} else if(snapshot.data != nil) {
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:snapshot.data] callbackId:command.callbackId];
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self sanitiseFirestoreDataDictionary:snapshot.data]] callbackId:command.callbackId];
}else{
[self sendPluginErrorWithMessage:@"Document not found in collection":command];
}
Expand Down Expand Up @@ -1668,7 +1668,7 @@ - (void)listenToDocumentInFirestoreCollection:(CDVInvokedUrlCommand*)command {
[document setObject:[NSNumber numberWithBool:snapshot.metadata.fromCache] forKey:@"fromCache"];
[document setObject:snapshot.metadata.hasPendingWrites ? @"local" : @"remote" forKey:@"source"];
}
[self sendPluginDictionaryResultAndKeepCallback:document command:command callbackId:command.callbackId];
[self sendPluginDictionaryResultAndKeepCallback:[self sanitiseFirestoreDataDictionary:document] command:command callbackId:command.callbackId];
}else{
[self sendPluginErrorWithError:error command:command];
}
Expand Down Expand Up @@ -1708,7 +1708,7 @@ - (void)fetchFirestoreCollection:(CDVInvokedUrlCommand*)command {
} else {
NSMutableDictionary* documents = [[NSMutableDictionary alloc] init];;
for (FIRDocumentSnapshot *document in snapshot.documents) {
[documents setObject:document.data forKey:document.documentID];
[documents setObject:[self sanitiseFirestoreDataDictionary:document.data] forKey:document.documentID];
}
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:documents] callbackId:command.callbackId];
}
Expand Down Expand Up @@ -1757,7 +1757,7 @@ - (void)listenToFirestoreCollection:(CDVInvokedUrlCommand*)command {
[document setObject:@"metadata" forKey:@"type"];
}
if(dc.document.data != nil){
[document setObject:dc.document.data forKey:@"snapshot"];
[document setObject:[self sanitiseFirestoreDataDictionary:dc.document.data] forKey:@"snapshot"];
}
if(dc.document.metadata != nil){
[document setObject:[NSNumber numberWithBool:dc.document.metadata.fromCache] forKey:@"fromCache"];
Expand Down Expand Up @@ -1918,6 +1918,23 @@ - (bool) _removeFirestoreListener: (NSNumber*) key {
}
}

- (NSMutableDictionary*) sanitiseFirestoreDataDictionary:(NSDictionary*) data {
NSMutableDictionary* sanitisedData = [[NSMutableDictionary alloc] init];
for(id key in data){
id value = [data objectForKey:key];
if([value isKindOfClass:[FIRDocumentReference class]]){
FIRDocumentReference* reference = (FIRDocumentReference*) value;
NSString* path = reference.path;
[sanitisedData setValue:path forKey:key];
}else if([value isKindOfClass:[NSDictionary class]]){
[sanitisedData setValue:[self sanitiseFirestoreDataDictionary:value] forKey:key];
}else{
[sanitisedData setValue:value forKey:key];
}
}
return sanitisedData;
}

/*
* Functions
*/
Expand Down

0 comments on commit 8f393e2

Please sign in to comment.