Skip to content

Commit 1b16399

Browse files
author
Josh Holtz
committed
Merge pull request #13 from edopelawi/master
Fix circular reference bug on JSONAPIResource
2 parents 4b858e4 + aff2883 commit 1b16399

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

Classes/JSONAPI.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ - (void)inflateWithDictionary:(NSDictionary*)dictionary {
120120
_includedResources = includedResources;
121121

122122
// Link included with included
123-
// TODO: Need to look into / stop circular references
124123
for (NSDictionary *typeIncluded in _includedResources.allValues) {
125124
for (JSONAPIResource *resource in typeIncluded.allValues) {
126125
[resource linkWithIncluded:self];

Classes/JSONAPIResource.m

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ - (void)linkWithIncluded:(JSONAPI*)jsonAPI {
172172

173173
JSONAPIResource *linkedResource = included[linkType][linksToId];
174174
if (linkedResource != nil) {
175-
[linkedResources addObject:linkedResources];
175+
[linkedResources addObject:linkedResource];
176176
}
177177
}
178178
}
@@ -226,6 +226,15 @@ - (id)copyWithZone:(NSZone *)zone {
226226

227227
}
228228

229+
BOOL isEmptyDictionary = !self.__dictionary || self.__dictionary.count == 0;
230+
231+
if (isEmptyDictionary) {
232+
for (NSString *key in self.propertyKeys) {
233+
id selfValue = [self valueForKey:key];
234+
[copy setValue:selfValue forKey:key];
235+
}
236+
}
237+
229238
return copy;
230239
}
231240

@@ -302,4 +311,32 @@ - (void)encodeWithCoder:(NSCoder *)aCoder
302311
}
303312
}
304313

314+
#pragma mark - NSObject -
315+
316+
- (BOOL)isEqual:(id)object
317+
{
318+
if (self == object) return YES;
319+
if (![object isMemberOfClass:[self class]]) return NO;
320+
321+
for (NSString *key in self.propertyKeys) {
322+
id selfValue = [self valueForKey:key];
323+
id objectValue = [object valueForKey:key];
324+
325+
BOOL valuesEqual = ((selfValue == nil && objectValue == nil) || [selfValue isEqual:objectValue]);
326+
if (!valuesEqual) return NO;
327+
}
328+
329+
return YES;
330+
}
331+
332+
- (NSUInteger)hash {
333+
NSUInteger value = 0;
334+
335+
for (NSString *key in self.propertyKeys) {
336+
value ^= [[self valueForKey:key] hash];
337+
}
338+
339+
return value;
340+
}
341+
305342
@end

0 commit comments

Comments
 (0)