Skip to content

Commit db4f268

Browse files
author
Josh Holtz
committed
Merge branch 'danylhebreux-StringSupport'
2 parents 254826f + 7770052 commit db4f268

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

Classes/JSONAPIResource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@interface JSONAPIResource : NSObject
1212

13-
@property (nonatomic, strong) NSNumber *ID;
13+
@property (nonatomic, strong) id ID;
1414
@property (nonatomic, strong) NSString *href;
1515
@property (nonatomic, strong) NSDictionary *links;
1616

Classes/JSONAPIResource.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ - (void)linkLinks:(NSDictionary*)linked {
155155
// Gets linked objects for the resource
156156
id linksTo = [self.links objectForKey:linkTypeUnmapped];
157157
if ([linksTo isKindOfClass:[NSNumber class]] == YES || [linksTo isKindOfClass:[NSString class]] == YES) {
158+
158159
JSONAPIResource *linkedResource = [[linked objectForKey:linkType] objectForKey:linksTo];
159160

160161
if (linkedResource != nil) {
@@ -164,7 +165,7 @@ - (void)linkLinks:(NSDictionary*)linked {
164165
} else if ([linksTo isKindOfClass:[NSArray class]] == YES) {
165166
NSMutableArray *linkedResources = [NSMutableArray array];
166167
[self.__resourceLinks setObject:linkedResources forKey:linkTypeUnmapped];
167-
for (NSNumber *linkedId in linksTo) {
168+
for (id linkedId in linksTo) {
168169
JSONAPIResource *linkedResource = [[linked objectForKey:linkType] objectForKey:linkedId];
169170
if (linkedResource != nil) {
170171
[linkedResources addObject:linkedResource];

Project/JSONAPI/ViewController.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ - (void)viewDidLoad
2424
{
2525
[super viewDidLoad];
2626

27+
return;
28+
2729
// Setup
2830
[JSONAPIResourceLinker link:@"author" toLinkedType:@"people"];
2931
[JSONAPIResourceModeler useResource:[CommentResource class] toLinkedType:@"comments"];

Project/JSONAPITests/JSONAPITests.m

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,53 @@ - (void)testResourcesObject {
129129
XCTAssertEqualObjects([post objectForKey:@"name"], [resource objectForKey:@"name"], @"Posts name does not equal %@", [post objectForKey:@"name"]);
130130
}
131131

132+
- (void)testResourcesObjectWithStringId {
133+
134+
NSDictionary *meta = @{ @"page_number" : @1, @"number_of_pages" : @5};
135+
NSDictionary *linked = @{ @"authors" : @[ @{ @"id" : @"9", @"name" : @"Josh" } ] };
136+
NSDictionary *post = @{ @"id" : @1, @"name" : @"Josh is awesome", @"links" : @{ @"author" : @"9" } };
137+
NSArray *posts = @[ post ];
138+
NSDictionary *json = @{ @"meta" : meta, @"linked" : linked, @"posts" : posts };
139+
140+
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
141+
JSONAPIResource *resource = [jsonAPI resourceForKey:@"posts"];
142+
143+
XCTAssertEqualObjects([post objectForKey:@"id"], resource.ID, @"Posts ID does not equal %@", [post objectForKey:@"id"]);
144+
XCTAssertEqualObjects([post objectForKey:@"name"], [resource objectForKey:@"name"], @"Posts name does not equal %@", [post objectForKey:@"name"]);
145+
}
146+
147+
- (void)testResourceObjectsWithArrayOfStringIds {
148+
149+
NSDictionary *meta = @{ @"page_number" : @1, @"number_of_pages" : @5};
150+
NSDictionary *linkedAuthor9 = @{ @"id" : @"9", @"name" : @"Josh" };
151+
NSDictionary *linkedAuthor11 = @{ @"id" : @"11", @"name" : @"Bandit" };
152+
NSArray *linkedAuthors = @[ linkedAuthor9, linkedAuthor11 ];
153+
NSDictionary *linked = @{ @"authors" : linkedAuthors };
154+
NSDictionary *post = @{ @"id" : @1, @"name" : @"Josh is awesome", @"links" : @{ @"authors" : @[ @"9", @"11" ] } };
155+
NSArray *posts = @[ post ];
156+
NSDictionary *json = @{ @"meta" : meta, @"linked" : linked, @"posts" : posts };
157+
158+
JSONAPI *jsonAPI = [[JSONAPI alloc] initWithDictionary:json];
159+
JSONAPIResource *resource = [jsonAPI resourceForKey:@"posts"];
160+
161+
NSArray *linkedAuthorsResources = [resource linkedResourceForKey:@"authors"];
162+
JSONAPIResource *linkedAuthorResource9, *linkedAuthorResource11;
163+
for (JSONAPIResource *linkedAuthorResource in linkedAuthorsResources) {
164+
if ([linkedAuthorResource.ID isEqualToString:[linkedAuthor9 objectForKey:@"id"]] == YES) {
165+
linkedAuthorResource9 = linkedAuthorResource;
166+
} else if ([linkedAuthorResource.ID isEqualToString:[linkedAuthor11 objectForKey:@"id"]] == YES) {
167+
linkedAuthorResource11 = linkedAuthorResource;
168+
}
169+
}
170+
171+
XCTAssert(linkedAuthorsResources.count == linkedAuthors.count, @"Linked author resource count is not %d", linkedAuthors.count);
172+
173+
XCTAssertEqualObjects([linkedAuthorResource9 objectForKey:@"name"], [linkedAuthor9 objectForKey:@"name"], @"Linked author's 9 name is not equal to %@", [linkedAuthor9 objectForKey:@"name"]);
174+
175+
XCTAssertEqualObjects([linkedAuthorResource11 objectForKey:@"name"], [linkedAuthor11 objectForKey:@"name"], @"Linked author's 11 name is not equal to %@", [linkedAuthor9 objectForKey:@"name"]);
176+
177+
}
178+
132179
- (void)testResourceLinkSameTypeName {
133180

134181
NSDictionary *meta = @{ @"page_number" : @1, @"number_of_pages" : @5};

0 commit comments

Comments
 (0)