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-7126] Stop dropping breadcrumbs with invalid metadata #1187

Merged
merged 2 commits into from
Sep 16, 2021
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
6 changes: 5 additions & 1 deletion Bugsnag/Client/BugsnagClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,13 @@ - (void)leaveBreadcrumbForNotificationName:(NSString *_Nonnull)notificationName
- (void)leaveBreadcrumbWithMessage:(NSString *_Nonnull)message
metadata:(NSDictionary *_Nullable)metadata
andType:(BSGBreadcrumbType)type {
NSDictionary *JSONMetadata = BSGJSONDictionary(metadata ?: @{});
if (JSONMetadata != metadata && metadata) {
bsg_log_warn("Breadcrumb metadata is not a valid JSON object: %@", metadata);
}
[self addBreadcrumbWithBlock:^(BugsnagBreadcrumb *_Nonnull crumbs) {
crumbs.message = message;
crumbs.metadata = metadata ?: @{};
crumbs.metadata = JSONMetadata;
crumbs.type = type;
}];
}
Expand Down
4 changes: 2 additions & 2 deletions Bugsnag/Helpers/BugsnagCollections.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ NSDictionary * BSGDictionaryWithKeyAndObject(NSString *key, id _Nullable object)
*/
NSDictionary *BSGDictMerge(NSDictionary *source, NSDictionary *destination);

/// Returns a representation of the dictionary that contains only valid JSON.
/// Returns the dictionary if it contains only valid JSON, or a new dictionary
/// where invalid values have been replaced by their descriptions.
/// Any dictionary keys that are not strings will be ignored.
/// Any values that are not valid JSON will be replaced by a string description.
NSDictionary * BSGJSONDictionary(NSDictionary *dictionary);

// MARK: - NSSet
Expand Down
3 changes: 2 additions & 1 deletion Bugsnag/include/Bugsnag/Bugsnag.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@
* a type.
*
* @param message The log message to leave.
* @param metadata Additional metadata included with the breadcrumb.
* @param metadata Diagnostic data relating to the breadcrumb.
* Values should be serializable to JSON with NSJSONSerialization.
* @param type A BSGBreadcrumbTypeValue denoting the type of breadcrumb.
*/
+ (void)leaveBreadcrumbWithMessage:(NSString *_Nonnull)message
Expand Down
3 changes: 2 additions & 1 deletion Bugsnag/include/Bugsnag/BugsnagClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@
* a type.
*
* @param message The log message to leave.
* @param metadata Additional metadata included with the breadcrumb.
* @param metadata Diagnostic data relating to the breadcrumb.
* Values should be serializable to JSON with NSJSONSerialization.
* @param type A BSGBreadcrumbTypeValue denoting the type of breadcrumb.
*/
- (void)leaveBreadcrumbWithMessage:(NSString *_Nonnull)message
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changelog

### Bug fixes

* Stop dropping breadcrumbs when provided invalid metadata (that is not JSON convertible.)
[#1187](https://github.com/bugsnag/bugsnag-cocoa/pull/1187)

* Fix Swift fatal error parsing for messages with no filename.
[#1186](https://github.com/bugsnag/bugsnag-cocoa/pull/1186)

Expand Down
5 changes: 5 additions & 0 deletions Tests/BugsnagBreadcrumbsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ - (void)testCallbackFreeConstructors2 {

XCTAssertEqualObjects(bc7[@"name"], @"user message");
XCTAssertEqualObjects(bc7[@"type"], @"user");

[client leaveBreadcrumbWithMessage:@"Invalid metadata" metadata:@{@"date": [NSDate distantFuture]} andType:BSGBreadcrumbTypeUser];
XCTAssertEqual(client.breadcrumbs.breadcrumbs.count, 9, @"Invalid metadata should not prevent a breadcrumb being left");
XCTAssertEqualObjects(client.breadcrumbs.breadcrumbs[8].message, @"Invalid metadata");
XCTAssertEqualObjects(client.breadcrumbs.breadcrumbs[8].metadata.allKeys, @[@"date"]);
}

/**
Expand Down
6 changes: 0 additions & 6 deletions Tests/BugsnagClientTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,11 @@ - (void)testMetadataInvalidKey {
BugsnagClient *client = [[BugsnagClient alloc] initWithConfiguration:configuration];
[client start];

XCTAssertEqual(client.breadcrumbs.breadcrumbs.count, 0);

id badMetadata = @{
@"test": @"string key is fine",
@85 : @"numeric key would break JSON"
};

[client leaveBreadcrumbWithMessage:@"test msg" metadata:badMetadata andType:BSGBreadcrumbTypeUser];

XCTAssertEqual(client.breadcrumbs.breadcrumbs.count, 0, @"A breadcrumb with invalid JSON payload should be rejected");

[client notifyError:[NSError errorWithDomain:@"test" code:0 userInfo:badMetadata]];
}

Expand Down