Skip to content

Commit 0a1a6ae

Browse files
author
Josh Holtz
committed
Merge pull request #25 from joshdholtz/json-api-v1.0-final
Json api v1.0 final
2 parents d6fb138 + c646ca1 commit 0a1a6ae

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

Classes/JSONAPIResourceParser.m

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -268,24 +268,31 @@ + (void)link:(NSObject <JSONAPIResource>*)resource withIncluded:(JSONAPI*)jsonAP
268268
// Loops through all keys to map to properties
269269
NSDictionary *properties = [descriptor properties];
270270
for (NSString *key in properties) {
271-
JSONAPIPropertyDescriptor *property = [properties objectForKey:key];
272-
if (property.resourceType) {
273-
id value = [resource valueForKey:key];
274-
if (value) {
275-
if ([value isKindOfClass:[NSArray class]]) {
276-
for (NSObject <JSONAPIResource> *element in value) {
277-
id include = included[property.resourceType][element.ID];
278-
if (include) {
279-
[self set:element withDictionary:include];
280-
}
281-
}
282-
} else {
283-
NSObject <JSONAPIResource> *attribute = value;
284-
id include = included[property.resourceType][attribute.ID];
285-
if (include) {
286-
[self set:attribute withDictionary:include];
287-
}
271+
JSONAPIPropertyDescriptor *propertyDescriptor = [properties objectForKey:key];
272+
id value = [resource valueForKey:key];
273+
id includedValue = included[[[propertyDescriptor.resourceType descriptor] type]];
274+
275+
// ordinary attribute
276+
if (propertyDescriptor.resourceType == nil) {
277+
continue;
278+
// has many
279+
} else if ([value isKindOfClass:[NSArray class]]) {
280+
NSMutableArray *matched = [value mutableCopy];
281+
[value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
282+
NSObject <JSONAPIResource> *res = obj;
283+
id v = includedValue[res.ID];
284+
if (v != nil) {
285+
matched[idx] = v;
288286
}
287+
}];
288+
289+
[resource setValue:matched forKey:key];
290+
// has one
291+
} else if (value != nil) {
292+
NSObject <JSONAPIResource> *res = value;
293+
id v = includedValue[res.ID];
294+
if (v != nil) {
295+
[resource setValue:v forKey:key];
289296
}
290297
}
291298
}

Project/JSONAPITests/JSONAPITests.m

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,16 @@ - (void)testIncludedPeopleAndComments {
6969
- (void)testDataArticleAuthorAndComments {
7070
NSDictionary *json = [self mainExampleJSON];
7171
JSONAPI *jsonAPI = [JSONAPI jsonAPIWithDictionary:json];
72-
72+
7373
ArticleResource *article = jsonAPI.resource;
74-
XCTAssertNotNil(article.author, @"Article author should not be nil");
75-
XCTAssertNotNil(article.comments, @"Article comments should not be nil");
74+
CommentResource *firstComment = article.comments.firstObject;
75+
76+
XCTAssertNotNil(article.author, @"Article's author should not be nil");
77+
XCTAssertNotNil(article.comments, @"Article's comments should not be nil");
7678
XCTAssertEqual(article.comments.count, 2, @"Article should contain 2 comments");
79+
XCTAssertEqualObjects(article.author.firstName, @"Dan", @"Article's author firstname should be 'Dan'");
80+
XCTAssertEqualObjects(firstComment.text, @"First!", @"Article's first comment should be 'First!'");
81+
XCTAssertEqualObjects(firstComment.author.firstName, @"Dan", @"Article's first comment author should be 'Dan'");
7782
}
7883

7984
- (void)testIncludedCommentIsLinked {
@@ -82,7 +87,7 @@ - (void)testIncludedCommentIsLinked {
8287

8388
CommentResource *comment = [jsonAPI includedResource:@"5" withType:@"comments"];
8489
XCTAssertNotNil(comment.author, @"Comment's author should not be nil");
85-
XCTAssertEqualObjects(comment.author.ID, @"2", @"Comment's author's ID should be 2");
90+
XCTAssertEqualObjects(comment.author.ID, @"9", @"Comment's author's ID should be 2");
8691
}
8792

8893
- (void)testNoError {

Project/JSONAPITests/main_example.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
},
5656
"relationships": {
5757
"author": {
58-
"data": { "type": "people", "id": "2" }
58+
"data": { "type": "people", "id": "9" }
5959
}
6060
},
6161
"links": {

0 commit comments

Comments
 (0)