Skip to content

Commit

Permalink
Only look for unsupported root object types when the body decodes wit…
Browse files Browse the repository at this point in the history
…hout error (big fix)
  • Loading branch information
Roy Ratcliffe committed Mar 30, 2012
1 parent 5e55b79 commit 7f2ff29
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 179 deletions.
53 changes: 37 additions & 16 deletions ActiveResourceKit/ARService.m
Original file line number Diff line number Diff line change
Expand Up @@ -303,20 +303,27 @@ - (void)buildWithAttributes:(NSDictionary *)attributes completionHandler:(ARReso
// element path with the site, using the site's scheme, host and port.
NSString *path = [self newElementPathWithPrefixOptions:nil];
[self get:path completionHandler:^(ARHTTPResponse *response, id object, NSError *error) {
if ([object isKindOfClass:[NSDictionary class]])
if (object && error == nil)
{
NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithDictionary:object];
[attrs addEntriesFromDictionary:attributes];
completionHandler(response, [[ARResource alloc] initWithService:self attributes:attrs], nil);
if ([object isKindOfClass:[NSDictionary class]])
{
NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithDictionary:object];
[attrs addEntriesFromDictionary:attributes];
completionHandler(response, [[ARResource alloc] initWithService:self attributes:attrs], nil);
}
else
{
// The response body decodes successfully but it does not
// decode to a dictionary. It must be something else, either
// an array, a string or some other primitive type. In which
// case, building with attributes must fail even though
// ostensibly the operation has succeeded. Set up an error.
completionHandler(response, nil, [NSError errorWithDomain:ARErrorDomain code:ARUnsupportedRootObjectTypeError userInfo:nil]);
}
}
else
{
// The response body decodes successfully but it does not
// decode to a dictionary. It must be something else, either
// an array, a string or some other primitive type. In which
// case, building with attributes must fail even though
// ostensibly the operation has succeeded. Set up an error.
completionHandler(response, nil, [NSError errorWithDomain:ARErrorDomain code:ARUnsupportedRootObjectTypeError userInfo:nil]);
completionHandler(response, object, error);
}
}];
}
Expand Down Expand Up @@ -355,13 +362,20 @@ - (void)findSingleWithID:(NSNumber *)ID options:(NSDictionary *)options completi
[self splitOptions:options prefixOptions:&prefixOptions queryOptions:&queryOptions];
NSString *path = [self elementPathForID:ID prefixOptions:prefixOptions queryOptions:queryOptions];
[self get:path completionHandler:^(ARHTTPResponse *response, id object, NSError *error) {
if ([object isKindOfClass:[NSDictionary class]])
if (object && error == nil)
{
completionHandler(response, [self instantiateRecordWithAttributes:object prefixOptions:prefixOptions], nil);
if ([object isKindOfClass:[NSDictionary class]])
{
completionHandler(response, [self instantiateRecordWithAttributes:object prefixOptions:prefixOptions], nil);
}
else
{
completionHandler(response, nil, [NSError errorWithDomain:ARErrorDomain code:ARUnsupportedRootObjectTypeError userInfo:nil]);
}
}
else
{
completionHandler(response, nil, [NSError errorWithDomain:ARErrorDomain code:ARUnsupportedRootObjectTypeError userInfo:nil]);
completionHandler(response, object, error);
}
}];
}
Expand All @@ -373,13 +387,20 @@ - (void)findOneWithOptions:(NSDictionary *)options completionHandler:(ARResource
{
NSString *path = [NSString stringWithFormat:@"%@%@", from, ARQueryStringForOptions([options objectForKey:ARParamsKey])];
[self get:path completionHandler:^(ARHTTPResponse *response, id object, NSError *error) {
if ([object isKindOfClass:[NSDictionary class]])
if (object && error == nil)
{
completionHandler(response, [self instantiateRecordWithAttributes:object prefixOptions:nil], nil);
if ([object isKindOfClass:[NSDictionary class]])
{
completionHandler(response, [self instantiateRecordWithAttributes:object prefixOptions:nil], nil);
}
else
{
completionHandler(response, nil, [NSError errorWithDomain:ARErrorDomain code:ARUnsupportedRootObjectTypeError userInfo:nil]);
}
}
else
{
completionHandler(response, nil, [NSError errorWithDomain:ARErrorDomain code:ARUnsupportedRootObjectTypeError userInfo:nil]);
completionHandler(response, object, error);
}
}];
}
Expand Down
Loading

0 comments on commit 7f2ff29

Please sign in to comment.