Skip to content

Commit

Permalink
fix: remove infinite values from message data
Browse files Browse the repository at this point in the history
  • Loading branch information
alla-uday committed Nov 1, 2021
1 parent 293c987 commit 2a6ee74
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 9 deletions.
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

0 comments on commit 2a6ee74

Please sign in to comment.