| @@ -15,9 +15,9 @@ | ||
| 15 | 15 | /** |
| 16 | 16 | * @param Service $api Service description |
| 17 | 17 | * @param JsonParser $parser JSON body builder |
| 18 | 18 | */ |
| 19 | - public function __construct(Service $api, JsonParser $parser = null) | |
| 19 | + public function __construct(Service $api, ?JsonParser $parser = null) | |
| 20 | 20 | { |
| 21 | 21 | parent::__construct($api); |
| 22 | 22 | $this->parser = $parser ?: new JsonParser(); |
| 23 | 23 | } |
| @@ -22,11 +22,25 @@ | ||
| 22 | 22 | $this->parser = $parser ?: new JsonParser(); |
| 23 | 23 | } |
| 24 | 24 | protected function payload(ResponseInterface $response, StructureShape $member, array &$result) |
| 25 | 25 | { |
| 26 | - $jsonBody = $this->parseJson($response->getBody(), $response); | |
| 27 | - if ($jsonBody) { | |
| 28 | - $result += $this->parser->parse($member, $jsonBody); | |
| 26 | + $responseBody = (string) $response->getBody(); | |
| 27 | + // Parse JSON if we have content | |
| 28 | + $parsedJson = null; | |
| 29 | + if (!empty($responseBody)) { | |
| 30 | + $parsedJson = $this->parseJson($responseBody, $response); | |
| 31 | + } else { | |
| 32 | + // An empty response body should be deserialized as null | |
| 33 | + $result = $parsedJson; | |
| 34 | + return; | |
| 35 | + } | |
| 36 | + $parsedBody = $this->parser->parse($member, $parsedJson); | |
| 37 | + if (\is_string($parsedBody) && $member['document']) { | |
| 38 | + // Document types can be strings: replace entire result | |
| 39 | + $result = $parsedBody; | |
| 40 | + } else { | |
| 41 | + // Merge array/object results into existing result | |
| 42 | + $result = \array_merge($result, (array) $parsedBody); | |
| 29 | 43 | } |
| 30 | 44 | } |
| 31 | 45 | public function parseMemberFromStream(StreamInterface $stream, StructureShape $member, $response) |
| 32 | 46 | { |