Added specs around behavior of empty mappable payloads and RKObjectLoaderDelegate invocations. Fixed issue where empty hashes ({}) would result in no delegate methods being invoked. fixes #225

This commit is contained in:
Blake Watters
2011-07-24 01:25:50 -04:00
parent 570b13ca07
commit 3a5354c4a0
4 changed files with 55 additions and 7 deletions

View File

@@ -164,7 +164,7 @@
return [RKObjectMappingResult mappingResultWithDictionary:[NSDictionary dictionaryWithObject:self.targetObject forKey:@""]];
}
return nil;
return [RKObjectMappingResult mappingResultWithDictionary:[NSDictionary dictionary]];
}
id parsedData = [parser objectFromString:bodyAsString error:error];
@@ -223,15 +223,11 @@
NSError* error = nil;
_result = [[self performMapping:&error] retain];
NSAssert(_result || error, @"Expected performMapping to return a mapping result or an error.");
if (self.result) {
[self processMappingResult:self.result];
} else if (error) {
[self didFailLoadWithError:error];
} else {
// TODO: This is causing no didLoadObject: flavored delegate methods to be invoked.
// A nil response with an error indicates success, but no mapping results
[self finalizeLoad:YES error:nil];
}
[pool drain];

View File

@@ -260,7 +260,9 @@
}
// If we found nothing eligible for mapping in the content, add an unmappable key path error and fail mapping
if (foundMappable == NO) {
// If the content is empty, we don't consider it an error
BOOL isEmpty = [self.sourceObject respondsToSelector:@selector(count)] && ([self.sourceObject count] == 0);
if (foundMappable == NO && !isEmpty) {
[self addErrorForUnmappableKeyPath:@""];
return nil;
}

View File

@@ -431,4 +431,36 @@
assertThatBool(responseLoader.success, is(equalToBool(YES)));
}
- (void)itShouldInvokeTheDelegateOnSuccessIfTheResponseIsAnEmptyArray {
RKObjectManager* objectManager = RKSpecNewObjectManager();
RKSpecResponseLoader* responseLoader = [RKSpecResponseLoader responseLoader];
responseLoader.timeout = 20;
[objectManager loadObjectsAtResourcePath:@"/empty/array" delegate:responseLoader];
[responseLoader waitForResponse];
assertThat(responseLoader.objects, isNot(nilValue()));
assertThatBool([responseLoader.objects isKindOfClass:[NSArray class]], is(equalToBool(YES)));
assertThat(responseLoader.objects, is(empty()));
}
- (void)itShouldInvokeTheDelegateOnSuccessIfTheResponseIsAnEmptyDictionary {
RKObjectManager* objectManager = RKSpecNewObjectManager();
RKSpecResponseLoader* responseLoader = [RKSpecResponseLoader responseLoader];
responseLoader.timeout = 20;
[objectManager loadObjectsAtResourcePath:@"/empty/dictionary" delegate:responseLoader];
[responseLoader waitForResponse];
assertThat(responseLoader.objects, isNot(nilValue()));
assertThatBool([responseLoader.objects isKindOfClass:[NSArray class]], is(equalToBool(YES)));
assertThat(responseLoader.objects, is(empty()));
}
- (void)itShouldInvokeTheDelegateOnSuccessIfTheResponseIsAnEmptyString {
RKObjectManager* objectManager = RKSpecNewObjectManager();
RKSpecResponseLoader* responseLoader = [RKSpecResponseLoader responseLoader];
responseLoader.timeout = 20;
[objectManager loadObjectsAtResourcePath:@"/empty/string" delegate:responseLoader];
[responseLoader waitForResponse];
assertThat(responseLoader.objects, isNot(nilValue()));
assertThatBool([responseLoader.objects isKindOfClass:[NSArray class]], is(equalToBool(YES)));
assertThat(responseLoader.objects, is(empty()));
}
@end

View File

@@ -101,6 +101,24 @@ class RestKit::SpecServer < Sinatra::Base
params.to_json
end
get '/empty/array' do
status 200
content_type 'application/json'
[].to_json
end
get '/empty/dictionary' do
status 200
content_type 'application/json'
{}.to_json
end
get '/empty/string' do
status 200
content_type 'application/json'
""
end
# start the server if ruby file executed directly
run! if app_file == $0
end