Skip to content

Commit

Permalink
Merge pull request #22 from WideSpectrumComputing/master
Browse files Browse the repository at this point in the history
Fixing SonarCloud detected reliability issues
  • Loading branch information
akornich authored Dec 16, 2020
2 parents f8301f0 + 6e59fbc commit 456924e
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 48 deletions.
11 changes: 10 additions & 1 deletion RollbarCommon/Sources/RollbarCommon/DTOs/RollbarDTO+Protected.m
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,16 @@ - (void)setNumber:(NSNumber *)data forKey:(NSString *)key {

- (RollbarDTO *)safelyGetDataTransferObjectByKey:(NSString *)key {

RollbarDTO *result = [[RollbarDTO alloc] initWithDictionary:[self getDataByKey:key]];
RollbarDTO *result = [RollbarDTO alloc];

NSDictionary<NSString *, id> *data = [self getDataByKey:key];
if (data) {
result = [result initWithDictionary:[self getDataByKey:key]];
}
else {
result = [result init];
}

return result;
}

Expand Down
103 changes: 60 additions & 43 deletions RollbarCommon/Sources/RollbarCommon/DTOs/RollbarDTO.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "RollbarDTO.h"
#import "RollbarSdkLog.h"
#import "NSJSONSerialization+Rollbar.h"

//#import <Foundation/NSObjCRuntime.h>
//#import "objc/runtime.h"
Expand Down Expand Up @@ -74,46 +75,57 @@ + (nullable NSData *)dataWithJSONObject:(id)obj
}

+ (NSMutableDictionary *)safeDataFromJSONObject:(id)obj {
NSMutableDictionary *safeData = [NSMutableDictionary new];
[obj enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[NSDictionary class]]) {
[safeData setObject:[[self class] safeDataFromJSONObject:obj] forKey:key];
} else if ([obj isKindOfClass:[NSArray class]]) {
[safeData setObject:((NSArray *)obj).mutableCopy forKey:key];
} else if ([obj isKindOfClass:[NSNumber class]]) {
[safeData setObject:obj forKey:key];
} else if ([obj isKindOfClass:[NSString class]]) {
[safeData setObject:obj forKey:key];
} else if ([obj isKindOfClass:[NSDate class]]) {
[safeData setObject:[obj description] forKey:key];
} else if ([obj isKindOfClass:[NSURL class]]) {
[safeData setObject:[obj absoluteString] forKey:key];
} else if ([obj isKindOfClass:[NSError class]]) {
[safeData setObject:[[self class] safeDataFromJSONObject:[obj userInfo]] forKey:key];
} else if ([obj isKindOfClass:[NSHTTPURLResponse class]]) {
[safeData setObject:[obj allHeaderFields] forKey:key];
} else if ([obj isKindOfClass:[NSSet class]]) {
[safeData setObject:[[obj allObjects] componentsJoinedByString:@","] forKey:key];
} else if ([obj isKindOfClass:[NSData class]]) {
NSError* error = nil;
NSMutableDictionary* json =
[NSJSONSerialization JSONObjectWithData:obj
options:(NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves)
error:&error];

if (error == nil) {
[safeData setObject:[[self class] safeDataFromJSONObject:json] forKey:key];
} else {
RollbarSdkLog(@"Error serializing NSData: %@", [error localizedDescription]);
}
} else if ([NSJSONSerialization isValidJSONObject:@{key:obj}]) {
[safeData setObject:obj forKey:key];
} else {
RollbarSdkLog(@"Error serializing class '%@' using NSJSONSerialization",
NSStringFromClass([obj class]));
}
}];
return safeData;

return [NSJSONSerialization rollbar_safeDataFromJSONObject:obj];

