Skip to content

Commit ce9ab82

Browse files
authored
feat(metadata): headers configuration (#6074)
1 parent 412ce02 commit ce9ab82

27 files changed

+204
-14
lines changed

features/main/headers.feature

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ Feature: Headers addition
66
When I send a "GET" request to "/dummy_cars"
77
Then the response status code should be 200
88
And the header "Sunset" should be equal to "Sat, 01 Jan 2050 00:00:00 +0000"
9+
10+
Scenario: Declare headers from resource
11+
When I send a "GET" request to "/redirect_to_foobar"
12+
Then the response status code should be 301
13+
And the header "Location" should be equal to "/foobar"
14+
And the header "Hello" should be equal to "World"

src/Metadata/ApiResource.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class ApiResource extends Metadata
3838
/**
3939
* @param array<int, HttpOperation>|array<string, HttpOperation>|Operations|null $operations Operations is a list of HttpOperation
4040
* @param array<string, Link>|array<string, mixed[]>|string[]|string|null $uriVariables
41+
* @param array<string, string> $headers
4142
* @param string|callable|null $provider
4243
* @param string|callable|null $processor
4344
* @param mixed|null $mercure
@@ -314,6 +315,7 @@ public function __construct(
314315
* - With GraphQL, the [`isDeprecated` and `deprecationReason` properties](https://facebook.github.io/graphql/June2018/#sec-Deprecation) will be added to the schema
315316
*/
316317
protected ?string $deprecationReason = null,
318+
protected ?array $headers = null,
317319
protected ?array $cacheHeaders = null,
318320
protected ?array $normalizationContext = null,
319321
protected ?array $denormalizationContext = null,
@@ -1280,6 +1282,19 @@ public function withController(string $controller): self
12801282
return $self;
12811283
}
12821284

1285+
public function getHeaders(): ?array
1286+
{
1287+
return $this->headers;
1288+
}
1289+
1290+
public function withHeaders(array $headers): self
1291+
{
1292+
$self = clone $this;
1293+
$self->headers = $headers;
1294+
1295+
return $self;
1296+
}
1297+
12831298
public function getCacheHeaders(): ?array
12841299
{
12851300
return $this->cacheHeaders;

src/Metadata/Delete.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function __construct(
3939
array $schemes = null,
4040
string $condition = null,
4141
string $controller = null,
42+
array $headers = null,
4243
array $cacheHeaders = null,
4344
array $paginationViaCursor = null,
4445
array $hydraContext = null,
@@ -115,6 +116,7 @@ public function __construct(
115116
schemes: $schemes,
116117
condition: $condition,
117118
controller: $controller,
119+
headers: $headers,
118120
cacheHeaders: $cacheHeaders,
119121
paginationViaCursor: $paginationViaCursor,
120122
hydraContext: $hydraContext,

src/Metadata/Error.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function __construct(
3939
array $schemes = null,
4040
string $condition = null,
4141
string $controller = null,
42+
array $headers = null,
4243
array $cacheHeaders = null,
4344
array $paginationViaCursor = null,
4445
array $hydraContext = null,
@@ -114,6 +115,7 @@ public function __construct(
114115
schemes: $schemes,
115116
condition: $condition,
116117
controller: $controller,
118+
headers: $headers,
117119
cacheHeaders: $cacheHeaders,
118120
paginationViaCursor: $paginationViaCursor,
119121
hydraContext: $hydraContext,

src/Metadata/Extractor/XmlResourceExtractor.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ private function buildExtendedBase(\SimpleXMLElement $resource): array
9696
'queryParameterValidationEnabled' => $this->phpize($resource, 'queryParameterValidationEnabled', 'bool'),
9797
'stateOptions' => $this->buildStateOptions($resource),
9898
'links' => $this->buildLinks($resource),
99+
'headers' => $this->buildHeaders($resource),
99100
]);
100101
}
101102

@@ -465,7 +466,6 @@ private function buildStateOptions(\SimpleXMLElement $resource): ?OptionsInterfa
465466
*/
466467
private function buildLinks(\SimpleXMLElement $resource): ?array
467468
{
468-
$links = $resource->links ?? null;
469469
if (!$resource->links) {
470470
return null;
471471
}
@@ -477,4 +477,21 @@ private function buildLinks(\SimpleXMLElement $resource): ?array
477477

478478
return $links;
479479
}
480+
481+
/**
482+
* @return array<string, string>
483+
*/
484+
private function buildHeaders(\SimpleXMLElement $resource): ?array
485+
{
486+
if (!$resource->headers) {
487+
return null;
488+
}
489+
490+
$headers = [];
491+
foreach ($resource->headers as $header) {
492+
$headers[(string) $header->header->attributes()->key] = (string) $header->header->attributes()->value;
493+
}
494+
495+
return $headers;
496+
}
480497
}

src/Metadata/Extractor/YamlResourceExtractor.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ private function buildExtendedBase(array $resource): array
123123
'outputFormats' => $this->buildArrayValue($resource, 'outputFormats'),
124124
'stateOptions' => $this->buildStateOptions($resource),
125125
'links' => $this->buildLinks($resource),
126+
'headers' => $this->buildHeaders($resource),
126127
]);
127128
}
128129

