mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-24 04:46:01 +08:00
Add assertions to prevent attempts to create request descriptor objects with inappropriate mappings. fixes #976
This commit is contained in:
@@ -22,6 +22,25 @@
|
||||
//
|
||||
|
||||
#import "RKRequestDescriptor.h"
|
||||
#import "RKObjectMapping.h"
|
||||
#import "RKDynamicMapping.h"
|
||||
|
||||
static void RKAssertValidMappingForRequestDescriptor(RKMapping *mapping)
|
||||
{
|
||||
if ([mapping isKindOfClass:[RKObjectMapping class]]) {
|
||||
if (! [[(RKObjectMapping *)mapping objectClass] isEqual:[NSMutableDictionary class]]) {
|
||||
[NSException raise:NSInvalidArgumentException format:@"`RKRequestDescriptor` objects must be initialized with a mapping whose target class is `NSMutableDictionary`, got '%@' (see `[RKObjectMapping requestMapping]`)", [(RKObjectMapping *)mapping objectClass]];
|
||||
}
|
||||
} else if ([mapping isKindOfClass:[RKDynamicMapping class]]) {
|
||||
[[(RKDynamicMapping *)mapping objectMappings] enumerateObjectsUsingBlock:^(RKObjectMapping *objectMapping, NSUInteger idx, BOOL *stop) {
|
||||
if (! [objectMapping.objectClass isEqual:[NSMutableDictionary class]]) {
|
||||
[NSException raise:NSInvalidArgumentException format:@"`RKRequestDescriptor` objects may only be initialized with `RKDynamicMapping` objects containing `RKObjectMapping` objects whose target class is `NSMutableDictionary`, got '%@' (see `[RKObjectMapping requestMapping]`)", objectMapping.objectClass];
|
||||
}
|
||||
}];
|
||||
} else {
|
||||
[NSException raise:NSInvalidArgumentException format:@"Expected an instance of `RKObjectMapping` or `RKDynamicMapping`, instead got '%@'", [mapping class]];
|
||||
}
|
||||
}
|
||||
|
||||
@interface RKRequestDescriptor ()
|
||||
|
||||
@@ -37,6 +56,7 @@
|
||||
{
|
||||
NSParameterAssert(mapping);
|
||||
NSParameterAssert(objectClass);
|
||||
RKAssertValidMappingForRequestDescriptor(mapping);
|
||||
|
||||
RKRequestDescriptor *requestDescriptor = [self new];
|
||||
requestDescriptor.mapping = mapping;
|
||||
|
||||
@@ -55,6 +55,13 @@ typedef RKObjectMapping *(^RKDynamicMappingDelegateBlock)(id);
|
||||
*/
|
||||
- (void)setObjectMapping:(RKObjectMapping *)objectMapping whenValueOfKeyPath:(NSString *)keyPath isEqualTo:(id)value;
|
||||
|
||||
/**
|
||||
Returns an array of object mappings that have been registered with the receiver.
|
||||
|
||||
@return An array of `RKObjectMapping` objects registered with the receiver.
|
||||
*/
|
||||
@property (nonatomic, readonly) NSArray *objectMappings;
|
||||
|
||||
///-----------------------------------------------------------------
|
||||
/// @name Retrieving the Object Mapping for an Object Representation
|
||||
///-----------------------------------------------------------------
|
||||
|
||||
@@ -43,6 +43,11 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSArray *)objectMappings
|
||||
{
|
||||
return [self.matchers valueForKey:@"objectMapping"];
|
||||
}
|
||||
|
||||
- (void)setObjectMapping:(RKObjectMapping *)objectMapping whenValueOfKeyPath:(NSString *)keyPath isEqualTo:(id)expectedValue
|
||||
{
|
||||
RKLogDebug(@"Adding dynamic object mapping for key '%@' with value '%@' to destination class: %@", keyPath, expectedValue, NSStringFromClass(objectMapping.objectClass));
|
||||
|
||||
Reference in New Issue
Block a user