mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-24 04:46:01 +08:00
Fix crash when attempting to union a relationship with a nil value. fixes #1195
This commit is contained in:
@@ -651,7 +651,7 @@ static BOOL RKIsMetadataKVCInvocation(NSInvocation *invocation)
|
||||
|
||||
if (relationshipMapping.assignmentPolicy == RKUnionAssignmentPolicy) {
|
||||
RKLogDebug(@"Mapping relationship with union assignment policy: constructing combined relationship value.");
|
||||
id existingObjects = [self.destinationObject valueForKeyPath:relationshipMapping.destinationKeyPath];
|
||||
id existingObjects = [self.destinationObject valueForKeyPath:relationshipMapping.destinationKeyPath] ?: @[];
|
||||
NSArray *existingObjectsArray = RKTransformedValueWithClass(existingObjects, [NSArray class], nil);
|
||||
[relationshipCollection addObjectsFromArray:existingObjectsArray];
|
||||
}
|
||||
|
||||
@@ -1912,6 +1912,28 @@
|
||||
assertThat(names, hasItems(@"Jeff", @"Zach", nil));
|
||||
}
|
||||
|
||||
- (void)testUnionAssignmentPolicyWithNilValue
|
||||
{
|
||||
RKTestUser *user = [RKTestUser new];
|
||||
user.friends = nil;
|
||||
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[RKTestUser class]];
|
||||
[mapping addAttributeMappingsFromArray:@[ @"name" ]];
|
||||
RKRelationshipMapping *relationshipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"friends" toKeyPath:@"friends" withMapping:mapping];
|
||||
relationshipMapping.assignmentPolicy = RKUnionAssignmentPolicy;
|
||||
[mapping addPropertyMapping:relationshipMapping];
|
||||
|
||||
NSDictionary *dictionary = @{ @"friends": @[ @{ @"name": @"Zach" } ] };
|
||||
RKMappingOperation *operation = [[RKMappingOperation alloc] initWithSourceObject:dictionary destinationObject:user mapping:mapping];
|
||||
RKObjectMappingOperationDataSource *dataSource = [RKObjectMappingOperationDataSource new];
|
||||
operation.dataSource = dataSource;
|
||||
|
||||
NSError *error = nil;
|
||||
[operation performMapping:&error];
|
||||
expect([user.friends count]).to.equal(1);
|
||||
NSArray *names = [user.friends valueForKey:@"name"];
|
||||
assertThat(names, hasItems(@"Zach", nil));
|
||||
}
|
||||
|
||||
- (void)testReplacementPolicyForUnmanagedRelationship
|
||||
{
|
||||
RKTestUser *user = [RKTestUser new];
|
||||
|
||||
Reference in New Issue
Block a user