Skip to content

Commit

Permalink
Fix a bug that caused request: didLoad: to be called with the raw res…
Browse files Browse the repository at this point in the history
…ponse instead of a string when the response does not represent a valid JSON object.

Summary:
If the response is not a JSON object, convert the actual response to a string instead of using the NSData object.
This will break clients that depend on the current behavior. If you need access to the raw response, use request: diReceiveResponse: instead.

Test Plan: Test with stream.publish requests

Reviewers: jacl, dgibson, yariv, caabernathy

Reviewed By: jacl

CC: leon

Differential Revision: https://phabricator.fb.com/D427844

Task ID: 842334
  • Loading branch information
suhasjoshi committed Mar 15, 2012
1 parent 84e953c commit 81c2954
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
13 changes: 8 additions & 5 deletions sample/Hackbook/Hackbook/RootViewController.m
Expand Up @@ -373,8 +373,9 @@ - (void)fbSessionInvalidated {

#pragma mark - FBRequestDelegate Methods
/**
* Called when the Facebook API request has returned a response. This callback
* gives you access to the raw response. It's called before
* Called when the Facebook API request has returned a response.
*
* This callback gives you access to the raw response. It's called before
* (void)request:(FBRequest *)request didLoad:(id)result,
* which is passed the parsed response object.
*/
Expand All @@ -384,9 +385,11 @@ - (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)respons

/**
* Called when a request returns and its response has been parsed into
* an object. The resulting object may be a dictionary, an array, a string,
* or a number, depending on the format of the API response. If you need access
* to the raw response, use:
* an object.
*
* The resulting object may be a dictionary, an array or a string, depending
* on the format of the API response. If you need access to the raw response,
* use:
*
* (void)request:(FBRequest *)request
* didReceiveResponse:(NSURLResponse *)response
Expand Down
14 changes: 11 additions & 3 deletions src/FBRequest.h
Expand Up @@ -105,7 +105,11 @@ typedef NSUInteger FBRequestState;
- (void)requestLoading:(FBRequest *)request;

/**
* Called when the server responds and begins to send back data.
* Called when the Facebook API request has returned a response.
*
* This callback gives you access to the raw response. It's called before
* (void)request:(FBRequest *)request didLoad:(id)result,
* which is passed the parsed response object.
*/
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response;

Expand All @@ -118,8 +122,12 @@ typedef NSUInteger FBRequestState;
* Called when a request returns and its response has been parsed into
* an object.
*
* The resulting object may be a dictionary, an array, a string, or a number,
* depending on thee format of the API response.
* The resulting object may be a dictionary, an array or a string, depending
* on the format of the API response. If you need access to the raw response,
* use:
*
* (void)request:(FBRequest *)request
* didReceiveResponse:(NSURLResponse *)response
*/
- (void)request:(FBRequest *)request didLoad:(id)result;

Expand Down
13 changes: 9 additions & 4 deletions src/FBRequest.m
Expand Up @@ -189,7 +189,6 @@ - (id)parseJsonResponse:(NSData *)data error:(NSError **)error {
NSString* responseString = [[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding]
autorelease];
SBJSON *jsonParser = [[SBJSON new] autorelease];
if ([responseString isEqualToString:@"true"]) {
return [NSDictionary dictionaryWithObject:@"true" forKey:@"result"];
} else if ([responseString isEqualToString:@"false"]) {
Expand All @@ -203,9 +202,15 @@ - (id)parseJsonResponse:(NSData *)data error:(NSError **)error {
}


SBJSON *jsonParser = [[SBJSON alloc] init];
id result = [jsonParser objectWithString:responseString];

if (![result isKindOfClass:[NSArray class]]) {
[jsonParser release];

if (result == nil) {
return responseString;
}

if ([result isKindOfClass:[NSDictionary class]]) {
if ([result objectForKey:@"error"] != nil) {
if (error != nil) {
*error = [self formError:kGeneralErrorCode
Expand Down Expand Up @@ -272,7 +277,7 @@ - (void)handleResponseData:(NSData *)data {
[self failWithError:error];
} else if ([_delegate respondsToSelector:
@selector(request:didLoad:)]) {
[_delegate request:self didLoad:(result == nil ? data : result)];
[_delegate request:self didLoad:result];
}

}
Expand Down

0 comments on commit 81c2954

Please sign in to comment.