blackgun
blackgun

Reputation: 411

RestKit POST response with wrong mapping

When I am doing a POST of an object, the response mapped to wrong object.

// Resquest for post new article

RKObjectMapping* articleRequestMapping = [RKObjectMapping requestMapping];
[articleRequestMapping addAttributeMappingsFromDictionary:@{
                                                     @"title"   : @"title",
                                                     @"body"    : @"body",
                                                     }];

RKRequestDescriptor *requestDescriptorArticle = [RKRequestDescriptor
                                                 requestDescriptorWithMapping:articleRequestMapping
                                                 objectClass:[Article class]
                                                 rootKeyPath:nil
                                                 method:RKRequestMethodPOST];

[objectManager addRequestDescriptor:requestDescriptorArticle];


// Response for post new article 
// response.body={
//   "result": {
//     "ok": 1
//   }
// }

RKObjectMapping *resultMapping = [RKObjectMapping mappingForClass:[Result class]];
[resultMapping addAttributeMappingsFromDictionary:@{
                                                    @"ok"   :  @"ok"
                                                  }];

RKResponseDescriptor *resArticleCreate = [RKResponseDescriptor
                                          responseDescriptorWithMapping:resultMapping
                                          method:RKRequestMethodPOST
                                          pathPattern:[NSString stringWithFormat:@"%@%@",apiVersion,@"/articles"]
                                          keyPath:@"result"
                                          statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:resArticleCreate];

The log:

2013-10-09 07:05:43.335 TabbedDemo[35156:4703] D restkit.object_mapping:RKMapperOperation.m:378 Executing mapping operation for representation: {
    result =     {
        ok = 1;
    };
}
 and targetObject: <Article: 0x7d88670>

targetObject is Article..., but it supposed to be Result...

I found the old similar issue here: https://github.com/RestKit/RestKit/issues/1081 I am using v0.21 (master branch), now I solve it by forcing targetObject.

// normal one, but with wrong mapping object.

//    [[RKObjectManager sharedManager] postObject:article path:nil parameters:@{@"_csrf" : self._csrf}
//                                        success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){
//                                            NSLog(@"result:%@",[mappingResult firstObject]);
//                                        }
//                                        failure:^(RKObjectRequestOperation *operation, NSError *error) {
//                                            NSLog(@"Hit error: %@", error);
//                                        }];

RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] appropriateObjectRequestOperationWithObject:article
                                                                        method:RKRequestMethodPOST
                                                                        path:nil
                                                                        parameters:@{@"_csrf" : self._csrf}];
Result *r1 = [Result new];
operation.targetObject = r1;

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"result:%@",[mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Hit error: %@", error);
}];

[operation start];

Is that a RestKit issue or I am doing wrong?

Upvotes: 1

Views: 900

Answers (1)

Wain
Wain

Reputation: 119041

It is a RestKit design assumption. It does cause issues in some cases like yours. When posting and object, RestKit will always try to map the response into that same object.

Your workaround solution is a good alternative.

You could alternatively post a dictionary and have the result mapped back into that dictionary.

Upvotes: 1

Related Questions