From 9e7cbce6d3c3f082f3c4eb44090f211fda961027 Mon Sep 17 00:00:00 2001 From: David Cordero Date: Thu, 19 Sep 2013 20:54:30 +0200 Subject: [PATCH 1/7] Added Error when the JSONDictionary received is nil --- Mantle/MTLJSONAdapter.h | 4 ++++ Mantle/MTLJSONAdapter.m | 13 ++++++++++++- MantleTests/MTLJSONAdapterSpec.m | 17 ++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Mantle/MTLJSONAdapter.h b/Mantle/MTLJSONAdapter.h index 64bf9359..47d25f38 100644 --- a/Mantle/MTLJSONAdapter.h +++ b/Mantle/MTLJSONAdapter.h @@ -57,9 +57,13 @@ // The domain for errors originating from MTLJSONAdapter. extern NSString * const MTLJSONAdapterErrorDomain; + // +classForParsingJSONDictionary: returned nil for the given dictionary. extern const NSInteger MTLJSONAdapterErrorNoClassFound; +// Error returned when the JSONDictionary received is not valid +extern const NSInteger MTLJSONAdapterErrorInvalidJSONDictionary; + // Converts a MTLModel object to and from a JSON dictionary. @interface MTLJSONAdapter : NSObject diff --git a/Mantle/MTLJSONAdapter.m b/Mantle/MTLJSONAdapter.m index 799f88af..9676e137 100644 --- a/Mantle/MTLJSONAdapter.m +++ b/Mantle/MTLJSONAdapter.m @@ -12,6 +12,7 @@ NSString * const MTLJSONAdapterErrorDomain = @"MTLJSONAdapterErrorDomain"; const NSInteger MTLJSONAdapterErrorNoClassFound = 2; +const NSInteger MTLJSONAdapterErrorInvalidJSONDictionary = 3; // An exception was thrown and caught. static const NSInteger MTLJSONAdapterErrorExceptionThrown = 1; @@ -71,11 +72,21 @@ - (id)initWithJSONDictionary:(NSDictionary *)JSONDictionary modelClass:(Class)mo NSParameterAssert([modelClass isSubclassOfClass:MTLModel.class]); NSParameterAssert([modelClass conformsToProtocol:@protocol(MTLJSONSerializing)]); - if (JSONDictionary == nil) return nil; + + if (JSONDictionary == nil) { + NSDictionary *userInfo = @{ + NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid JSONDictionary", @""), + NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"The JSONDictionary parameter is invalid.", @"") + }; + *error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONDictionary + userInfo:userInfo]; + return nil; + } if ([modelClass respondsToSelector:@selector(classForParsingJSONDictionary:)]) { modelClass = [modelClass classForParsingJSONDictionary:JSONDictionary]; if (modelClass == nil) { + if (error != NULL) { NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: NSLocalizedString(@"Could not parse JSON", @""), diff --git a/MantleTests/MTLJSONAdapterSpec.m b/MantleTests/MTLJSONAdapterSpec.m index 9284a0a4..2fb51fb3 100644 --- a/MantleTests/MTLJSONAdapterSpec.m +++ b/MantleTests/MTLJSONAdapterSpec.m @@ -77,7 +77,9 @@ NSError *error = nil; MTLJSONAdapter *adapter = [[MTLJSONAdapter alloc] initWithJSONDictionary:nil modelClass:MTLTestModel.class error:&error]; expect(adapter).to.beNil(); - expect(error).to.beNil(); + expect(error).notTo.beNil(); + expect(error.domain).to.equal(MTLJSONAdapterErrorDomain); + expect(error.code).to.equal(MTLJSONAdapterErrorInvalidJSONDictionary); }); it(@"should ignore unrecognized JSON keys", ^{ @@ -140,4 +142,17 @@ expect(error.code).to.equal(MTLJSONAdapterErrorNoClassFound); }); +it(@"should fail to initialize if JSON dictionary is nil", ^{ + NSError *error = nil; + MTLTestModel *model = [MTLJSONAdapter modelOfClass:MTLSubstitutingTestModel.class fromJSONDictionary:nil error:&error]; + expect(model).to.beNil(); + + expect(error).notTo.beNil(); + expect(error.domain).to.equal(MTLJSONAdapterErrorDomain); + expect(error.code).to.equal(MTLJSONAdapterErrorInvalidJSONDictionary); +}); + SpecEnd + + + From 59969fd28000398e18ca878671ed74f840c2e02e Mon Sep 17 00:00:00 2001 From: David Cordero Date: Fri, 20 Sep 2013 00:20:40 +0200 Subject: [PATCH 2/7] Check if error if not NULL before using it and fix a comment --- Mantle/MTLJSONAdapter.h | 3 +-- Mantle/MTLJSONAdapter.m | 15 ++++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Mantle/MTLJSONAdapter.h b/Mantle/MTLJSONAdapter.h index 47d25f38..a3ba7711 100644 --- a/Mantle/MTLJSONAdapter.h +++ b/Mantle/MTLJSONAdapter.h @@ -57,11 +57,10 @@ // The domain for errors originating from MTLJSONAdapter. extern NSString * const MTLJSONAdapterErrorDomain; - // +classForParsingJSONDictionary: returned nil for the given dictionary. extern const NSInteger MTLJSONAdapterErrorNoClassFound; -// Error returned when the JSONDictionary received is not valid +// The provided JSONDictionary is not valid. extern const NSInteger MTLJSONAdapterErrorInvalidJSONDictionary; // Converts a MTLModel object to and from a JSON dictionary. diff --git a/Mantle/MTLJSONAdapter.m b/Mantle/MTLJSONAdapter.m index 9676e137..5d7748f0 100644 --- a/Mantle/MTLJSONAdapter.m +++ b/Mantle/MTLJSONAdapter.m @@ -74,19 +74,20 @@ - (id)initWithJSONDictionary:(NSDictionary *)JSONDictionary modelClass:(Class)mo if (JSONDictionary == nil) { - NSDictionary *userInfo = @{ - NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid JSONDictionary", @""), - NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"The JSONDictionary parameter is invalid.", @"") - }; - *error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONDictionary - userInfo:userInfo]; + if (error != NULL) { + NSDictionary *userInfo = @{ + NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid JSONDictionary", @""), + NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"The JSONDictionary parameter is invalid.", @"") + }; + *error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONDictionary + userInfo:userInfo]; + } return nil; } if ([modelClass respondsToSelector:@selector(classForParsingJSONDictionary:)]) { modelClass = [modelClass classForParsingJSONDictionary:JSONDictionary]; if (modelClass == nil) { - if (error != NULL) { NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: NSLocalizedString(@"Could not parse JSON", @""), From 10e0de526cf71e9ec7c94db6c4083e0004753b5d Mon Sep 17 00:00:00 2001 From: David Cordero Date: Fri, 20 Sep 2013 13:22:11 +0200 Subject: [PATCH 3/7] Update the description of a test to the new behaviour --- MantleTests/MTLJSONAdapterSpec.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MantleTests/MTLJSONAdapterSpec.m b/MantleTests/MTLJSONAdapterSpec.m index 2fb51fb3..e5a9677e 100644 --- a/MantleTests/MTLJSONAdapterSpec.m +++ b/MantleTests/MTLJSONAdapterSpec.m @@ -73,7 +73,7 @@ expect([MTLJSONAdapter JSONDictionaryFromModel:model]).to.equal(values); }); -it(@"should return nil with a nil JSON dictionary, but no error", ^{ +it(@"should return nil with a nil JSON dictionary, and invalid json error", ^{ NSError *error = nil; MTLJSONAdapter *adapter = [[MTLJSONAdapter alloc] initWithJSONDictionary:nil modelClass:MTLTestModel.class error:&error]; expect(adapter).to.beNil(); From 135a67fe101b567a311f9244cb078e3b9c5fc93c Mon Sep 17 00:00:00 2001 From: David Cordero Date: Mon, 23 Sep 2013 17:08:22 +0200 Subject: [PATCH 4/7] Added the new behaviour to the documentation --- Mantle/MTLJSONAdapter.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Mantle/MTLJSONAdapter.h b/Mantle/MTLJSONAdapter.h index a3ba7711..847fb10b 100644 --- a/Mantle/MTLJSONAdapter.h +++ b/Mantle/MTLJSONAdapter.h @@ -98,7 +98,8 @@ extern const NSInteger MTLJSONAdapterErrorInvalidJSONDictionary; // // JSONDictionary - A dictionary representing JSON data. This should match the // format returned by NSJSONSerialization. If this argument is -// nil, the method returns nil. +// nil, the method returns nil and set error to +// MTLJSONAdapterErrorInvalidJSONDictionary. // modelClass - The MTLModel subclass to attempt to parse from the JSON. // This class must conform to . This // argument must not be nil. From a33a4b422df7a3d4c2610dc31bd4bd2095eb36f1 Mon Sep 17 00:00:00 2001 From: David Cordero Date: Tue, 24 Sep 2013 23:10:14 +0200 Subject: [PATCH 5/7] Minor fixes in syntax and descriptions --- Mantle/MTLJSONAdapter.h | 3 +-- Mantle/MTLJSONAdapter.m | 3 +-- MantleTests/MTLJSONAdapterSpec.m | 5 +---- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/Mantle/MTLJSONAdapter.h b/Mantle/MTLJSONAdapter.h index 847fb10b..fc62329c 100644 --- a/Mantle/MTLJSONAdapter.h +++ b/Mantle/MTLJSONAdapter.h @@ -98,8 +98,7 @@ extern const NSInteger MTLJSONAdapterErrorInvalidJSONDictionary; // // JSONDictionary - A dictionary representing JSON data. This should match the // format returned by NSJSONSerialization. If this argument is -// nil, the method returns nil and set error to -// MTLJSONAdapterErrorInvalidJSONDictionary. +// nil, the method returns nil and an error with code MTLJSONAdapterErrorInvalidJSONDictionary. // modelClass - The MTLModel subclass to attempt to parse from the JSON. // This class must conform to . This // argument must not be nil. diff --git a/Mantle/MTLJSONAdapter.m b/Mantle/MTLJSONAdapter.m index 5d7748f0..26df55fc 100644 --- a/Mantle/MTLJSONAdapter.m +++ b/Mantle/MTLJSONAdapter.m @@ -79,8 +79,7 @@ - (id)initWithJSONDictionary:(NSDictionary *)JSONDictionary modelClass:(Class)mo NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid JSONDictionary", @""), NSLocalizedFailureReasonErrorKey: NSLocalizedString(@"The JSONDictionary parameter is invalid.", @"") }; - *error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONDictionary - userInfo:userInfo]; + *error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONDictionary userInfo:userInfo]; } return nil; } diff --git a/MantleTests/MTLJSONAdapterSpec.m b/MantleTests/MTLJSONAdapterSpec.m index e5a9677e..c4959397 100644 --- a/MantleTests/MTLJSONAdapterSpec.m +++ b/MantleTests/MTLJSONAdapterSpec.m @@ -73,7 +73,7 @@ expect([MTLJSONAdapter JSONDictionaryFromModel:model]).to.equal(values); }); -it(@"should return nil with a nil JSON dictionary, and invalid json error", ^{ +it(@"should return nil and an error with a nil JSON dictionary", ^{ NSError *error = nil; MTLJSONAdapter *adapter = [[MTLJSONAdapter alloc] initWithJSONDictionary:nil modelClass:MTLTestModel.class error:&error]; expect(adapter).to.beNil(); @@ -153,6 +153,3 @@ }); SpecEnd - - - From 663512c6e52158174cde4a86a5e319a50e53f7fb Mon Sep 17 00:00:00 2001 From: Keith Duncan Date: Thu, 10 Oct 2013 19:40:11 +0100 Subject: [PATCH 6/7] Remove duplicate test --- MantleTests/MTLJSONAdapterSpec.m | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/MantleTests/MTLJSONAdapterSpec.m b/MantleTests/MTLJSONAdapterSpec.m index c4959397..99ce58e3 100644 --- a/MantleTests/MTLJSONAdapterSpec.m +++ b/MantleTests/MTLJSONAdapterSpec.m @@ -142,14 +142,4 @@ expect(error.code).to.equal(MTLJSONAdapterErrorNoClassFound); }); -it(@"should fail to initialize if JSON dictionary is nil", ^{ - NSError *error = nil; - MTLTestModel *model = [MTLJSONAdapter modelOfClass:MTLSubstitutingTestModel.class fromJSONDictionary:nil error:&error]; - expect(model).to.beNil(); - - expect(error).notTo.beNil(); - expect(error.domain).to.equal(MTLJSONAdapterErrorDomain); - expect(error.code).to.equal(MTLJSONAdapterErrorInvalidJSONDictionary); -}); - SpecEnd From c82064b9a1f64a28a1b4a9872993f76e61cb19f2 Mon Sep 17 00:00:00 2001 From: Keith Duncan Date: Thu, 10 Oct 2013 19:42:03 +0100 Subject: [PATCH 7/7] Wrap documentation at 80 characters --- Mantle/MTLJSONAdapter.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Mantle/MTLJSONAdapter.h b/Mantle/MTLJSONAdapter.h index fc62329c..3a3f4219 100644 --- a/Mantle/MTLJSONAdapter.h +++ b/Mantle/MTLJSONAdapter.h @@ -98,7 +98,8 @@ extern const NSInteger MTLJSONAdapterErrorInvalidJSONDictionary; // // JSONDictionary - A dictionary representing JSON data. This should match the // format returned by NSJSONSerialization. If this argument is -// nil, the method returns nil and an error with code MTLJSONAdapterErrorInvalidJSONDictionary. +// nil, the method returns nil and an error with code +// MTLJSONAdapterErrorInvalidJSONDictionary. // modelClass - The MTLModel subclass to attempt to parse from the JSON. // This class must conform to . This // argument must not be nil.