Crash when mapping JSON null to BOOL property

338 views
Skip to first unread message

Levi Brown

unread,
Nov 13, 2012, 11:38:15 AM11/13/12
to res...@googlegroups.com
Hello all,

I've got some JSON coming back from a service which at some times will have null instead of true/false for an attribute which is a boolean; Like this:

    "hide_locations" : null,

My object to receive this value has a property:

    @property (nonatomic, assign) BOOL hideLocations;


My mapping is configured like this:


    RKObjectMapping *retVal = [RKObjectMapping mappingForClass:[self class]];

    [retVal mapKeyPath:@"hide_locations" toAttribute:@"hideLocations"];


But when I perform the mapping, RestKit 0.10.3 is crashing on this line:

    [self.destinationObject setValue:value forKeyPath:attributeMapping.destinationKeyPath];

in
    - (void)applyAttributeMapping:(RKObjectAttributeMapping *)attributeMapping withValue:(id)value

in RKObjectMappingOperation.m with the following error:

2012-11-13 09:33:04.352 Tests[73816:5f03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '[<MyClass 0x13ba9a10> setNilValueForKey]: could not set nil as the value for the key hideLocations.'


It seems quite odd to me that a mature framework like this doesn't handle this situation more gracefully, so I can't help but think I've configured something incorrectly. I would appreciate any insights you might have.

Thanks,

Levi

Blake Watters

unread,
Nov 13, 2012, 7:47:24 PM11/13/12
to res...@googlegroups.com
I would recommend trying this on the development branch. We have recently merged type introspection support for non-object property types, which should coerce the null into a false value. I will add a unit test for this case to development to verify.

Blake Watters

unread,
Nov 13, 2012, 8:43:40 PM11/13/12
to res...@googlegroups.com
Actually the Boolean type introspection interprets the backing type as a NSNumber. I have a failing test for this case and will think on how to handle...

Levi Brown

unread,
Nov 14, 2012, 11:39:39 AM11/14/12
to res...@googlegroups.com
Thanks Blake. I'll look forward to your solution.

Cheers,

Levi

Blake Watters

unread,
Nov 14, 2012, 3:47:13 PM11/14/12
to res...@googlegroups.com
You should probably just implement `setNilValueForKey:` on your target object and handle it appropriately. I don't know that RK can or should try to predict how you would want to handle a nil value for scalar types in all cases, so its probably better to keep the behavior uniform. Such a mapping will always invoke setNilValueForKey: where you can handle it as appropriate for that model.

Levi Brown

unread,
Nov 14, 2012, 4:44:39 PM11/14/12
to res...@googlegroups.com
Blake,

Thanks for the suggestion and reply.

I respectfully disagree, however. To me it seems like RestKit should at least gracefully handle this kind of situation, and not crash. For scalars it seems pretty easy to have a set of default behaviors (that could be augmented?)... for all numbers, null is zero. For Strings, null is null. For booleans, null is false.

I think of this crash a bit like an SQL injection vulnerability. The framework should protect against, and sanitize, malformed (intentionally or not) input.

Levi

Blake Watters

unread,
Dec 1, 2012, 4:01:20 PM12/1/12
to res...@googlegroups.com
Levi -

I appreciate the feedback. I have the tests laying around from investing this in a work copy and I will reevaluate.

Cheers,
Blake

Blake Watters

unread,
Dec 26, 2012, 2:47:35 PM12/26/12
to res...@googlegroups.com
Hi Levi -

I have expanded the support for mapping to primitive types and we will now coerce a null value into a numeric 0 value. Details are available on this commit: https://github.com/RestKit/RestKit/commit/b858f2753f5ec9d5c3f45b71151293b4ca7f9a88

Will be included with pre5 

Levi Brown

unread,
Dec 27, 2012, 12:35:12 PM12/27/12
to res...@googlegroups.com
Excellent. Thanks for taking the time and being responsive to feedback Blake.

Happy Holidays!
Reply all
Reply to author
Forward
0 new messages