Skip to content

Commit c860945

Browse files
committed
fix(state): add support for the Deprecation link header
1 parent 502c0e2 commit c860945

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

src/State/Processor/AddLinkHeaderProcessor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
5252
// We add our header here as Symfony does it only for the main Request and we want it to be done on errors (sub-request) as well
5353
$linksProvider = $request->attributes->get('_api_platform_links');
5454
if ($this->serializer && ($links = $linksProvider?->getLinks())) {
55-
$response->headers->set('Link', $this->serializer->serialize($links));
55+
$linkHeader = implode(',', array_filter([$this->serializer->serialize($links), $response->headers->get('Link')]));
56+
$response->headers->set('Link', $linkHeader === '' ? null : $linkHeader);
5657
}
5758
$this->stopwatch?->stop('api_platform.processor.add_link_header');
5859

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\GetCollection;
19+
20+
#[ApiResource(
21+
operations: [new GetCollection(provider: [self::class, 'provide'])],
22+
deprecationReason: 'This API is deprecated ',
23+
headers: [
24+
'deprecation' => '@1688169599',
25+
'sunset' => 'Sun, 30 Jun 2024 23:59:59 UTC',
26+
'link' => '<https://developer.example.com/deprecation>; rel="deprecation"; type="text/html"',
27+
],
28+
)]
29+
class DeprecationHeader
30+
{
31+
public function __construct(#[ApiProperty(identifier: true)] public int $id)
32+
{
33+
}
34+
35+
public static function provide(): iterable
36+
{
37+
return [
38+
new self(1),
39+
new self(2),
40+
];
41+
}
42+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Functional;
15+
16+
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
17+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\DeprecationHeader;
18+
use ApiPlatform\Tests\SetupClassResourcesTrait;
19+
20+
final class DeprecationHeaderTest extends ApiTestCase
21+
{
22+
use SetupClassResourcesTrait;
23+
24+
protected static ?bool $alwaysBootKernel = false;
25+
26+
/**
27+
* @return class-string[]
28+
*/
29+
public static function getResources(): array
30+
{
31+
return [DeprecationHeader::class];
32+
}
33+
34+
public function testDeprecationHeader(): void
35+
{
36+
$response = self::createClient()->request('GET', '/deprecation_headers');
37+
38+
$headers = $response->getHeaders();
39+
40+
$this->assertContains('@1688169599', $headers['deprecation']);
41+
$this->assertContains('Sun, 30 Jun 2024 23:59:59 UTC', $headers['sunset']);
42+
$this->assertStringContainsString('<https://developer.example.com/deprecation>; rel="deprecation"; type="text/html"', $headers['link'][0]);
43+
}
44+
}

0 commit comments

Comments
 (0)