New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throw an error if mappings don't match property keys #294
Changes from 6 commits
c4b4d9a
b164a87
6a411e8
2e291ff
4f7901b
b0b8d53
6400511
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
NSString * const MTLJSONAdapterErrorDomain = @"MTLJSONAdapterErrorDomain"; | ||
const NSInteger MTLJSONAdapterErrorNoClassFound = 2; | ||
const NSInteger MTLJSONAdapterErrorInvalidJSONDictionary = 3; | ||
const NSInteger MTLJSONAdapterErrorInvalidJSONMapping = 4; | ||
|
||
// An exception was thrown and caught. | ||
const NSInteger MTLJSONAdapterErrorExceptionThrown = 1; | ||
|
@@ -102,7 +103,24 @@ - (id)initWithJSONDictionary:(NSDictionary *)JSONDictionary modelClass:(Class)mo | |
|
||
NSMutableDictionary *dictionaryValue = [[NSMutableDictionary alloc] initWithCapacity:JSONDictionary.count]; | ||
|
||
for (NSString *propertyKey in [self.modelClass propertyKeys]) { | ||
NSSet *propertyKeys = [self.modelClass propertyKeys]; | ||
|
||
for (NSString *JSONKeyPath in self.JSONKeyPathsByPropertyKey.allKeys) { | ||
if ([propertyKeys containsObject:JSONKeyPath]) continue; | ||
|
||
if (error != NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, IMO It shouldn't throw error in case of umapped JSON Key. I've multiple case where server respond lot of field in JSON response and i don't want to get them all in my object, so no reason to throw an error. At least it should be configurable, one should be able to choose to be "strict" or not on mapping. More than that, the pull requested merged seems to have error in it, as it silently fails. Seems due to line 150 in "antle/MTLJSONAdapter.h" where code is Should be As there is no current error, it show nothing. Thanks in advance for fixing the issue. Regards, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This only affects the property mapping in your model, so if you say map the property The Also check out #325 where we're changing this to an exception. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, And about the error, let read the block 150 - 159 Thx On Tue, May 13, 2014 at 5:05 PM, Robert Böhnke notifications@github.comwrote:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Odd, do you have a test case you could put into a gist? The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok thx for the error parameter, didn't get that point, thx for your I'll try to make a test case. Thx for you help On Tue, May 13, 2014 at 5:59 PM, Robert Böhnke notifications@github.comwrote:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok founded why error didn't get there. return [MTLJSONAdapter modelOfClass:modelClass it's hardcoded to null. On Tue, May 13, 2014 at 6:12 PM, Vincent YSMAL vincent.ysmal@gmail.comwrote:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok after deeper look, you were right, sorry for this misunderstanding. Anyway there is an issue due to error hardcoded to null and avoid having NSValueTransformer+MTLPredefinedTransformerAdditions.m return [MTLJSONAdapter modelOfClass:modelClass On Tue, May 13, 2014 at 6:16 PM, Vincent YSMAL vincent.ysmal@gmail.comwrote:
|
||
NSDictionary *userInfo = @{ | ||
NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid JSON mapping", nil), | ||
NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"%1$@ could not be parsed because its JSON mapping contains illegal property keys.", nil), modelClass] | ||
}; | ||
|
||
*error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONMapping userInfo:userInfo]; | ||
} | ||
|
||
return nil; | ||
} | ||
|
||
for (NSString *propertyKey in propertyKeys) { | ||
NSString *JSONKeyPath = [self JSONKeyPathForPropertyKey:propertyKey]; | ||
if (JSONKeyPath == nil) continue; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
const NSInteger MTLManagedObjectAdapterErrorUnsupportedManagedObjectPropertyType = 5; | ||
const NSInteger MTLManagedObjectAdapterErrorUnsupportedRelationshipClass = 6; | ||
const NSInteger MTLManagedObjectAdapterErrorUniqueFetchRequestFailed = 7; | ||
const NSInteger MTLManagedObjectAdapterErrorInvalidManagedObjectMapping = 8; | ||
|
||
// Performs the given block in the context's queue, if it has one. | ||
static id performInContext(NSManagedObjectContext *context, id (^block)(void)) { | ||
|
@@ -250,6 +251,23 @@ - (id)modelFromManagedObject:(NSManagedObject *)managedObject processedObjects:( | |
} | ||
|
||
+ (id)modelOfClass:(Class)modelClass fromManagedObject:(NSManagedObject *)managedObject error:(NSError **)error { | ||
NSSet *propertyKeys = [modelClass propertyKeys]; | ||
|
||
for (NSString *mappedPropertyKey in [modelClass managedObjectKeysByPropertyKey].allKeys) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above. |
||
if ([propertyKeys containsObject:mappedPropertyKey]) continue; | ||
|
||
if (error != NULL) { | ||
NSDictionary *userInfo = @{ | ||
NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid entity attribute mapping", nil), | ||
NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"%1$@ could not be parsed because its entity attribute mapping contains illegal property keys.", nil), modelClass] | ||
}; | ||
|
||
*error = [NSError errorWithDomain:MTLManagedObjectAdapterErrorDomain code:MTLManagedObjectAdapterErrorInvalidManagedObjectMapping userInfo:userInfo]; | ||
} | ||
|
||
return nil; | ||
} | ||
|
||
CFMutableDictionaryRef processedObjects = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); | ||
if (processedObjects == NULL) return nil; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
allKeys
is expensive and redundant here. Dictionaries are already fast enumerated by key.