Skip to content

Commit e21855f

Browse files
committed
Add support for generic one to many relationships
1 parent 39867db commit e21855f

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

Classes/JSONAPIResourceParser.m

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ + (NSDictionary*)dictionaryFor:(NSObject <JSONAPIResource>*)resource {
112112
if (valueArray.count > 0) {
113113
NSMutableArray *dictionaryArray = [[NSMutableArray alloc] initWithCapacity:valueArray.count];
114114

115-
if ([property resourceType]) {
115+
if ([property resourceType] || [((NSArray *)value).firstObject conformsToProtocol:@protocol(JSONAPIResource)]) {
116116
if (linkage == nil) {
117117
linkage = [[NSMutableDictionary alloc] init];
118118
}
@@ -279,10 +279,9 @@ + (void)link:(NSObject <JSONAPIResource>*)resource withIncluded:(JSONAPI*)jsonAP
279279
Class valueClass = nil;
280280
if (propertyDescriptor.resourceType) {
281281
valueClass = propertyDescriptor.resourceType;
282-
} else if ([value conformsToProtocol:@protocol(JSONAPIResource)]) {
282+
} else if ([value conformsToProtocol:@protocol(JSONAPIResource)] || [value isKindOfClass:[NSArray class]]) {
283283
valueClass = [value class];
284284
}
285-
id includedValue = included[[[valueClass descriptor] type]];
286285

287286
// ordinary attribute
288287
if (valueClass == nil) {
@@ -291,20 +290,30 @@ + (void)link:(NSObject <JSONAPIResource>*)resource withIncluded:(JSONAPI*)jsonAP
291290
} else if ([value isKindOfClass:[NSArray class]]) {
292291
NSMutableArray *matched = [value mutableCopy];
293292
[value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
294-
NSObject <JSONAPIResource> *res = obj;
295-
id v = includedValue[res.ID];
296-
if (v != nil) {
297-
matched[idx] = v;
293+
if ([obj conformsToProtocol:@protocol(JSONAPIResource)]) {
294+
NSObject <JSONAPIResource> *res = obj;
295+
id includedValue = included[[[res.class descriptor] type]];
296+
if (includedValue) {
297+
id v = includedValue[res.ID];
298+
if (v != nil) {
299+
matched[idx] = v;
300+
}
301+
}
298302
}
299303
}];
300304

301305
[resource setValue:matched forKey:key];
302306
// has one
303307
} else if (value != nil) {
304-
NSObject <JSONAPIResource> *res = value;
305-
id v = includedValue[res.ID];
306-
if (v != nil) {
307-
[resource setValue:v forKey:key];
308+
if ([value conformsToProtocol:@protocol(JSONAPIResource)]) {
309+
id <JSONAPIResource> res = value;
310+
id includedValue = included[[[res.class descriptor] type]];
311+
if (includedValue) {
312+
id v = included[[[res.class descriptor] type]][res.ID];
313+
if (v != nil) {
314+
[resource setValue:v forKey:key];
315+
}
316+
}
308317
}
309318
}
310319
}

0 commit comments

Comments
 (0)