From afea33be7ff87fbf163b3db61727ca9941ee3149 Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Thu, 26 Dec 2019 23:14:37 +0300 Subject: [PATCH] Validate JSON schema for application/json responses only --- Framework/Framework/URLTransaction.m | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Framework/Framework/URLTransaction.m b/Framework/Framework/URLTransaction.m index 7685c3d..b7895da 100644 --- a/Framework/Framework/URLTransaction.m +++ b/Framework/Framework/URLTransaction.m @@ -459,6 +459,12 @@ - (void)resume { return; } + NSString *contentType = [request.response.allHeaderFields valueForKey:HTTPHeaderFieldContentType]; + if (![contentType isEqualToString:MediaTypeApplicationJSON]) { + dispatch_group_leave(group); + return; + } + JSONSchema *schema = request.JSONSchemas[@(statusCode)]; if (!schema) { dispatch_group_leave(group); @@ -468,8 +474,11 @@ - (void)resume { NSOperationQueue *queue = [NSOperationQueue new]; [queue addOperationWithBlock:^{ NSError *error = nil; - BOOL valid = [schema validateObject:request.data.json error:&error]; - if (!valid) { + id json = [NSJSONSerialization JSONObjectWithData:request.data options:0 error:&error]; + if (!error) { + [schema validateObject:json error:&error]; + } + if (error) { request.error = error; }