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

fix: remove infinite values from message data #138

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
52 changes: 52 additions & 0 deletions UnitTests/MPDataModelTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,58 @@ - (void)testMessageInstance {
XCTAssertNotNil(dictionaryRepresentation, @"Should not have been nil.");
}

- (void)testFixInvalidKeysInDictionary {
double four = 4.0;
double zed = 0.0;

NSDictionary *messageInfo = @{@"MessageKey1": @"MessageValue1",
@"MessageKey2": @"MessageValue2"};
NSMutableDictionary *messageDictionary = messageInfo.mutableCopy;
[MPMessage fixInvalidKeysInDictionary:messageDictionary messageInfo:messageInfo];
XCTAssertEqualObjects(messageInfo, messageDictionary);

messageInfo = @{@"MessageKey1": @"MessageValue1",
@"MessageKey2": @(four/zed)};
messageDictionary = messageInfo.mutableCopy;
[MPMessage fixInvalidKeysInDictionary:messageDictionary messageInfo:messageInfo];
XCTAssertEqual([messageDictionary objectForKey:@"MessageKey2"], nil);
XCTAssertEqual([messageDictionary objectForKey:@"MessageKey1"], @"MessageValue1");

messageInfo = @{@"MessageKey1": @"MessageValue1",
@"MessageKey2": @{
@"NestedKey1": @(four/zed),
@"NestedKey2": @"test"}};
messageDictionary = messageInfo.mutableCopy;
[MPMessage fixInvalidKeysInDictionary:messageDictionary messageInfo:messageInfo];
XCTAssertEqual([messageDictionary[@"MessageKey2"] objectForKey:@"NestedKey1"], nil);
XCTAssertEqual([messageDictionary[@"MessageKey2"] objectForKey:@"NestedKey2"], @"test");

messageInfo = @{@"MessageKey1": @"MessageValue1",
@"MessageKey2": @{
@"NestedKey1": @{ @"NestedKeyA": @(four/zed),
@"NestedKeyB": @"test"},
@"NestedKey2": @"test"}

};
messageDictionary = messageInfo.mutableCopy;
[MPMessage fixInvalidKeysInDictionary:messageDictionary messageInfo:messageInfo];
XCTAssertEqual([messageDictionary[@"MessageKey2"][@"NestedKey1"] objectForKey:@"NestedKeyA"], nil);
XCTAssertEqual([messageDictionary[@"MessageKey2"][@"NestedKey1"] objectForKey:@"NestedKeyB"], @"test");

messageInfo = @{@"MessageKey1": @(four/zed),
@"MessageKey2": @{
@"NestedKey1": @{ @"NestedKeyA": @(four/zed),
@"NestedKeyB": @"test"},
@"NestedKey2": @(four/zed)}};
messageDictionary = messageInfo.mutableCopy;
[MPMessage fixInvalidKeysInDictionary:messageDictionary messageInfo:messageInfo];
XCTAssertEqual([messageDictionary objectForKey:@"MessageKey1"], nil);
XCTAssertEqual([messageDictionary[@"MessageKey2"] objectForKey:@"NestedKey2"], nil);
XCTAssertEqual([messageDictionary[@"MessageKey2"][@"NestedKey1"] objectForKey:@"NestedKeyA"], nil);
XCTAssertEqual([messageDictionary[@"MessageKey2"][@"NestedKey1"] objectForKey:@"NestedKeyB"], @"test");

}

- (void)testMessageInstanceWithInfinite {
MPSession *session = [[MPSession alloc] initWithStartTime:[[NSDate date] timeIntervalSince1970] userId:[MPPersistenceController mpId]];

Expand Down
3 changes: 3 additions & 0 deletions mParticle-Apple-SDK/Data Model/MPMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@
- (void)truncateMessageDataProperty:(nonnull NSString *)property
toLength:(NSInteger)length;

+ (void)fixInvalidKeysInDictionary:(nonnull NSMutableDictionary*)messageDictionary
messageInfo:(nonnull NSDictionary*) messageInfo;

@end
34 changes: 25 additions & 9 deletions mParticle-Apple-SDK/Data Model/MPMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ - (instancetype)initWithSessionId:(NSNumber *)sessionId messageId:(int64_t)messa
return self;
}

+ (void)fixInvalidKeysInDictionary:(NSMutableDictionary*)messageDictionary messageInfo:(NSDictionary*) messageInfo{
for (NSString *key in messageInfo) {
if ([messageInfo[key] isKindOfClass:[NSDictionary class]]) {
if (![NSJSONSerialization isValidJSONObject: messageInfo[key]]) {
NSMutableDictionary *temp = [messageDictionary[key] mutableCopy];
[MPMessage fixInvalidKeysInDictionary:temp messageInfo:messageInfo[key]];
messageDictionary[key] = temp;
}
} else {
if ([messageInfo[key] isKindOfClass:[NSNumber class]]) {
NSNumber *value = (NSNumber *)messageInfo[key];
if(value.doubleValue == INFINITY || value.doubleValue == -INFINITY || isnan(value.doubleValue)) {
MPILogVerbose(@"Invalid Message Data for key: %@", key);
MPILogVerbose(@"Value should not be infinite. Removing value from message data");
messageDictionary[key] = nil;
}

}
}
}
return;
}

- (instancetype)initWithSession:(MPSession *)session messageType:(NSString *)messageType messageInfo:(NSDictionary *)messageInfo uploadStatus:(MPUploadStatus)uploadStatus UUID:(NSString *)uuid timestamp:(NSTimeInterval)timestamp userId:(NSNumber *)userId dataPlanId:(NSString *)dataPlanId dataPlanVersion:(NSNumber *)dataPlanVersion {
NSNumber *sessionId = nil;

Expand All @@ -40,15 +63,8 @@ - (instancetype)initWithSession:(MPSession *)session messageType:(NSString *)mes
}

NSMutableDictionary *messageDictionary = messageInfo.mutableCopy;
for (NSString *key in messageInfo) {
if ([messageInfo[key] isKindOfClass:[NSNumber class]]) {
NSNumber *value = (NSNumber *)messageInfo[key];
if(value.doubleValue == INFINITY || value.doubleValue == -INFINITY || isnan(value.doubleValue)) {
MPILogVerbose(@"Invalid Message Data for key: %@", key);
MPILogVerbose(@"Value should not be infinite. Removing value from message data");
messageDictionary[key] = nil;
}
}
if (![NSJSONSerialization isValidJSONObject: messageInfo]) {
[MPMessage fixInvalidKeysInDictionary:messageDictionary messageInfo:messageInfo];
}

return [self initWithSessionId:sessionId
Expand Down