File tree Expand file tree Collapse file tree
JSONModelDemoTests/UnitTests Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ];
Original file line number Diff line number Diff 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 ],
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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;
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments