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:
A
getObjectfrom/api/repository/1/Customers/:idfetches aCustomerfrom the server with a specific id.A
Customerposted to/api/repository/1/Customersuploads it to the server and expects aCustomerPostResponseobject to be returned.
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.