Skip to content

Fix 1-to-many relationships serialization according to JSON API v1.0 #34

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

Merged
merged 3 commits into from
Jan 22, 2016
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
17 changes: 11 additions & 6 deletions Classes/JSONAPIResourceParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ + (NSDictionary*)dictionaryFor:(NSObject <JSONAPIResource>*)resource {
[dictionaryArray addObject:[self link:valueElement from:resource withKey:[property jsonName]]];
}

[linkage setValue:dictionaryArray forKey:[property jsonName]];

NSDictionary *dataDictionary = @{@"data" : dictionaryArray};
[linkage setValue:dataDictionary forKey:[property jsonName]];
} else {
NSFormatter *format = [property formatter];

Expand Down Expand Up @@ -366,10 +366,15 @@ + (NSDictionary*)link:(NSObject <JSONAPIResource>*)resource from:(NSObject <JSON
}

if (resource.ID) {
[reference setValue:@{
@"type" : descriptor.type,
@"id" : resource.ID
} forKey:@"data"];
NSDictionary *referenceObject = @{
@"type" : descriptor.type,
@"id" : resource.ID
};
if ([[owner valueForKey:key] isKindOfClass:[NSArray class]]) {
reference = referenceObject.mutableCopy;
} else {
[reference setValue:referenceObject forKey:@"data"];
}
}

return reference;
Expand Down
27 changes: 16 additions & 11 deletions Project/JSONAPITests/JSONAPITests.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,21 @@ - (void)testSerializeComplex {
newAuthor.firstName = @"Karl";
newAuthor.lastName = @"Armstrong";

CommentResource *newComment = [[CommentResource alloc] init];
newComment.ID = [NSUUID UUID];
newComment.author = newAuthor;
newComment.text = @"First!";
CommentResource *firstComment = [[CommentResource alloc] init];
firstComment.ID = [NSUUID UUID];
firstComment.author = newAuthor;
firstComment.text = @"First!";

CommentResource *secondComment = [[CommentResource alloc] init];
secondComment.ID = [NSUUID UUID];
secondComment.author = newAuthor;
secondComment.text = @"Second!";

ArticleResource *newArticle = [[ArticleResource alloc] init];
newArticle.title = @"Title";
newArticle.author = newAuthor;
newArticle.date = [NSDate date];
newArticle.comments = [[NSArray alloc] initWithObjects:newComment, nil];
newArticle.comments = [[NSArray alloc] initWithObjects:firstComment, secondComment, nil];

NSDictionary *json = [JSONAPIResourceParser dictionaryFor:newArticle];
XCTAssertEqualObjects(json[@"type"], @"articles", @"Did not create Article!");
Expand All @@ -171,8 +176,8 @@ - (void)testSerializeComplex {
XCTAssertNil(json[@"relationships"][@"author"][@"first-name"], @"Bad link!");

XCTAssertNotNil(json[@"relationships"][@"comments"], @"Did not create links!");
XCTAssertTrue([json[@"relationships"][@"comments"] isKindOfClass:[NSArray class]], @"Comments should be array!.");
XCTAssertEqual([json[@"relationships"][@"comments"] count], 1, @"Comments should have 1 element!.");
XCTAssertTrue([json[@"relationships"][@"comments"][@"data"] isKindOfClass:[NSArray class]], @"Comments data should be array!.");
XCTAssertEqual([json[@"relationships"][@"comments"][@"data"] count], 2, @"Comments should have 2 elements!.");
}

- (void)testCreate {
Expand Down Expand Up @@ -226,11 +231,11 @@ - (void)testGenericSerialization {
NSDictionary *serializedFirstPost = [JSONAPIResourceParser dictionaryFor:posts.firstObject];
NSDictionary *serializedSecondPost = [JSONAPIResourceParser dictionaryFor:posts.lastObject];

XCTAssertNotNil(serializedFirstPost[@"relationships"][@"attachments"][0], @"Media attachment should not be nil");
XCTAssertNotNil(serializedFirstPost[@"relationships"][@"attachments"][1], @"Web page url attachment should not be nil");
XCTAssertNotNil(serializedFirstPost[@"relationships"][@"attachments"][@"data"][0], @"Media attachment should not be nil");
XCTAssertNotNil(serializedFirstPost[@"relationships"][@"attachments"][@"data"][1], @"Web page url attachment should not be nil");

XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"attachments"][0][@"data"][@"id"], @15, @"Media id should be '15'");
XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"attachments"][0][@"data"][@"type"], @"Media", @"Media type should be 'Media'");
XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"attachments"][@"data"][0][@"id"], @15, @"Media id should be '15'");
XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"attachments"][@"data"][0][@"type"], @"Media", @"Media type should be 'Media'");

XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"publisher"][@"data"][@"id"], @45, @"User id should be '45'");
XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"publisher"][@"data"][@"type"], @"User", @"User type should be 'User'");
Expand Down