Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keypath regression #266

Merged
merged 3 commits into from
Mar 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions Mantle/MTLJSONAdapter.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -106,24 +106,21 @@ - (id)initWithJSONDictionary:(NSDictionary *)JSONDictionary modelClass:(Class)mo
NSString *JSONKeyPath = [self JSONKeyPathForPropertyKey:propertyKey]; NSString *JSONKeyPath = [self JSONKeyPathForPropertyKey:propertyKey];
if (JSONKeyPath == nil) continue; if (JSONKeyPath == nil) continue;


id value = JSONDictionary; id value;
NSArray *JSONKeyPathComponents = [JSONKeyPath componentsSeparatedByString:@"."]; @try {
for (NSString *itemJSONKeyPathComponent in JSONKeyPathComponents) { value = [JSONDictionary valueForKeyPath:JSONKeyPath];
if (![value isKindOfClass:NSDictionary.class]) { } @catch (NSException *ex) {
if (error != NULL) { if (error != NULL) {
NSDictionary *userInfo = @{ NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid JSON dictionary", @""), NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid JSON dictionary", nil),
NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"%@ could not be parsed because an invalid JSON dictionary was provided for key path \"%@\"", @""), modelClass, JSONKeyPath], NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"%1$@ could not be parsed because an invalid JSON dictionary was provided for key path \"%2$@\"", nil), modelClass, JSONKeyPath],
}; MTLJSONAdapterThrownExceptionErrorKey: ex

};
*error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONDictionary userInfo:userInfo];
} *error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONDictionary userInfo:userInfo];

return nil;
} }


value = [value valueForKey:itemJSONKeyPathComponent]; return nil;
if (value == nil || value == NSNull.null) break;
} }


if (value == nil) continue; if (value == nil) continue;
Expand Down
23 changes: 23 additions & 0 deletions MantleTests/MTLJSONAdapterSpec.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -88,6 +88,29 @@
expect(error.code).to.equal(MTLJSONAdapterErrorInvalidJSONDictionary); expect(error.code).to.equal(MTLJSONAdapterErrorInvalidJSONDictionary);
}); });


it(@"should support key paths across arrays", ^{
NSDictionary *values = @{
@"users": @[
@{
@"name": @"foo"
},
@{
@"name": @"bar"
},
@{
@"name": @"baz"
}
]
};

NSError *error = nil;
MTLArrayTestModel *model = [MTLJSONAdapter modelOfClass:MTLArrayTestModel.class fromJSONDictionary:values error:&error];
expect(model).notTo.beNil();
expect(error).to.beNil();

expect(model.names).to.equal((@[ @"foo", @"bar", @"baz" ]));
});

it(@"should initialize without returning any error when using a JSON dictionary which Null.null as value",^{ it(@"should initialize without returning any error when using a JSON dictionary which Null.null as value",^{
NSDictionary *values = @{ NSDictionary *values = @{
@"username": @"foo", @"username": @"foo",
Expand Down
7 changes: 7 additions & 0 deletions MantleTests/MTLTestModel.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ extern const NSInteger MTLTestModelNameMissing;


@end @end


@interface MTLArrayTestModel : MTLModel <MTLJSONSerializing>

// This property is associated with a "users.username" key in JSON.
@property (nonatomic, copy) NSString *names;

@end

// Parses MTLTestModel objects from JSON instead. // Parses MTLTestModel objects from JSON instead.
@interface MTLSubstitutingTestModel : MTLModel <MTLJSONSerializing> @interface MTLSubstitutingTestModel : MTLModel <MTLJSONSerializing>
@end @end
Expand Down
10 changes: 10 additions & 0 deletions MantleTests/MTLTestModel.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ - (void)mergeCountFromModel:(MTLTestModel *)model {


@end @end


@implementation MTLArrayTestModel

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"names": @"users.name"
};
}

@end

@implementation MTLSubstitutingTestModel @implementation MTLSubstitutingTestModel


+ (NSDictionary *)JSONKeyPathsByPropertyKey { + (NSDictionary *)JSONKeyPathsByPropertyKey {
Expand Down