From a42fb8711417e62c18bb861619e48822b85ee487 Mon Sep 17 00:00:00 2001 From: Fernando Arellano Date: Tue, 6 Jun 2017 13:43:14 -0700 Subject: [PATCH 1/2] Add the resources to the include members to link relationships against same type of resources. For example having a response like: ``` { "data": [ { "id": "29", "type": "ediary_entry_groups", "attributes": { "name": "Record Fasting", "permanent_link": "fasting", "position": 2, "category": null, "created_at": "2017-06-02T20:06:16Z", "updated_at": "2017-06-02T20:06:16Z" }, "relationships": { "ediary_entries": { "data": [] }, "entry_templates": { "data": [] }, "child_entry_groups": { "data": [ { "id": "30", "type": "ediary_entry_groups" }, { "id": "31", "type": "ediary_entry_groups" }, { "id": "32", "type": "ediary_entry_groups" }, { "id": "33", "type": "ediary_entry_groups" } ] }, "parent_entry_group": { "data": null } } }, ... ``` The relationships are not being mapped because they are inside data, not included as we expect. --- Classes/JSONAPI.m | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Classes/JSONAPI.m b/Classes/JSONAPI.m index 6026dc1..cd6d7b7 100644 --- a/Classes/JSONAPI.m +++ b/Classes/JSONAPI.m @@ -117,6 +117,16 @@ - (void)inflateWithDictionary:(NSDictionary*)dictionary { // Parses included resources id included = dictionary[@"included"]; NSMutableDictionary *includedResources = [[NSMutableDictionary alloc] init]; + // Adding the resources to the included attribute + for (NSObject *resource in _resources) { + JSONAPIResourceDescriptor *desc = [resource.class descriptor]; + + NSMutableDictionary *typeDict = includedResources[desc.type] ?: @{}.mutableCopy; + typeDict[resource.ID] = resource; + + includedResources[desc.type] = typeDict; + } + if ([included isKindOfClass:[NSArray class]] == YES) { for (NSDictionary *data in included) { From 36a73d3e040b6da546290ab5ce0bfa4d9d878d9f Mon Sep 17 00:00:00 2001 From: Fernando Arellano Date: Tue, 6 Jun 2017 13:46:44 -0700 Subject: [PATCH 2/2] Validate the that the value is not nil before attempting to insert it --- Classes/JSONAPIResourceParser.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/JSONAPIResourceParser.m b/Classes/JSONAPIResourceParser.m index be4c087..9bd597e 100644 --- a/Classes/JSONAPIResourceParser.m +++ b/Classes/JSONAPIResourceParser.m @@ -347,7 +347,7 @@ + (NSArray*)relatedResourcesFor:(NSObject *)resource { id value = [resource valueForKey:key]; if ([value isKindOfClass:[NSArray class]]) { [related addObjectsFromArray:value]; - } else { + } else if (value) { [related addObject:value]; } }