Skip to content

Commit 8079ee8

Browse files
committed
Merge pull request #98 from NUBIC/rg_deserialization
De-serialization fails when response group is null
2 parents 5369221 + f4adc36 commit 8079ee8

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

NUSurveyor/Models/NUResponseSet.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ - (void) fromJson:(NSString *)jsonString {
421421
[newResponse setValue:self forKey:@"responseSet"];
422422
[newResponse setValue:[response objectForKey:@"question_id"] forKey:@"question"];
423423
[newResponse setValue:[response objectForKey:@"answer_id"] forKey:@"answer"];
424-
NSString* responseGroupRaw = [response objectForKey:@"response_group"];
425-
NSNumber* responseGroup = responseGroupRaw ? [NSNumber numberWithInteger:[responseGroupRaw integerValue]] : NULL;
424+
id responseGroupRaw = [response objectForKey:@"response_group"];
425+
NSNumber* responseGroup = responseGroupRaw && ([NSNull null] != responseGroupRaw) ? [NSNumber numberWithInteger:[responseGroupRaw integerValue]] : NULL;
426426
[newResponse setValue:responseGroup forKey:@"responseGroup"];
427427
NSString* value = [response objectForKey:@"value"] == [NSNull null] ? nil : [response objectForKey:@"value"];
428428
[newResponse setValue:[value description] forKey:@"value"];

NUSurveyorTests/NUResponseSetTest.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ - (void) testFromJsonWithResponseGroup {
117117
STAssertEqualObjects([[[[rs valueForKey:@"responses"] objectEnumerator] nextObject] valueForKey:@"responseGroup"], [NSNumber numberWithInt:1], @"Should be 1");
118118
}
119119

120+
- (void)testFromJsonWithNullResponseGroup {
121+
NSString* jsonString = @"{\"uuid\":\"9af6d142-7fac-4ccb-9bca-58a05308a5a7\",\"survey_id\":\"94b3d750-fb63-4540-a1e2-dd7f88be9b4f\",\"created_at\":\"1970-02-04T05:15:30Z\",\"completed_at\":\"1990-03-06T07:21:42Z\",\"responses\":[{\"uuid\":\"07d72796-ebb2-4be2-91b9-68f5a30a0054\",\"answer_id\":\"9c788711-8373-44d7-b44b-754c31e596a9\",\"question_id\":\"376a501b-c32f-49de-b4d7-e28030a2ea94\",\"response_group\":null,\"created_at\":\"1970-02-04T05:15:30Z\",\"modified_at\":\"1990-03-06T07:21:42Z\"}]}";
122+
123+
NSEntityDescription *entity = [[self.model entitiesByName] objectForKey:@"ResponseSet"];
124+
NUResponseSet *rs = [[NUResponseSet alloc] initWithEntity:entity insertIntoManagedObjectContext:self.ctx];
125+
[rs fromJson:jsonString];
126+
127+
STAssertNil([[[[rs valueForKey:@"responses"] objectEnumerator] nextObject] valueForKey:@"responseGroup"], nil);
128+
}
129+
120130
- (void) testFromJsonWithNullCompletedAt {
121131
NSString* jsonString = @"{\"uuid\":\"9af6d142-7fac-4ccb-9bca-58a05308a5a7\",\"survey_id\":\"94b3d750-fb63-4540-a1e2-dd7f88be9b4f\",\"created_at\":\"1970-02-04T05:15:30Z\",\"completed_at\":null,\"responses\":[]}";
122132

0 commit comments

Comments
 (0)