gonzalezreal / Groot

From JSON to Core Data and back.
Other
534 stars 61 forks source link

GRTUniquingSerializationStrategy: attribute with null value replaces original value #86

Closed ValentinStrazdin closed 6 years ago

ValentinStrazdin commented 6 years ago

I have NSManagedObject with identity attribute. This object has several nullable properties. I set values for these properties and save NSManagedObjectContext. When I get JSON from server containing list of objects I want to merge these changes with my existing objects. Although corresponding attributes are null my saved properties are replaced with these nulls.

I looked through your code and found the following bug in file "NSManagedObject+Groot.m" line 167:

if (rawValue != nil) {
     if (rawValue != [NSNull null]) {
        NSValueTransformer *transformer = [attribute grt_JSONTransformer];
        if (transformer) {
            value = [transformer transformedValue:rawValue];
        } else {
            value = rawValue;
        }
    }
} else if (mergeChanges) {
        // Just validate the current value
        value = [self valueForKey:attribute.name];
        [self validateValue:&value forKey:attribute.name error:outError];  

        return;
    }

So my case is not correctly processed by this code.

Could you replace it with:

if (rawValue != nil && rawValue != [NSNull null]) {
        NSValueTransformer *transformer = [attribute grt_JSONTransformer];
        if (transformer) {
            value = [transformer transformedValue:rawValue];
        } else {
            value = rawValue;
        }
} else if (mergeChanges) {
        // Just validate the current value
        value = [self valueForKey:attribute.name];
        [self validateValue:&value forKey:attribute.name error:outError];

        return;
    }
ManueGE commented 6 years ago

I don’t think this is a bug. If the json from the server contains a key with the value nil, the corresponding property must be set to nil too.

Why should groot ignore nil values in Json? The only ignored keys are those which are not included in the Json.

gonzalezreal commented 6 years ago

As @ManueGE says, if the JSON has explicit null attributes, it is not a bug. It would be a bug if a partial update would nullify all the attributes not present in the JSON.

ManueGE commented 6 years ago

@ValentinStrazdin As a suggestion, if you want to achieve this behaviour, you can still use a JSONDictionaryTransformerName annotation, and remove the keys whose value is null from the dictionary. This way, they will be ignored.