Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #197 from dcordero/fixCrashWhenBuildingTheModel

Fix a crash when receving something different than a NSDictionary
  • Loading branch information...
commit 7a474371f262d7ea252d8646f29d9c75ea47e80a 2 parents 46833e7 + c8f6f64
@robb robb authored
Showing with 12 additions and 3 deletions.
  1. +2 −3 Mantle/MTLJSONAdapter.m
  2. +10 −0 MantleTests/MTLJSONAdapterSpec.m
View
5 Mantle/MTLJSONAdapter.m
@@ -72,12 +72,11 @@ - (id)initWithJSONDictionary:(NSDictionary *)JSONDictionary modelClass:(Class)mo
NSParameterAssert([modelClass isSubclassOfClass:MTLModel.class]);
NSParameterAssert([modelClass conformsToProtocol:@protocol(MTLJSONSerializing)]);
-
- if (JSONDictionary == nil) {
+ if (JSONDictionary == nil || ![JSONDictionary isKindOfClass:NSDictionary.class]) {
if (error != NULL) {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: NSLocalizedString(@"Missing JSON dictionary", @""),
- NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"%@ could not be created because no JSON dictionary was provided.", @""), NSStringFromClass(modelClass)],
+ NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"%@ could not be created because an invalid JSON dictionary was provided: %@", @""), NSStringFromClass(modelClass), JSONDictionary.class],
};
*error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONDictionary userInfo:userInfo];
}
View
10 MantleTests/MTLJSONAdapterSpec.m
@@ -82,6 +82,16 @@
expect(error.code).to.equal(MTLJSONAdapterErrorInvalidJSONDictionary);
});
+it(@"should return nil and an error with a wrong data type as dictionary", ^{
+ NSError *error = nil;
+ id wrongDictionary = @"";
+ MTLJSONAdapter *adapter = [[MTLJSONAdapter alloc] initWithJSONDictionary:wrongDictionary modelClass:MTLTestModel.class error:&error];
+ expect(adapter).to.beNil();
+ expect(error).notTo.beNil();
+ expect(error.domain).to.equal(MTLJSONAdapterErrorDomain);
+ expect(error.code).to.equal(MTLJSONAdapterErrorInvalidJSONDictionary);
+});
+
it(@"should ignore unrecognized JSON keys", ^{
NSDictionary *values = @{
@"foobar": @"foo",
Please sign in to comment.
Something went wrong with that request. Please try again.