From b303aff0433c57aa5c7c493ff4c9529c96a9e3b9 Mon Sep 17 00:00:00 2001 From: Rafael Kayumov Date: Mon, 18 Jan 2016 18:30:36 +0300 Subject: [PATCH 1/3] Fix tests to check correct 1-to-many relationships serialization according to JSON API v1.0 specification --- Project/JSONAPITests/JSONAPITests.m | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/Project/JSONAPITests/JSONAPITests.m b/Project/JSONAPITests/JSONAPITests.m index ecdd2be..c2c1305 100644 --- a/Project/JSONAPITests/JSONAPITests.m +++ b/Project/JSONAPITests/JSONAPITests.m @@ -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!"); @@ -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 should be array!."); + XCTAssertEqual([json[@"relationships"][@"comments"][@"data"] count], 2, @"Comments should have 1 element!."); } - (void)testCreate { @@ -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'"); From 58145546d5ac3867936815f247d12f95f8082b19 Mon Sep 17 00:00:00 2001 From: Rafael Kayumov Date: Mon, 18 Jan 2016 18:32:14 +0300 Subject: [PATCH 2/3] Fix 1-to-many relationships serialization according to JSON API v1.0 specification --- Classes/JSONAPIResourceParser.m | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Classes/JSONAPIResourceParser.m b/Classes/JSONAPIResourceParser.m index f3d6d89..c737a02 100644 --- a/Classes/JSONAPIResourceParser.m +++ b/Classes/JSONAPIResourceParser.m @@ -121,8 +121,8 @@ + (NSDictionary*)dictionaryFor:(NSObject *)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]; @@ -366,10 +366,15 @@ + (NSDictionary*)link:(NSObject *)resource from:(NSObject Date: Wed, 20 Jan 2016 23:49:54 +0300 Subject: [PATCH 3/3] Fix assertion description --- Project/JSONAPITests/JSONAPITests.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project/JSONAPITests/JSONAPITests.m b/Project/JSONAPITests/JSONAPITests.m index c2c1305..16cc297 100644 --- a/Project/JSONAPITests/JSONAPITests.m +++ b/Project/JSONAPITests/JSONAPITests.m @@ -176,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"][@"data"] isKindOfClass:[NSArray class]], @"Comments should be array!."); - XCTAssertEqual([json[@"relationships"][@"comments"][@"data"] count], 2, @"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 {