Skip to content

Commit be2f1e7

Browse files
committed
* fixed handling of nulls in JSON
* jsonmodel#101
1 parent 9f9559b commit be2f1e7

7 files changed

Lines changed: 38 additions & 6 deletions

File tree

‎JSONModel/JSONModel/JSONModel.m‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,16 @@ -(id)initWithDictionary:(NSDictionary*)dict error:(NSError**)err
221221
id jsonValue = [dict valueForKeyPath: jsonKeyPath];
222222

223223
//check for Optional properties
224-
if (isNull(jsonValue) && property.isOptional==YES) {
224+
if (isNull(jsonValue)) {
225225
//skip this property, continue with next property
226-
continue;
226+
if (property.isOptional==YES) continue;
227+
228+
//null value for required property
229+
NSString* msg = [NSString stringWithFormat:@"Value of required model key %@ is null", property.name];
230+
JSONModelError* dataErr = [JSONModelError errorInvalidDataWithMessage:msg];
231+
*err = [dataErr errorByPrependingKeyPathComponent:property.name];
232+
233+
return nil;
227234
}
228235

229236
Class jsonValueClass = [jsonValue class];

‎JSONModel/JSONModelNetworking/JSONHTTPClient.m‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ +(NSString*)contentTypeForRequestString:(NSString*)requestString
9999
//fetch the charset name from the default string encoding
100100
NSString* contentType = requestContentType;
101101

102-
if ([contentType isEqualToString:kContentTypeAutomatic]) {
102+
if (requestString.length>0 && [contentType isEqualToString:kContentTypeAutomatic]) {
103103
//check for "eventual" JSON array or dictionary
104104
NSString* firstAndLastChar = [NSString stringWithFormat:@"%@%@",
105105
[requestString substringToIndex:1],

‎JSONModelDemoTests/UnitTests/OptionalPropertiesTests.m‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,17 @@ -(void)testPropertyMissing
4545

4646
}
4747

48+
-(void)testNullValuesForOptionalProperties
49+
{
50+
NSString* jsonWithNulls = @"{\"notRequredProperty\":null,\"fillerNumber\":1}";
51+
52+
NSError* err;
53+
o = [[OptionalPropModel alloc] initWithString: jsonWithNulls error:&err];
54+
STAssertNil(err, [err localizedDescription]);
55+
STAssertNotNil(o, @"Could not initialize the model");
56+
57+
STAssertTrue(!o.notRequredProperty, @"notRequredProperty' is not nil");
58+
59+
}
60+
4861
@end

‎JSONModelDemoTests/UnitTests/SimpleDataErrorTests.m‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,16 @@ -(void)testForNilInputFromDictionary
148148
STAssertTrue(err.code == kJSONModelErrorNilInput, @"Wrong error for nil dictionary input");
149149
}
150150

151+
-(void)testForNullValuesForRequiredProperty
152+
{
153+
JSONModelError* err = nil;
154+
NSString* jsonString = @"{\"author\":\"Marin\",\"year\":null}";
155+
156+
CopyrightModel* cpModel = [[CopyrightModel alloc] initWithString:jsonString
157+
error:&err];
158+
cpModel = nil;
159+
STAssertTrue(err, @"No error returned when initialized with nil dictionary");
160+
STAssertTrue(err.code == kJSONModelErrorInvalidData, @"Wrong error null value for a required property");
161+
}
162+
151163
@end

‎JSONModelDemoTests/UnitTests/TestModels/GitHubKeyMapRepoModel.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@interface GitHubKeyMapRepoModel : JSONModel
1212

1313
@property (strong, nonatomic) NSString* __description;
14-
@property (strong, nonatomic) NSString* language;
14+
@property (strong, nonatomic) NSString<Optional>* language;
1515
@property (assign, nonatomic) NSString<Index>* name;
1616

1717
@end

‎JSONModelDemoTests/UnitTests/TestModels/GitHubRepoModel.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@property (strong, nonatomic) NSString* owner;
2020
@property (assign, nonatomic) int forks;
2121
@property (strong, nonatomic) NSString* description;
22-
@property (strong, nonatomic) NSString* language;
22+
@property (strong, nonatomic) NSString<Optional>* language;
2323
@property (assign, nonatomic) BOOL fork;
2424
@property (assign, nonatomic) double size;
2525
@property (assign, nonatomic) int followers;

‎JSONModelDemoTests/UnitTests/TestModels/JSONTypesModel.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@
3535
@property (strong, nonatomic) NSMutableDictionary* dynamicDictionary;
3636

3737
/* null */
38-
@property (strong, nonatomic) NSString* notAvailable;
38+
@property (strong, nonatomic) NSString<Optional>* notAvailable;
3939

4040
@end

0 commit comments

Comments
 (0)