Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit be2f1e7

Browse filesBrowse files
committed
* fixed handling of nulls in JSON
* jsonmodel#101
1 parent 9f9559b commit be2f1e7
Copy full SHA for be2f1e7

File tree

Expand file treeCollapse file tree

7 files changed

+38
-6
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

7 files changed

+38
-6
lines changed
Open diff view settings
Collapse file

‎JSONModel/JSONModel/JSONModel.m‎

Copy file name to clipboardExpand all lines: JSONModel/JSONModel/JSONModel.m
+9-2Lines 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];
Collapse file

‎JSONModel/JSONModelNetworking/JSONHTTPClient.m‎

Copy file name to clipboardExpand all lines: JSONModel/JSONModelNetworking/JSONHTTPClient.m
+1-1Lines 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],
Collapse file

‎JSONModelDemoTests/UnitTests/OptionalPropertiesTests.m‎

Copy file name to clipboardExpand all lines: JSONModelDemoTests/UnitTests/OptionalPropertiesTests.m
+13Lines 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
Collapse file

‎JSONModelDemoTests/UnitTests/SimpleDataErrorTests.m‎

Copy file name to clipboardExpand all lines: JSONModelDemoTests/UnitTests/SimpleDataErrorTests.m
+12Lines 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
Collapse file

‎JSONModelDemoTests/UnitTests/TestModels/GitHubKeyMapRepoModel.h‎

Copy file name to clipboardExpand all lines: JSONModelDemoTests/UnitTests/TestModels/GitHubKeyMapRepoModel.h
+1-1Lines 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
Collapse file

‎JSONModelDemoTests/UnitTests/TestModels/GitHubRepoModel.h‎

Copy file name to clipboardExpand all lines: JSONModelDemoTests/UnitTests/TestModels/GitHubRepoModel.h
+1-1Lines 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;
Collapse file

‎JSONModelDemoTests/UnitTests/TestModels/JSONTypesModel.h‎

Copy file name to clipboardExpand all lines: JSONModelDemoTests/UnitTests/TestModels/JSONTypesModel.h
+1-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.