@@ -432,4 +433,21 @@ private function buildLinks(array $resource): ?array
432433

433434
return $links;
434435
}
436+
437+
/**
438+
* @return array<string, string>
439+
*/
440+
private function buildHeaders(array $resource): ?array
441+
{
442+
if (!isset($resource['headers']) || !\is_array($resource['headers'])) {
443+
return null;
444+
}
445+
446+
$headers = [];
447+
foreach ($resource['headers'] as $key => $value) {
448+
$headers[$key] = $value;
449+
}
450+
451+
return $headers;
452+
}
435453
}

src/Metadata/Extractor/schema/resources.xsd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,19 @@
407407
</xsd:sequence>
408408
</xsd:complexType>
409409

410+
<xsd:element name="header">
411+
<xsd:complexType>
412+
<xsd:attribute name="key" type="xsd:string"/>
413+
<xsd:attribute name="value" type="xsd:string"/>
414+
</xsd:complexType>
415+
</xsd:element>
416+
417+
<xsd:complexType name="headers">
418+
<xsd:sequence>
419+
<xsd:element ref="header"/>
420+
</xsd:sequence>
421+
</xsd:complexType>
422+
410423
<xsd:group name="base">
411424
<xsd:sequence>
412425
<xsd:element name="denormalizationContext" minOccurs="0" type="sequenceWithValues"/>
@@ -439,6 +452,7 @@
439452
<xsd:element name="types" minOccurs="0" type="types"/>
440453
<xsd:element name="uriVariables" minOccurs="0" type="uriVariables"/>
441454
<xsd:element name="links" minOccurs="0" type="links"/>
455+
<xsd:element name="headers" minOccurs="0" type="headers"/>
442456
</xsd:sequence>
443457
</xsd:group>
444458

