Permalink
Browse files

Merge pull request #235 from dcordero/nullManagementInKeyPath

Fix backwards compatibility for keys with value null
  • Loading branch information...
2 parents dcb2f9d + 248f3a8 commit eac31d82fbc5ea29762ebc2d158701f4cfd142b8 @jspahrsummers jspahrsummers committed Feb 11, 2014
Showing with 18 additions and 1 deletion.
  1. +1 −1 Mantle/MTLJSONAdapter.m
  2. +17 −0 MantleTests/MTLJSONAdapterSpec.m
@@ -123,7 +123,7 @@ - (id)initWithJSONDictionary:(NSDictionary *)JSONDictionary modelClass:(Class)mo
}
value = [value valueForKey:itemJSONKeyPathComponent];
- if (value == nil) break;
+ if (value == nil || value == NSNull.null) break;
}
if (value == nil) continue;
@@ -88,6 +88,23 @@
expect(error.code).to.equal(MTLJSONAdapterErrorInvalidJSONDictionary);
});
+it(@"should initialize without returning any error when using a JSON dictionary which Null.null as value",^{
+ NSDictionary *values = @{
+ @"username": @"foo",
+ @"nested": NSNull.null,
+ @"count": @"0"
+ };
+
+ NSError *error = nil;
+ MTLTestModel *model = [MTLJSONAdapter modelOfClass:MTLTestModel.class fromJSONDictionary:values error:&error];
+ expect(model).notTo.beNil();
+ expect(error).to.beNil();
+
+ expect(model.name).to.equal(@"foo");
+ expect(model.count).to.equal(0);
+ expect(model.nestedName).to.beNil();
+});
+
it(@"should return nil and an error with a nil JSON dictionary", ^{
NSError *error = nil;
MTLJSONAdapter *adapter = [[MTLJSONAdapter alloc] initWithJSONDictionary:nil modelClass:MTLTestModel.class error:&error];

0 comments on commit eac31d8

Please sign in to comment.