Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Plat-11007] null user components #1599

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Bugsnag/BugsnagInternals.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ typedef void (^ BSGClientObserver)(BSGClientObserverEvent event, _Nullable id va

- (NSDictionary *)toJson;

- (NSDictionary *)toJsonWithNSNulls;

@end

#pragma mark -
Expand Down
2 changes: 1 addition & 1 deletion Bugsnag/Client/BugsnagClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ - (BugsnagUser *)user {
- (void)setUser:(NSString *)userId withEmail:(NSString *)email andName:(NSString *)name {
@synchronized (self.configuration) {
[self.configuration setUser:userId withEmail:email andName:name];
[self.state addMetadata:[self.configuration.user toJson] toSection:BSGKeyUser];
[self.state addMetadata:[self.configuration.user toJsonWithNSNulls] toSection:BSGKeyUser];
if (self.observer) {
self.observer(BSGClientObserverUpdateUser, self.user);
}
Expand Down
19 changes: 19 additions & 0 deletions Bugsnag/Payload/BugsnagUser.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@ @implementation BugsnagUser

- (instancetype)initWithDictionary:(NSDictionary *)dict {
if ((self = [super init])) {
id nsnull = [NSNull null];
_id = dict[@"id"];
if (_id == nsnull) {
_id = nil;
}
_email = dict[@"email"];
if (_email == nsnull) {
_email = nil;
}
_name = dict[@"name"];
if (_name == nsnull) {
_name = nil;
}
}
return self;
}
Expand All @@ -32,6 +42,14 @@ - (instancetype)initWithId:(NSString *)id name:(NSString *)name emailAddress:(NS
return self;
}

- (NSDictionary *)toJsonWithNSNulls {
NSMutableDictionary *dict = [NSMutableDictionary new];
dict[@"id"] = self.id ?: [NSNull null];
dict[@"email"] = self.email ?: [NSNull null];
dict[@"name"] = self.name ?: [NSNull null];
return [NSDictionary dictionaryWithDictionary:dict];
}

- (NSDictionary *)toJson {
NSMutableDictionary *dict = [NSMutableDictionary new];
dict[@"id"] = self.id;
Expand All @@ -40,6 +58,7 @@ - (NSDictionary *)toJson {
return [NSDictionary dictionaryWithDictionary:dict];
}


- (BugsnagUser *)withId {
if (self.id) {
return self;
Expand Down
29 changes: 29 additions & 0 deletions Tests/BugsnagTests/BugsnagUserTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,35 @@ - (void)testDictDeserialisation {
XCTAssertEqualObjects(user.name, @"Tom Bombadil");
}

- (void)testDictNullDeserialisation {

NSDictionary *dict = @{
@"id": [NSNull null],
@"email": [NSNull null],
@"name": [NSNull null]
};
BugsnagUser *user = [[BugsnagUser alloc] initWithDictionary:dict];

XCTAssertNotNil(user);
XCTAssertNil(user.id);
XCTAssertNil(user.email);
XCTAssertNil(user.name);
}

- (void)testDictNullSerialisation {
BugsnagUser *user = [[BugsnagUser alloc] initWithId:nil name:nil emailAddress:nil];
NSDictionary *dict = [user toJson];
XCTAssertEqualObjects(@{}, dict);

dict = [user toJsonWithNSNulls];
NSDictionary *expected = @{
@"id": [NSNull null],
@"email": [NSNull null],
@"name": [NSNull null]
};
XCTAssertEqualObjects(expected, dict);
}

- (void)testPayloadSerialisation {
BugsnagUser *payload = [[BugsnagUser alloc] initWithId:@"test" name:@"Tom Bombadil" emailAddress:@"[email protected]"];
NSDictionary *rootNode = [payload toJson];
Expand Down