src/Metadata/Get.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function __construct(
3939
array $schemes = null,
4040
string $condition = null,
4141
string $controller = null,
42+
array $headers = null,
4243
array $cacheHeaders = null,
4344
array $paginationViaCursor = null,
4445
array $hydraContext = null,
@@ -114,6 +115,7 @@ public function __construct(
114115
schemes: $schemes,
115116
condition: $condition,
116117
controller: $controller,
118+
headers: $headers,
117119
cacheHeaders: $cacheHeaders,
118120
paginationViaCursor: $paginationViaCursor,
119121
hydraContext: $hydraContext,

src/Metadata/GetCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function __construct(
3939
array $schemes = null,
4040
string $condition = null,
4141
string $controller = null,
42+
array $headers = null,
4243
array $cacheHeaders = null,
4344
array $paginationViaCursor = null,
4445
array $hydraContext = null,
@@ -115,6 +116,7 @@ public function __construct(
115116
schemes: $schemes,
116117
condition: $condition,
117118
controller: $controller,
119+
headers: $headers,
118120
cacheHeaders: $cacheHeaders,
119121
paginationViaCursor: $paginationViaCursor,
120122
hydraContext: $hydraContext,

src/Metadata/HttpOperation.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class HttpOperation extends Operation
5555
* stale_while_revalidate?: int,
5656
* stale-if-error?: int,
5757
* }|null $cacheHeaders {@see https://api-platform.com/docs/core/performance/#setting-custom-http-cache-headers}
58+
* @param array<string, string>|null $headers
5859
* @param array{
5960
* field: string,
6061
* direction: string,
@@ -144,6 +145,7 @@ public function __construct(
144145
protected ?array $schemes = null,
145146
protected ?string $condition = null,
146147
protected ?string $controller = null,
148+
protected ?array $headers = null,
147149
protected ?array $cacheHeaders = null,
148150
protected ?array $paginationViaCursor = null,
149151
protected ?array $hydraContext = null,
@@ -511,6 +513,19 @@ public function withController(string $controller): self
511513
return $self;
512514
}
513515

516+
public function getHeaders(): ?array
517+
{
518+
return $this->headers;
519+
}
520+
521+
public function withHeaders(array $headers): self
522+
{
523+
$self = clone $this;
524+
$self->headers = $headers;
525+
526+
return $self;
527+
}
528+
514529
public function getCacheHeaders(): ?array
515530
{
516531
return $this->cacheHeaders;

src/Metadata/NotExposed.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function __construct(
5050
array $schemes = null,
5151
string $condition = null,
5252
?string $controller = 'api_platform.action.not_exposed',
53+
array $headers = null,
5354
array $cacheHeaders = null,
5455
array $paginationViaCursor = null,
5556

@@ -128,6 +129,7 @@ public function __construct(
128129
schemes: $schemes,
129130
condition: $condition,
130131
controller: $controller,
132+
headers: $headers,
131133
cacheHeaders: $cacheHeaders,
132134
paginationViaCursor: $paginationViaCursor,
133135
hydraContext: $hydraContext,

src/Metadata/Patch.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function __construct(
3939
array $schemes = null,
4040
string $condition = null,
4141
string $controller = null,
42+
array $headers = null,
4243
array $cacheHeaders = null,
4344
array $paginationViaCursor = null,
4445
array $hydraContext = null,
@@ -115,6 +116,7 @@ public function __construct(
115116
schemes: $schemes,
116117
condition: $condition,
117118
controller: $controller,
119+
headers: $headers,
118120
cacheHeaders: $cacheHeaders,
119121
paginationViaCursor: $paginationViaCursor,
120122
hydraContext: $hydraContext,

src/Metadata/Post.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function __construct(
3939
array $schemes = null,
4040
string $condition = null,
4141
string $controller = null,
42+
array $headers = null,
4243
array $cacheHeaders = null,
4344
array $paginationViaCursor = null,
4445
array $hydraContext = null,
@@ -116,6 +117,7 @@ public function __construct(
116117
schemes: $schemes,
117118
condition: $condition,
118119
controller: $controller,
120+
headers: $headers,
119121
cacheHeaders: $cacheHeaders,
120122
paginationViaCursor: $paginationViaCursor,
121123
hydraContext: $hydraContext,

src/Metadata/Put.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function __construct(
3939
array $schemes = null,
4040
string $condition = null,
4141
string $controller = null,
42+
array $headers = null,
4243
array $cacheHeaders = null,
4344
array $paginationViaCursor = null,
4445
array $hydraContext = null,
@@ -116,6 +117,7 @@ public function __construct(
116117
schemes: $schemes,
117118
condition: $condition,
118119
controller: $controller,
120+
headers: $headers,
119121
cacheHeaders: $cacheHeaders,
120122
paginationViaCursor: $paginationViaCursor,
121123
hydraContext: $hydraContext,

src/Metadata/Tests/Extractor/Adapter/XmlResourceAdapter.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,20 @@ private function buildLinks(\SimpleXMLElement $resource, array $values = null):
507507
$childNode->addAttribute('href', $values[0]['href']);
508508
}
509509

510+
private function buildHeaders(\SimpleXMLElement $resource, array $values = null): void
511+
{
512+
if (!$values) {
513+
return;
514+
}
515+
516+
$node = $resource->addChild('headers');
517+
foreach ($values as $key => $value) {
518+
$childNode = $node->addChild('header');
519+
$childNode->addAttribute('key', $key);
520+
$childNode->addAttribute('value', $value);
521+
}
522+
}
523+
510524
private function parse($value): ?string
511525
{
512526
if (null === $value) {

0 commit comments

Comments
 (0)