Skip to content

Commit ae06611

Browse files
author
Josh Holtz
committed
Merge pull request #34 from RafaelKayumov/master
Fix 1-to-many relationships serialization according to JSON API v1.0
2 parents 3b220f7 + 3d109c1 commit ae06611

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

Classes/JSONAPIResourceParser.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ + (NSDictionary*)dictionaryFor:(NSObject <JSONAPIResource>*)resource {
121121
[dictionaryArray addObject:[self link:valueElement from:resource withKey:[property jsonName]]];
122122
}
123123

124-
[linkage setValue:dictionaryArray forKey:[property jsonName]];
125-
124+
NSDictionary *dataDictionary = @{@"data" : dictionaryArray};
125+
[linkage setValue:dataDictionary forKey:[property jsonName]];
126126
} else {
127127
NSFormatter *format = [property formatter];
128128

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

368368
if (resource.ID) {
369-
[reference setValue:@{
370-
@"type" : descriptor.type,
371-
@"id" : resource.ID
372-
} forKey:@"data"];
369+
NSDictionary *referenceObject = @{
370+
@"type" : descriptor.type,
371+
@"id" : resource.ID
372+
};
373+
if ([[owner valueForKey:key] isKindOfClass:[NSArray class]]) {
374+
reference = referenceObject.mutableCopy;
375+
} else {
376+
[reference setValue:referenceObject forKey:@"data"];
377+
}
373378
}
374379

375380
return reference;

Project/JSONAPITests/JSONAPITests.m

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,21 @@ - (void)testSerializeComplex {
151151
newAuthor.firstName = @"Karl";
152152
newAuthor.lastName = @"Armstrong";
153153

154-
CommentResource *newComment = [[CommentResource alloc] init];
155-
newComment.ID = [NSUUID UUID];
156-
newComment.author = newAuthor;
157-
newComment.text = @"First!";
154+
CommentResource *firstComment = [[CommentResource alloc] init];
155+
firstComment.ID = [NSUUID UUID];
156+
firstComment.author = newAuthor;
157+
firstComment.text = @"First!";
158+
159+
CommentResource *secondComment = [[CommentResource alloc] init];
160+
secondComment.ID = [NSUUID UUID];
161+
secondComment.author = newAuthor;
162+
secondComment.text = @"Second!";
158163

159164
ArticleResource *newArticle = [[ArticleResource alloc] init];
160165
newArticle.title = @"Title";
161166
newArticle.author = newAuthor;
162167
newArticle.date = [NSDate date];
163-
newArticle.comments = [[NSArray alloc] initWithObjects:newComment, nil];
168+
newArticle.comments = [[NSArray alloc] initWithObjects:firstComment, secondComment, nil];
164169

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

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

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

229-
XCTAssertNotNil(serializedFirstPost[@"relationships"][@"attachments"][0], @"Media attachment should not be nil");
230-
XCTAssertNotNil(serializedFirstPost[@"relationships"][@"attachments"][1], @"Web page url attachment should not be nil");
234+
XCTAssertNotNil(serializedFirstPost[@"relationships"][@"attachments"][@"data"][0], @"Media attachment should not be nil");
235+
XCTAssertNotNil(serializedFirstPost[@"relationships"][@"attachments"][@"data"][1], @"Web page url attachment should not be nil");
231236

232-
XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"attachments"][0][@"data"][@"id"], @15, @"Media id should be '15'");
233-
XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"attachments"][0][@"data"][@"type"], @"Media", @"Media type should be 'Media'");
237+
XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"attachments"][@"data"][0][@"id"], @15, @"Media id should be '15'");
238+
XCTAssertEqualObjects(serializedFirstPost[@"relationships"][@"attachments"][@"data"][0][@"type"], @"Media", @"Media type should be 'Media'");
234239

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

0 commit comments

Comments
 (0)