Skip to content

Commit ae6b58d

Browse files
committed
Add support for parameterizing NSSet. refs RestKit#1010
1 parent b9457ce commit ae6b58d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Code/Network/RKObjectParameterization.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ - (void)mappingOperation:(RKMappingOperation *)operation didSetValue:(id)value f
111111
} else if ([value isKindOfClass:[NSDecimalNumber class]]) {
112112
// Precision numbers are serialized as strings to work around Javascript notation limits
113113
transformedValue = [(NSDecimalNumber *)value stringValue];
114+
} else if ([value isKindOfClass:[NSSet class]]) {
115+
// NSSets are not natively serializable, so let's just turn it into an NSArray
116+
transformedValue = [value allObjects];
114117
} else if ([value isKindOfClass:[NSOrderedSet class]]) {
115118
// NSOrderedSets are not natively serializable, so let's just turn it into an NSArray
116119
transformedValue = [value array];

Tests/Logic/ObjectMapping/RKObjectParameterizationTest.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,25 @@ - (void)testShouldSerializeANSOrderedSetToJSON
319319
expect(string).to.equal(@"{\"key1-form-name\":\"value1\",\"set-form-name\":[\"setElementOne\",\"setElementTwo\",\"setElementThree\"]}");
320320
}
321321

322+
- (void)testShouldSerializeAnNSSetToJSON
323+
{
324+
NSDictionary *object = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1",
325+
[NSSet setWithObjects:@"setElementOne", nil], @"set",
326+
nil];
327+
RKObjectMapping *mapping = [RKObjectMapping requestMapping];
328+
[mapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"key1" toKeyPath:@"key1-form-name"]];
329+
[mapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"set" toKeyPath:@"set-form-name"]];
330+
331+
NSError *error = nil;
332+
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping objectClass:[NSDictionary class] rootKeyPath:nil];
333+
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:object requestDescriptor:requestDescriptor error:&error];
334+
NSData *data = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];
335+
NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
336+
337+
expect(error).to.beNil();
338+
expect(string).to.equal(@"{\"key1-form-name\":\"value1\",\"set-form-name\":[\"setElementOne\"]}");
339+
}
340+
322341
- (void)testParameterizationOfAttributesNestedByKeyPath
323342
{
324343
NSDictionary *object = @{ @"name" : @"Blake Watters", @"occupation" : @"Hacker" };

0 commit comments

Comments
 (0)