Skip to content

Commit 6f88bb6

Browse files
author
Josh Holtz
committed
Merge pull request #33 from ViralFoundry/master
Add support for empty to-one relationship according to JSON API v1.0
2 parents 72e0fb8 + af6ec15 commit 6f88bb6

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

Classes/JSONAPIResourceParser.m

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,20 @@ + (void)set:(NSObject <JSONAPIResource> *)resource withDictionary:dictionary {
200200
if (property.resourceType) {
201201
if (relationships) {
202202
id value = [relationships objectForKey:[property jsonName]];
203-
[resource setValue:[JSONAPIResourceParser jsonAPILink:value] forKey:key];
203+
if (value[@"data"] != [NSNull null]) {
204+
[resource setValue:[JSONAPIResourceParser jsonAPILink:value] forKey:key];
205+
}
204206
}
205207

206208
} else if (relationships[key]) {
207209
if (relationships) {
208210
id value = relationships[key];
209-
[resource setValue:[JSONAPIResourceParser jsonAPILink:value] forKey:key];
211+
if (value[@"data"] != [NSNull null]) {
212+
[resource setValue:[JSONAPIResourceParser jsonAPILink:value] forKey:key];
213+
}
210214
}
211215
} else {
212-
id value = [attributes objectForKey:[property jsonName]];;
216+
id value = [attributes objectForKey:[property jsonName]];
213217
if ((id)[NSNull null] == value) {
214218
value = [dictionary objectForKey:[property jsonName]];
215219
}

Project/JSONAPI.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
685481EB1AE4161900D3A633 /* JSONAPIPropertyDescriptor.m in Sources */ = {isa = PBXBuildFile; fileRef = 685481EA1AE4161900D3A633 /* JSONAPIPropertyDescriptor.m */; };
5252
685481EF1AE419CB00D3A633 /* JSONAPIResourceDescriptor.m in Sources */ = {isa = PBXBuildFile; fileRef = 685481EE1AE419CB00D3A633 /* JSONAPIResourceDescriptor.m */; };
5353
68A469941AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 68A469931AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m */; };
54+
7817CDAE1C39A51100D864E6 /* empty_relationship_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 7817CDAD1C39A51100D864E6 /* empty_relationship_example.json */; };
55+
7817CDAF1C39A85000D864E6 /* empty_relationship_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 7817CDAD1C39A51100D864E6 /* empty_relationship_example.json */; };
5456
781CF6CA1C1ECC260052D755 /* generic_relationships_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 781CF6C91C1ECC260052D755 /* generic_relationships_example.json */; };
5557
781CF6CB1C1ECC710052D755 /* generic_relationships_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 781CF6C91C1ECC260052D755 /* generic_relationships_example.json */; };
5658
78A5C1CA1C1E1336008C8632 /* NewsFeedPostResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 78A5C1C91C1E1336008C8632 /* NewsFeedPostResource.m */; };
@@ -116,6 +118,7 @@
116118
68A469921AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDateFormatter+JSONAPIDateFormatter.h"; sourceTree = "<group>"; };
117119
68A469931AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDateFormatter+JSONAPIDateFormatter.m"; sourceTree = "<group>"; };
118120
68DF9E951B06B4C500FA6429 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
121+
7817CDAD1C39A51100D864E6 /* empty_relationship_example.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = empty_relationship_example.json; sourceTree = "<group>"; };
119122
781CF6C91C1ECC260052D755 /* generic_relationships_example.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = generic_relationships_example.json; sourceTree = "<group>"; };
120123
78A5C1C81C1E1336008C8632 /* NewsFeedPostResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NewsFeedPostResource.h; sourceTree = "<group>"; };
121124
78A5C1C91C1E1336008C8632 /* NewsFeedPostResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NewsFeedPostResource.m; sourceTree = "<group>"; };
@@ -281,6 +284,7 @@
281284
03FBD8D81AB879E800789DF3 /* Assets */ = {
282285
isa = PBXGroup;
283286
children = (
287+
7817CDAD1C39A51100D864E6 /* empty_relationship_example.json */,
284288
781CF6C91C1ECC260052D755 /* generic_relationships_example.json */,
285289
03FBD8D91AB879FD00789DF3 /* main_example.json */,
286290
03FBD8E11AB8E46E00789DF3 /* error_example.json */,
@@ -368,6 +372,7 @@
368372
isa = PBXResourcesBuildPhase;
369373
buildActionMask = 2147483647;
370374
files = (
375+
7817CDAF1C39A85000D864E6 /* empty_relationship_example.json in Resources */,
371376
781CF6CB1C1ECC710052D755 /* generic_relationships_example.json in Resources */,
372377
03A055D31868E038004807F0 /* Images.xcassets in Resources */,
373378
03866459186CCE6600985CEC /* CHANGELOG.md in Resources */,
@@ -386,6 +391,7 @@
386391
03FBD8E31AB8E46E00789DF3 /* error_example.json in Resources */,
387392
03A055E41868E038004807F0 /* InfoPlist.strings in Resources */,
388393
781CF6CA1C1ECC260052D755 /* generic_relationships_example.json in Resources */,
394+
7817CDAE1C39A51100D864E6 /* empty_relationship_example.json in Resources */,
389395
03FBD8DA1AB879FD00789DF3 /* main_example.json in Resources */,
390396
);
391397
runOnlyForDeploymentPostprocessing = 0;

Project/JSONAPITests/JSONAPITests.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,27 @@ - (void)testGenericSerialization {
239239
XCTAssertEqualObjects(serializedSecondPost[@"relationships"][@"publisher"][@"data"][@"type"], @"SocialCommunity", @"SocialCommunity type should be 'SocialCommunity'");
240240
}
241241

242+
#pragma mark - Empty relationship tests
243+
244+
- (void)testEmptyRelationship {
245+
NSDictionary *json = [self emptyRelationshipsExampleJSON];
246+
JSONAPI *jsonAPI = [JSONAPI jsonAPIWithDictionary:json];
247+
248+
NewsFeedPostResource *testPost = jsonAPI.resource;
249+
250+
XCTAssertNil(testPost.publisher, @"Test post's publisher should be nil");
251+
252+
XCTAssertNotNil(testPost.attachments, @"Test post's attachments should not be nil");
253+
XCTAssertEqual(testPost.attachments.count, 1, @"Test post's attachments should contain 1 object");
254+
XCTAssertTrue(((JSONAPIResourceBase *)testPost.attachments.firstObject).class == MediaResource.class, @"First attachment should be of class MediaResource");
255+
}
256+
242257
#pragma mark - Private
243258

259+
- (NSDictionary*)emptyRelationshipsExampleJSON {
260+
return [self jsonFor:@"empty_relationship_example" ofType:@"json"];
261+
}
262+
244263
- (NSDictionary*)genericRelationshipsExampleJSON {
245264
return [self jsonFor:@"generic_relationships_example" ofType:@"json"];
246265
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"data": {
3+
"attributes": {
4+
"createdAt": "2015-10-28T19:35:28.194Z",
5+
"title": "Empty relationship test post",
6+
"text": "Test text"
7+
},
8+
"id": 1,
9+
"type": "NewsFeedPost",
10+
"relationships": {
11+
"publisher": {
12+
"data": null
13+
},
14+
"attachments" : {
15+
"data" : [
16+
{
17+
"id" : 15,
18+
"type" : "Media"
19+
}
20+
]
21+
}
22+
}
23+
24+
}
25+
}

0 commit comments

Comments
 (0)