From 7357332a28a5b496be34f11809227b73ab05e615 Mon Sep 17 00:00:00 2001 From: Chase Coalwell Date: Thu, 13 Jun 2019 08:54:18 -0700 Subject: [PATCH] fix json parse error on empty headers (#1819) --- .../changelog_fix_json_parse_header.json | 7 +++++ src/Api/Parser/AbstractRestParser.php | 17 +++++++++-- .../protocols/output/rest-json.json | 30 +++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 .changes/nextrelease/changelog_fix_json_parse_header.json diff --git a/.changes/nextrelease/changelog_fix_json_parse_header.json b/.changes/nextrelease/changelog_fix_json_parse_header.json new file mode 100644 index 0000000000..65a55fbb2b --- /dev/null +++ b/.changes/nextrelease/changelog_fix_json_parse_header.json @@ -0,0 +1,7 @@ +[ + { + "type": "enhancement", + "category": "Api", + "description": "Fix json parse error when extracting header" + } + ] \ No newline at end of file diff --git a/src/Api/Parser/AbstractRestParser.php b/src/Api/Parser/AbstractRestParser.php index 1e7fa94b1f..c264767f16 100644 --- a/src/Api/Parser/AbstractRestParser.php +++ b/src/Api/Parser/AbstractRestParser.php @@ -128,10 +128,21 @@ private function extractHeader( return; } case 'string': - if ($shape['jsonvalue']) { - $value = $this->parseJson(base64_decode($value), $response); + try { + if ($shape['jsonvalue']) { + $value = $this->parseJson(base64_decode($value), $response); + } + + // If value is not set, do not add to output structure. + if (!isset($value)) { + return; + } + break; + } catch (\Exception $e) { + //If the value cannot be parsed, then do not add it to the + //output structure. + return; } - break; } $result[$name] = $value; diff --git a/tests/Api/test_cases/protocols/output/rest-json.json b/tests/Api/test_cases/protocols/output/rest-json.json index 60095401ec..180328f16e 100644 --- a/tests/Api/test_cases/protocols/output/rest-json.json +++ b/tests/Api/test_cases/protocols/output/rest-json.json @@ -702,6 +702,36 @@ "headers": {"X-Amz-Foo": "eyJGb28iOiJCYXIifQ=="}, "body": "" } + }, + { + "given": { + "output": { + "shape": "OutputShape" + }, + "name": "OperationName" + }, + "result": { + "Attr": {} + }, + "response": { + "status_code": 200, + "headers": {"X-Amz-Foo": "e30="}, + "body": "" + } + }, + { + "given": { + "output": { + "shape": "OutputShape" + }, + "name": "OperationName" + }, + "result": {}, + "response": { + "status_code": 200, + "headers": {"X-Amz-Foo": ""}, + "body": "" + } } ] }