// NSMutableDictionary *safeData = [NSMutableDictionary new];
// [obj enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
//
// if ([obj isKindOfClass:[NSDictionary class]]) {
// [safeData setObject:[[self class] safeDataFromJSONObject:obj] forKey:key];
// } else if ([obj isKindOfClass:[NSArray class]]) {
// [safeData setObject:((NSArray *)obj).mutableCopy forKey:key];
// } else if ([obj isKindOfClass:[NSNumber class]]) {
// [safeData setObject:obj forKey:key];
// } else if ([obj isKindOfClass:[NSString class]]) {
// [safeData setObject:obj forKey:key];
// } else if ([obj isKindOfClass:[NSDate class]]) {
// [safeData setObject:[obj description] forKey:key];
// } else if ([obj isKindOfClass:[NSURL class]]) {
// NSString *url = [obj absoluteString];
// if (url) {
// [safeData setObject:[obj absoluteString] forKey:key];
// }
// else if([obj description]) {
// [safeData setObject:[obj description] forKey:key];
// }
// } else if ([obj isKindOfClass:[NSError class]]) {
// [safeData setObject:[[self class] safeDataFromJSONObject:[obj userInfo]] forKey:key];
// } else if ([obj isKindOfClass:[NSHTTPURLResponse class]]) {
// [safeData setObject:[obj allHeaderFields] forKey:key];
// } else if ([obj isKindOfClass:[NSSet class]]) {
// [safeData setObject:[[obj allObjects] componentsJoinedByString:@","] forKey:key];
// } else if ([obj isKindOfClass:[NSData class]]) {
// NSError* error = nil;
// NSMutableDictionary* json =
// [NSJSONSerialization JSONObjectWithData:obj
// options:(NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves)
// error:&error];
//
// if (error == nil) {
// [safeData setObject:[[self class] safeDataFromJSONObject:json] forKey:key];
// } else {
// RollbarSdkLog(@"Error serializing NSData: %@", [error localizedDescription]);
// }
// } else if ([NSJSONSerialization isValidJSONObject:@{key:obj}]) {
// [safeData setObject:obj forKey:key];
// } else {
// RollbarSdkLog(@"Error serializing class '%@' using NSJSONSerialization",
// NSStringFromClass([obj class]));
// }
// }];
//
// return safeData;
}

+ (unsigned long)measureJSONDataByteSize:(NSData*)jsonData {
Expand Down Expand Up @@ -207,16 +219,21 @@ - (NSArray *)getDefinedProperties {

NSMutableArray *result = [NSMutableArray array];

unsigned int outCount, i;
unsigned int outCount;
objc_property_t *properties = class_copyPropertyList([self class], &outCount);
if (!properties) {
return result;
}

for(i = 0; i < outCount; ++i) {
for(unsigned int i = 0; i < outCount; ++i) {
objc_property_t property = properties[i];
const char *propName = property_getName(property);
if(propName) {
NSString *propertyName = [NSString stringWithCString:propName
encoding:[NSString defaultCStringEncoding]];
[result addObject:propertyName];
if (propertyName) {
[result addObject:propertyName];
}
}
}

Expand Down
17 changes: 15 additions & 2 deletions RollbarCommon/Sources/RollbarCommon/NSJSONSerialization+Rollbar.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,28 @@ + (NSMutableDictionary *)rollbar_safeDataFromJSONObject:(id)obj {

NSMutableDictionary *safeData = [NSMutableDictionary new];
[obj enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {

if ([obj isKindOfClass:[NSDictionary class]]) {
[safeData setObject:[[self class] rollbar_safeDataFromJSONObject:obj] forKey:key];
} else if ([obj isKindOfClass:[NSArray class]]) {
[safeData setObject:((NSArray *)obj).mutableCopy forKey:key];
} else if ([NSJSONSerialization isValidJSONObject:@{key:obj}]) {
[safeData setObject:obj forKey:key];
} else if ([obj isKindOfClass:[NSNumber class]]) {
[safeData setObject:obj forKey:key];
} else if ([obj isKindOfClass:[NSString class]]) {
[safeData setObject:obj forKey:key];
} else if ([obj isKindOfClass:[NSDate class]]) {
[safeData setObject:[obj description] forKey:key];
} else if ([obj isKindOfClass:[NSURL class]]) {
[safeData setObject:[obj absoluteString] forKey:key];
} else if ([obj isKindOfClass:[NSError class]]) {
NSString *url = [obj absoluteString];
if (url) {
[safeData setObject:[obj absoluteString] forKey:key];
}
else if([obj description]) {
[safeData setObject:[obj description] forKey:key];
}
} else if ([obj isKindOfClass:[NSError class]] && [obj userInfo]) {
[safeData setObject:[[self class] rollbar_safeDataFromJSONObject:[obj userInfo]] forKey:key];
} else if ([obj isKindOfClass:[NSHTTPURLResponse class]]) {
[safeData setObject:[obj allHeaderFields] forKey:key];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN

/// Turns JSON-like object structure into a valid NSJSONSerialization structure
/// @param obj JSON-like object
+ (NSDictionary *)rollbar_safeDataFromJSONObject:(id)obj;
+ (NSMutableDictionary *)rollbar_safeDataFromJSONObject:(id)obj;

/// Byte-length of a NSData representation of a JSON structure
/// @param jsonData NSData representation of a JSON structure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ NS_ASSUME_NONNULL_BEGIN
- (NSMutableDictionary *)safelyGetDictionaryByKey:(NSString *)key;
- (NSMutableArray *)safelyGetArrayByKey:(NSString *)key;
- (NSMutableString *)safelyGetStringByKey:(NSString *)key;
- (NSNumber *)safelyGetNumberByKey:(NSString *)key;
- (nullable NSNumber *)safelyGetNumberByKey:(NSString *)key;

#pragma mark - Core API: data setters by key

Expand Down

0 comments on commit 456924e

Please sign in to comment.