Skip to content

Commit

Permalink
Fixes #200 - Make userId optional
Browse files Browse the repository at this point in the history
  • Loading branch information
HazAT committed Aug 23, 2017
1 parent b2e0708 commit 376e30f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 22 deletions.
3 changes: 2 additions & 1 deletion Sources/Sentry/SentryKSCrashReportConverter.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ - (SentryEvent *)convertReportToEvent {
- (SentryUser *_Nullable)convertUser {
SentryUser *user = nil;
if (nil != self.userContext[@"user"]) {
user = [[SentryUser alloc] initWithUserId:self.userContext[@"user"][@"id"]];
user = [[SentryUser alloc] init];

This comment has been minimized.

Copy link
@marcomonteiro

marcomonteiro Aug 23, 2017

You might want to keep the initializer around so it doesn't break any client relying on it. Thanks for working on this so quickly. :)

This comment has been minimized.

Copy link
@HazAT

HazAT Aug 23, 2017

Author Member

You are probably right, thx.

user.userId = self.userContext[@"user"][@"id"];
user.email = self.userContext[@"user"][@"email"];
user.username = self.userContext[@"user"][@"username"];
user.extra = self.userContext[@"user"][@"extra"];
Expand Down
13 changes: 4 additions & 9 deletions Sources/Sentry/SentryUser.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,14 @@

@implementation SentryUser

- (instancetype)initWithUserId:(NSString *)userId {
self = [super init];
if (self) {
self.userId = userId;
}
return self;
- (instancetype)init {
return [super init];
}

- (NSDictionary<NSString *, id> *)serialize {
NSMutableDictionary *serializedData = @{
@"id": self.userId
}.mutableCopy;
NSMutableDictionary *serializedData = [[NSMutableDictionary alloc] init];

[serializedData setValue:self.userId forKey:@"id"];
[serializedData setValue:self.email forKey:@"email"];
[serializedData setValue:self.username forKey:@"username"];
[serializedData setValue:[self.extra sentry_sanitize] forKey:@"extra"];
Expand Down
11 changes: 3 additions & 8 deletions Sources/Sentry/include/SentryUser.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ NS_ASSUME_NONNULL_BEGIN

NS_SWIFT_NAME(User)
@interface SentryUser : NSObject <SentrySerializable>
SENTRY_NO_INIT

/**
* Id of the user
* Optional: Id of the user
*/
@property(nonatomic, copy) NSString *userId;

Expand All @@ -42,12 +41,8 @@ SENTRY_NO_INIT
*/
@property(nonatomic, strong) NSDictionary<NSString *, id> *_Nullable extra;

/**
* Initializes a SentryUser with the id
* @param userId NSString
* @return SentryUser
*/
- (instancetype)initWithUserId:(NSString *)userId;
- (instancetype)init;
+ (instancetype)new NS_UNAVAILABLE;

@end

Expand Down
6 changes: 4 additions & 2 deletions Tests/SentryTests/SentryInterfacesTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,14 @@ - (void)testThread {
}

- (void)testUser {
SentryUser *user = [[SentryUser alloc] initWithUserId:@"1"];
SentryUser *user = [[SentryUser alloc] init];
user.userId = @"1";
XCTAssertNotNil(user.userId);
NSDictionary *serialized = @{@"id": @"1"};
XCTAssertEqualObjects([user serialize], serialized);

SentryUser *user2 = [[SentryUser alloc] initWithUserId:@"1"];
SentryUser *user2 = [[SentryUser alloc] init];
user2.userId = @"1";
XCTAssertNotNil(user2.userId);
user2.email = @"a@b.com";
user2.username = @"tony";
Expand Down
4 changes: 3 additions & 1 deletion Tests/SentryTests/SentryRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,9 @@ - (void)testRequestQueueMultipleEvents {
- (void)testUseClientProperties {
self.client.tags = @{@"a": @"b"};
self.client.extra = @{@"c": @"d"};
self.client.user = [[SentryUser alloc] initWithUserId:@"XXXXXX"];
SentryUser *user = [[SentryUser alloc] init];
user.userId = @"XXXXXX";
self.client.user = user;
NSDate *date = [NSDate date];
SentryBreadcrumb *crumb = [[SentryBreadcrumb alloc] initWithLevel:kSentrySeverityInfo category:@"you"];
crumb.timestamp = date;
Expand Down
3 changes: 2 additions & 1 deletion Tests/SentryTests/SentrySwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class SentrySwiftTests: XCTestCase {
Client.shared?.breadcrumbs.add(Breadcrumb(level: .info, category: "test"))
XCTAssertEqual(Client.shared?.breadcrumbs.count(), 1)
Client.shared?.enableAutomaticBreadcrumbTracking()
let user = User(userId: "1234")
let user = User()
user.userId = "1234"
user.email = "hello@sentry.io"
user.extra = ["is_admin": true]
Client.shared?.user = user
Expand Down

0 comments on commit 376e30f

Please sign in to comment.