Permalink
Browse files

Fix bug where class chaining in NSArrays could cause bad results.

If the container type has been specified when deserializing an array,
disregard the attached class information.
  • Loading branch information...
1 parent 5bfa8be commit c823d1a7549ef0cd008b4f460d76d0f87b5cc187 @pashields committed Mar 21, 2012
Showing with 16 additions and 0 deletions.
  1. +5 −0 ClassMapper/Categories/NSMutableArray+ClassMapper.m
  2. +11 −0 ClassMapperTests/ClassMapperTests.m
@@ -20,6 +20,11 @@ - (NSMutableArray *)_cm_update_with:(NSArray *)serialized withClass:(Class)class
}
[self removeAllObjects];
for (id cereal in serialized) {
+ /* Generally, we want to chain class types. If someone has clearly specified
+ the type of the container, however, we want to break the chain. */
+ if ([class isSubclassOfClass:[NSArray class]]) {
+ class = nil;
+ }
class = FIRST_NOT_NULL(instanceClass, class, [cereal class], nil);
[self addObject:[ClassMapper deserialize:cereal toClass:class]];
}
@@ -158,6 +158,17 @@ - (void)testDictWithNullForDictToObj {
STAssertTrue(zip.aDict == (NSDictionary *)[NSNull null], @"Failure to deserialize NSNull when null is in for dict.");
}
+
+#pragma mark array to simple array
+- (void)testSimpleArrayToSimpleArray {
+ /* ["foo"] -> ["foo"] */
+ NSArray *ray = [NSArray arrayWithObject:@"foo"];
+
+ NSArray *rayCopy = [ClassMapper deserialize:ray toClass:[NSArray class]];
+
+ STAssertEqualObjects([ray lastObject], [rayCopy lastObject], @"Simple array deserialization fails");
+}
+
#pragma mark array to classarray
- (void)testArrayToArray {
/* [{"aString":"MOTORHEAD"}, {"aString":"BLACK SABBATH"}] -> [Bar, Bar] */

0 comments on commit c823d1a

Please sign in to comment.