Skip to content

Commit 5814554

Browse files
committed
Fix 1-to-many relationships serialization according to JSON API v1.0 specification
1 parent b303aff commit 5814554

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
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;

0 commit comments

Comments
 (0)