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": "" + } } ] }