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

Fix keypath regression #265

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions Mantle/MTLJSONAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ - (id)initWithJSONDictionary:(NSDictionary *)JSONDictionary modelClass:(Class)mo
id value = JSONDictionary;
NSArray *JSONKeyPathComponents = [JSONKeyPath componentsSeparatedByString:@"."];
for (NSString *itemJSONKeyPathComponent in JSONKeyPathComponents) {
if (![value isKindOfClass:NSDictionary.class]) {
if (!([value isKindOfClass:NSDictionary.class] || [value isKindOfClass:NSArray.class])) {
if (error != NULL) {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid JSON dictionary", @""),
NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"%@ could not be parsed because an invalid JSON dictionary was provided for key path \"%@\"", @""), modelClass, JSONKeyPath],
NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"%@ could not be parsed because an invalid JSON object was provided for key path \"%@\"", @""), modelClass, JSONKeyPath],
};

*error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONDictionary userInfo:userInfo];
Expand Down
23 changes: 23 additions & 0 deletions MantleTests/MTLJSONAdapterSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,29 @@
expect(error.code).to.equal(MTLJSONAdapterErrorInvalidJSONDictionary);
});

it(@"should support key paths across", ^{
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",^{
NSDictionary *values = @{
@"username": @"foo",
Expand Down
7 changes: 7 additions & 0 deletions MantleTests/MTLTestModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ extern const NSInteger MTLTestModelNameMissing;

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

@end

@implementation MTLArrayTestModel

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

@end

@implementation MTLSubstitutingTestModel

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
Expand Down