Skip to content

Commit 594d049

Browse files
bsneedBrandon Sneed
andauthored
Fixed issue where non-serializable types would get into payload (#937)
Co-authored-by: Brandon Sneed <[email protected]>
1 parent a73ef3b commit 594d049

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

Analytics/Internal/SEGUtils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ NSString *_Nullable SEGIDFA(void);
6262

6363
NSString *SEGEventNameForScreenTitle(NSString *title);
6464

65+
@interface NSJSONSerialization (Serializable)
66+
+ (BOOL)isOfSerializableType:(id)obj;
67+
@end
68+
6569
// Deep copy and check NSCoding conformance
6670
@protocol SEGSerializableDeepCopy <NSObject>
6771
-(id _Nullable) serializableMutableDeepCopy;

Analytics/Internal/SEGUtils.m

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,20 @@ static id SEGCoerceJSONObject(id obj)
559559
}
560560

561561

562+
@implementation NSJSONSerialization(Serializable)
563+
+ (BOOL)isOfSerializableType:(id)obj
564+
{
565+
if ([obj isKindOfClass:[NSArray class]] ||
566+
[obj isKindOfClass:[NSDictionary class]] ||
567+
[obj isKindOfClass:[NSString class]] ||
568+
[obj isKindOfClass:[NSNumber class]] ||
569+
[obj isKindOfClass:[NSNull class]])
570+
return YES;
571+
return NO;
572+
}
573+
@end
574+
575+
562576
@implementation NSDictionary(SerializableDeepCopy)
563577

564578
- (id)serializableDeepCopy:(BOOL)mutable
@@ -569,11 +583,12 @@ - (id)serializableDeepCopy:(BOOL)mutable
569583
id aValue = [self objectForKey:key];
570584
id theCopy = nil;
571585

572-
if (![aValue conformsToProtocol:@protocol(NSCoding)]) {
586+
if (![NSJSONSerialization isOfSerializableType:aValue]) {
587+
NSString *className = NSStringFromClass([aValue class]);
573588
#ifdef DEBUG
574-
NSAssert(FALSE, @"key `%@` doesn't conform to NSCoding and can't be serialized for delivery.", key);
589+
NSAssert(FALSE, @"key `%@` is a %@ and can't be serialized for delivery.", key, className);
575590
#else
576-
SEGLog(@"key `%@` doesn't conform to NSCoding and can't be serialized for delivery.", key);
591+
SEGLog(@"key `%@` is a %@ and can't be serializaed for delivery.", key, className);
577592
// simply leave it out since we can't encode it anyway.
578593
continue;
579594
#endif
@@ -617,16 +632,17 @@ -(id)serializableDeepCopy:(BOOL)mutable
617632
for (id aValue in self) {
618633
id theCopy = nil;
619634

620-
if (![aValue conformsToProtocol:@protocol(NSCoding)]) {
635+
if (![NSJSONSerialization isOfSerializableType:aValue]) {
636+
NSString *className = NSStringFromClass([aValue class]);
621637
#ifdef DEBUG
622-
NSAssert(FALSE, @"type `%@` doesn't conform to NSCoding and can't be serialized for delivery.", NSStringFromClass([aValue class]));
638+
NSAssert(FALSE, @"found a %@ which can't be serialized for delivery.", className);
623639
#else
624-
SEGLog(@"type `%@` doesn't conform to NSCoding and can't be serialized for delivery.", NSStringFromClass([aValue class]));
640+
SEGLog(@"found a %@ which can't be serializaed for delivery.", className);
625641
// simply leave it out since we can't encode it anyway.
626642
continue;
627643
#endif
628644
}
629-
645+
630646
if ([aValue conformsToProtocol:@protocol(SEGSerializableDeepCopy)]) {
631647
theCopy = [aValue serializableDeepCopy:mutable];
632648
} else if ([aValue conformsToProtocol:@protocol(NSCopying)]) {

AnalyticsTests/SerializationTests.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#import <XCTest/XCTest.h>
1010
@import Analytics;
1111

12+
@interface NSJSONSerialization (Serializable)
13+
+ (BOOL)isOfSerializableType:(id)obj;
14+
@end
15+
1216
@protocol SEGSerializableDeepCopy <NSObject>
1317
-(id _Nullable) serializableDeepCopy;
1418
@end
@@ -52,4 +56,20 @@ - (void)testDeepCopyAndConformance {
5256
XCTAssertThrows([nonserializable serializableDeepCopy]);
5357
}
5458

59+
- (void)testDateIssue {
60+
NSDate *date = [NSDate date];
61+
NSString *test = @"test";
62+
63+
XCTAssertFalse([NSJSONSerialization isOfSerializableType:date]);
64+
XCTAssertTrue([NSJSONSerialization isOfSerializableType:test]);
65+
66+
NSDictionary *nonserializable = @{@"test": date};
67+
NSDictionary *serializable = @{@"test": @1};
68+
XCTAssertThrows([nonserializable serializableDeepCopy]);
69+
XCTAssertNoThrow([serializable serializableDeepCopy]);
70+
71+
nonserializable = @{@"test": @[date]};
72+
XCTAssertThrows([nonserializable serializableDeepCopy]);
73+
}
74+
5575
@end

Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ SPEC CHECKSUMS:
1313

1414
PODFILE CHECKSUM: b1320b7107926418d81b6d48a9c864abdf9c3eaf
1515

16-
COCOAPODS: 1.9.3
16+
COCOAPODS: 1.9.1

0 commit comments

Comments
 (0)