Skip to content

Add support for empty to-one relationship according to JSON API v1.0 #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Classes/JSONAPIResourceParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,20 @@ + (void)set:(NSObject <JSONAPIResource> *)resource withDictionary:dictionary {
if (property.resourceType) {
if (relationships) {
id value = [relationships objectForKey:[property jsonName]];
[resource setValue:[JSONAPIResourceParser jsonAPILink:value] forKey:key];
if (value[@"data"] != [NSNull null]) {
[resource setValue:[JSONAPIResourceParser jsonAPILink:value] forKey:key];
}
}

} else if (relationships[key]) {
if (relationships) {
id value = relationships[key];
[resource setValue:[JSONAPIResourceParser jsonAPILink:value] forKey:key];
if (value[@"data"] != [NSNull null]) {
[resource setValue:[JSONAPIResourceParser jsonAPILink:value] forKey:key];
}
}
} else {
id value = [attributes objectForKey:[property jsonName]];;
id value = [attributes objectForKey:[property jsonName]];
if ((id)[NSNull null] == value) {
value = [dictionary objectForKey:[property jsonName]];
}
Expand Down
6 changes: 6 additions & 0 deletions Project/JSONAPI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
685481EB1AE4161900D3A633 /* JSONAPIPropertyDescriptor.m in Sources */ = {isa = PBXBuildFile; fileRef = 685481EA1AE4161900D3A633 /* JSONAPIPropertyDescriptor.m */; };
685481EF1AE419CB00D3A633 /* JSONAPIResourceDescriptor.m in Sources */ = {isa = PBXBuildFile; fileRef = 685481EE1AE419CB00D3A633 /* JSONAPIResourceDescriptor.m */; };
68A469941AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 68A469931AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m */; };
7817CDAE1C39A51100D864E6 /* empty_relationship_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 7817CDAD1C39A51100D864E6 /* empty_relationship_example.json */; };
7817CDAF1C39A85000D864E6 /* empty_relationship_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 7817CDAD1C39A51100D864E6 /* empty_relationship_example.json */; };
781CF6CA1C1ECC260052D755 /* generic_relationships_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 781CF6C91C1ECC260052D755 /* generic_relationships_example.json */; };
781CF6CB1C1ECC710052D755 /* generic_relationships_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 781CF6C91C1ECC260052D755 /* generic_relationships_example.json */; };
78A5C1CA1C1E1336008C8632 /* NewsFeedPostResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 78A5C1C91C1E1336008C8632 /* NewsFeedPostResource.m */; };
Expand Down Expand Up @@ -116,6 +118,7 @@
68A469921AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDateFormatter+JSONAPIDateFormatter.h"; sourceTree = "<group>"; };
68A469931AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDateFormatter+JSONAPIDateFormatter.m"; sourceTree = "<group>"; };
68DF9E951B06B4C500FA6429 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
7817CDAD1C39A51100D864E6 /* empty_relationship_example.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = empty_relationship_example.json; sourceTree = "<group>"; };
781CF6C91C1ECC260052D755 /* generic_relationships_example.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = generic_relationships_example.json; sourceTree = "<group>"; };
78A5C1C81C1E1336008C8632 /* NewsFeedPostResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NewsFeedPostResource.h; sourceTree = "<group>"; };
78A5C1C91C1E1336008C8632 /* NewsFeedPostResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NewsFeedPostResource.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -281,6 +284,7 @@
03FBD8D81AB879E800789DF3 /* Assets */ = {
isa = PBXGroup;
children = (
7817CDAD1C39A51100D864E6 /* empty_relationship_example.json */,
781CF6C91C1ECC260052D755 /* generic_relationships_example.json */,
03FBD8D91AB879FD00789DF3 /* main_example.json */,
03FBD8E11AB8E46E00789DF3 /* error_example.json */,
Expand Down Expand Up @@ -368,6 +372,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7817CDAF1C39A85000D864E6 /* empty_relationship_example.json in Resources */,
781CF6CB1C1ECC710052D755 /* generic_relationships_example.json in Resources */,
03A055D31868E038004807F0 /* Images.xcassets in Resources */,
03866459186CCE6600985CEC /* CHANGELOG.md in Resources */,
Expand All @@ -386,6 +391,7 @@
03FBD8E31AB8E46E00789DF3 /* error_example.json in Resources */,
03A055E41868E038004807F0 /* InfoPlist.strings in Resources */,
781CF6CA1C1ECC260052D755 /* generic_relationships_example.json in Resources */,
7817CDAE1C39A51100D864E6 /* empty_relationship_example.json in Resources */,
03FBD8DA1AB879FD00789DF3 /* main_example.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
19 changes: 19 additions & 0 deletions Project/JSONAPITests/JSONAPITests.m
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,27 @@ - (void)testGenericSerialization {
XCTAssertEqualObjects(serializedSecondPost[@"relationships"][@"publisher"][@"data"][@"type"], @"SocialCommunity", @"SocialCommunity type should be 'SocialCommunity'");
}

#pragma mark - Empty relationship tests

- (void)testEmptyRelationship {
NSDictionary *json = [self emptyRelationshipsExampleJSON];
JSONAPI *jsonAPI = [JSONAPI jsonAPIWithDictionary:json];

NewsFeedPostResource *testPost = jsonAPI.resource;

XCTAssertNil(testPost.publisher, @"Test post's publisher should be nil");

XCTAssertNotNil(testPost.attachments, @"Test post's attachments should not be nil");
XCTAssertEqual(testPost.attachments.count, 1, @"Test post's attachments should contain 1 object");
XCTAssertTrue(((JSONAPIResourceBase *)testPost.attachments.firstObject).class == MediaResource.class, @"First attachment should be of class MediaResource");
}

#pragma mark - Private

- (NSDictionary*)emptyRelationshipsExampleJSON {
return [self jsonFor:@"empty_relationship_example" ofType:@"json"];
}

- (NSDictionary*)genericRelationshipsExampleJSON {
return [self jsonFor:@"generic_relationships_example" ofType:@"json"];
}
Expand Down
25 changes: 25 additions & 0 deletions Project/JSONAPITests/empty_relationship_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"data": {
"attributes": {
"createdAt": "2015-10-28T19:35:28.194Z",
"title": "Empty relationship test post",
"text": "Test text"
},
"id": 1,
"type": "NewsFeedPost",
"relationships": {
"publisher": {
"data": null
},
"attachments" : {
"data" : [
{
"id" : 15,
"type" : "Media"
}
]
}
}

}
}