Christo
Christo

Reputation: 182

Restkit - Wrong object mapping expected

I get this mapping error when trying to map a response using RestKit 0.20.2. The problem is introduced when using the same class for multiple descriptors. The error is as follow:

RKMapperOperation.m:98 Adding mapping error: Expected an object mapping for class of type   'Customer', provider returned one for 'CustomerUploadResponse'

Here's my scenario:

So I set up the request and response descriptors as follow:

NSString* getCustomerPath = @"/api/repository/1/Customers/4834";
NSString* postCustomerPath = @"/api/repository/1/Customers";

//Post customer mapping and descriptor
RKObjectMapping *postCustomerMapping = [RKObjectMapping requestMapping];
[postCustomerMapping addAttributeMappingsFromArray: [NSArray arrayWithObjects:@"ID",@"LastModifiedTime",@"Name",@"ApiKey",@"CustomFieldValues",nil]];

RKRequestDescriptor *postCustomerRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:postCustomerMapping objectClass:[Customer class] rootKeyPath:nil];
[[RKObjectManager sharedManager] addRequestDescriptor:postCustomerRequestDescriptor];

//Post customer response mapping and descriptor
RKObjectMapping *postCustomerResponseMapping = [RKObjectMapping mappingForClass:[CustomerUploadResponse class]];
[postCustomerResponseMapping addAttributeMappingsFromArray:[NSArray arrayWithObjects:@"ID",nil]];

RKResponseDescriptor *postCustomerResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:postCustomerResponseMapping pathPattern:postCustomerPath keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]];
[[RKObjectManager sharedManager] addResponseDescriptor:postCustomerResponseDescriptor];


//Get customer response mapping and descriptor
RKObjectMapping *getCustomerResponseMapping = [RKObjectMapping mappingForClass:[Customer class]];
[getCustomerResponseMapping addAttributeMappingsFromArray:[NSArray arrayWithObjects:@"ID",@"LastModifiedTime",@"Name",@"ApiKey",@"CustomFieldValues",nil]];

RKResponseDescriptor *getCustomerResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:getCustomerResponseMapping pathPattern:getCustomerPath keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]];
[[RKObjectManager sharedManager] addResponseDescriptor:getCustomerResponseDescriptor];

When fetching a Customer with the following:

[[RKObjectManager sharedManager] getObject:nil path:getCustomerPath parameters:nil     success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSLog(@"Success fetching customer");
} failure:^(RKObjectRequestOperation *operation, NSError *error){
    NSLog(@"Failure fetching customer");
}];

Success fetching customer is written to the log.

However, when posting a Customer with the following:

[[RKObjectManager sharedManager] postObject:customer path:postCustomerPath parameters:nil   success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSLog(@"Success posting customer");
} failure:^(RKObjectRequestOperation *operation, NSError *error){
    NSLog(@"Failure posting customer");
}];

Success posting customer with id (null) is written to the log and a mapping error is encountered.

W restkit.object_mapping:RKMapperOperation.m:98 Adding mapping error: Expected an object mapping for class of type 'Customer', provider returned one for 'CustomerUploadResponse'

I've found that the two descriptors each works fine independently when the other one is omitted. I've also checked, there is no confusion between the two paths.

Upvotes: 0

Views: 701

Answers (1)

tekstop
tekstop

Reputation: 81

Why do you have two separate response descriptors? One should suffice! When you post an object, RestKit expects the same object back and hence will throw the mapping error. From your mapping, you have the same object, you don't need to create 2 mappings.

A Typical example is:

You have a customer object (without the id) and you post it to your webservice. The webservice inserts it in to a database and fills in the id (which was missing initially). The webservice returns the filled in object and RestKit automatically maps the missing id field in your customer object with the filled in field returned.

Upvotes: 1

Related Questions