iosrestrestkitupdatemodel

RestKit Update object maps null attributes


I am trying to perform a partial update of a model using the RestKit library. I am already able to create/list the models without any problem. Also, I am able to crate/list and update using the Rest API and postman. Problem comes when updating using RestKit.

This is my mapping:

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[UserProfile class]];
NSDictionary *resultsMappingDictionary = @{@"id": @"profileId",
                                           @"username": @"username",
                                           @"first_name": @"firstName",
                                           @"last_name": @"lastName",
                                           @"email": @"email",
                                           @"description": @"job",
                                           @"image": @"imgUrl",
                                           @"auth_token": @"authToken"
                                           };

responseMapping.assignsDefaultValueForMissingAttributes = NO;
[responseMapping addAttributeMappingsFromDictionary:resultsMappingDictionary];

return responseMapping;

When I perform an update with a UserProfile object only containing the email, with the method

- (void)putObject:(id)object
         path:(NSString *)path
   parameters:(NSDictionary *)parameters
      success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
      failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure

I face an error coming from the server,

NSLocalizedRecoverySuggestion={"first_name":["This field may not be null."],"last_name":["This field may not be null."],"description":["This field may not be null."],"image":["This field may not be null."]

I think that RestKit maps to null every property that is nil in the model. I'd specifically want not to map them at all, so the server does not try to update any null values.

I tried to use

responseMapping.assignsDefaultValueForMissingAttributes = NO;

in the mapping, but with no change in the behavior.

Is there any way I can tell RestKit to just ignore and not map any property that is nil?

PS: I tried performing that same call with all non-null parameters, and it updates the model with no problem at all.


Solution

  • You need to create a specific mapping with only the keys you want to process.

    assignsDefaultValueForMissingAttributes is used to set values for missing keys. What you have is missing values for defined keys. Hence assignsDefaultValueForMissingAttributes doesn't do anything for you.