Skip to content

Commit c04c859

Browse files
authored
Add an accept header in JSON request to help 3rd party services like localstack (#1721)
1 parent 55f001d commit c04c859

File tree

6 files changed

+36
-20
lines changed

6 files changed

+36
-20
lines changed

src/Generator/InputGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ private function inputClassRequestGetters(StructureShape $inputShape, ClassBuild
389389
$serializer = $this->serializer->get($operation->getService());
390390

391391
if ((null !== $payloadProperty = $inputShape->getPayload()) && $inputShape->getMember($payloadProperty)->isStreaming()) {
392-
$body['header'] = '$headers = [];' . "\n";
392+
$body['header'] = '$headers = ' . $serializer->getHeaders($operation, false) . ';' . "\n";
393393
} else {
394-
$body['header'] = '$headers = ' . $serializer->getHeaders($operation) . ';' . "\n";
394+
$body['header'] = '$headers = ' . $serializer->getHeaders($operation, true) . ';' . "\n";
395395
}
396396

397397
$body['querystring'] = '$query = [];' . "\n";

src/Generator/RequestSerializer/JsonRpcSerializer.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@
1515
*/
1616
class JsonRpcSerializer extends RestJsonSerializer
1717
{
18-
public function getHeaders(Operation $operation): string
18+
public function getHeaders(Operation $operation, bool $withPayload): string
1919
{
20-
return strtr(<<<PHP
21-
[
22-
'Content-Type' => 'application/x-amz-json-VERSION',
23-
'X-Amz-Target' => 'TARGET',
24-
];
20+
if (!$withPayload) {
21+
return "['Accept' => 'application/json']";
22+
}
2523

26-
PHP
27-
, [
28-
'VERSION' => number_format($operation->getService()->getJsonVersion(), 1),
29-
'TARGET' => sprintf('%s.%s', $operation->getService()->getTargetPrefix(), $operation->getName()),
30-
]);
24+
return strtr("[
25+
'Content-Type' => 'application/x-amz-json-VERSION',
26+
'X-Amz-Target' => 'TARGET',
27+
'Accept' => 'application/json',
28+
]", [
29+
'VERSION' => number_format($operation->getService()->getJsonVersion(), 1),
30+
'TARGET' => sprintf('%s.%s', $operation->getService()->getTargetPrefix(), $operation->getName()),
31+
]);
3132
}
3233
}

src/Generator/RequestSerializer/QuerySerializer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ public function __construct(NamespaceRegistry $namespaceRegistry, RequirementsRe
4343
$this->requirementsRegistry = $requirementsRegistry;
4444
}
4545

46-
public function getHeaders(Operation $operation): string
46+
public function getHeaders(Operation $operation, bool $withPayload): string
4747
{
48-
return '["content-type" => "application/x-www-form-urlencoded"]';
48+
if (!$withPayload) {
49+
return '[]';
50+
}
51+
52+
return "['content-type' => 'application/x-www-form-urlencoded']";
4953
}
5054

5155
public function generateRequestBody(Operation $operation, StructureShape $shape): SerializerResultBody

src/Generator/RequestSerializer/RestJsonSerializer.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,16 @@ public function __construct(NamespaceRegistry $namespaceRegistry, RequirementsRe
4343
$this->requirementsRegistry = $requirementsRegistry;
4444
}
4545

46-
public function getHeaders(Operation $operation): string
46+
public function getHeaders(Operation $operation, bool $withPayload): string
4747
{
48-
return '["content-type" => "application/json"]';
48+
if (!$withPayload) {
49+
return "['Accept' => 'application/json']";
50+
}
51+
52+
return "[
53+
'Content-Type' => 'application/json',
54+
'Accept' => 'application/json',
55+
]";
4956
}
5057

5158
public function generateRequestBody(Operation $operation, StructureShape $shape): SerializerResultBody

src/Generator/RequestSerializer/RestXmlSerializer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ public function __construct(NamespaceRegistry $namespaceRegistry, RequirementsRe
4040
$this->requirementsRegistry = $requirementsRegistry;
4141
}
4242

43-
public function getHeaders(Operation $operation): string
43+
public function getHeaders(Operation $operation, bool $withPayload): string
4444
{
45-
return '["content-type" => "application/xml"]';
45+
if (!$withPayload) {
46+
return '[]';
47+
}
48+
49+
return "['content-type' => 'application/xml']";
4650
}
4751

4852
public function generateRequestBody(Operation $operation, StructureShape $shape): SerializerResultBody

src/Generator/RequestSerializer/Serializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ public function generateRequestBody(Operation $operation, StructureShape $shape)
2727
*/
2828
public function generateRequestBuilder(StructureShape $shape, bool $needsChecks): SerializerResultBuilder;
2929

30-
public function getHeaders(Operation $operation): string;
30+
public function getHeaders(Operation $operation, bool $withPayload): string;
3131
}

0 commit comments

Comments
 (0)