Skip to content

Commit 6c9b508

Browse files
authoredNov 5, 2024··
fix(laravel): remove link header when jsonld is not enabled (#6768)
1 parent aaaaa2c commit 6c9b508

File tree

3 files changed

+105
-8
lines changed

3 files changed

+105
-8
lines changed
 

‎src/Laravel/ApiPlatformProvider.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,14 @@ public function register(): void
529529
return new CallableProcessor(new ServiceLocator($tagged));
530530
});
531531

532+
$this->app->singleton(RespondProcessor::class, function () {
533+
return new AddLinkHeaderProcessor(new RespondProcessor(), new HttpHeaderSerializer());
534+
});
535+
536+
$this->app->singleton(SerializeProcessor::class, function (Application $app) {
537+
return new SerializeProcessor($app->make(RespondProcessor::class), $app->make(Serializer::class), $app->make(SerializerContextBuilderInterface::class));
538+
});
539+
532540
$this->app->singleton(WriteProcessor::class, function (Application $app) {
533541
return new WriteProcessor($app->make(SerializeProcessor::class), $app->make(CallableProcessor::class));
534542
});
@@ -547,19 +555,18 @@ public function register(): void
547555
);
548556
});
549557

550-
$this->app->singleton(SerializeProcessor::class, function (Application $app) {
551-
return new SerializeProcessor($app->make(RespondProcessor::class), $app->make(Serializer::class), $app->make(SerializerContextBuilderInterface::class));
552-
});
553-
554558
$this->app->singleton(HydraLinkProcessor::class, function (Application $app) {
555559
return new HydraLinkProcessor($app->make(WriteProcessor::class), $app->make(UrlGeneratorInterface::class));
556560
});
557561

558-
$this->app->singleton(RespondProcessor::class, function () {
559-
return new AddLinkHeaderProcessor(new RespondProcessor(), new HttpHeaderSerializer());
560-
});
562+
$this->app->bind(ProcessorInterface::class, function (Application $app) {
563+
$config = $app['config'];
564+
if ($config->has('api-platform.formats.jsonld')) {
565+
return $app->make(HydraLinkProcessor::class);
566+
}
561567

562-
$this->app->bind(ProcessorInterface::class, HydraLinkProcessor::class);
568+
return $app->make(WriteProcessor::class);
569+
});
563570

564571
$this->app->singleton(ObjectNormalizer::class, function (Application $app) {
565572
$config = $app['config'];

‎src/Laravel/Tests/LinkHeaderTest.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
15+
use Illuminate\Contracts\Config\Repository;
16+
use Illuminate\Foundation\Application;
17+
use Illuminate\Foundation\Testing\RefreshDatabase;
18+
use Orchestra\Testbench\Concerns\WithWorkbench;
19+
use Orchestra\Testbench\TestCase;
20+
21+
class LinkHeaderTest extends TestCase
22+
{
23+
use ApiTestAssertionsTrait;
24+
use RefreshDatabase;
25+
use WithWorkbench;
26+
27+
/**
28+
* @param Application $app
29+
*/
30+
protected function defineEnvironment($app): void
31+
{
32+
tap($app['config'], function (Repository $config): void {
33+
$config->set('app.debug', true);
34+
$config->set('api-platform.formats', ['jsonld' => ['application/ld+json']]);
35+
$config->set('api-platform.docs_formats', ['jsonld' => ['application/ld+json']]);
36+
});
37+
}
38+
39+
public function testLinkHeader(): void
40+
{
41+
$response = $this->get('/api/', ['accept' => ['application/ld+json']]);
42+
$response->assertStatus(200);
43+
$response->assertHeader('link', '<http://localhost/api/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"');
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
15+
use Illuminate\Contracts\Config\Repository;
16+
use Illuminate\Foundation\Application;
17+
use Illuminate\Foundation\Testing\RefreshDatabase;
18+
use Orchestra\Testbench\Concerns\WithWorkbench;
19+
use Orchestra\Testbench\TestCase;
20+
21+
class LinkHeaderWithoutJsonldTest extends TestCase
22+
{
23+
use ApiTestAssertionsTrait;
24+
use RefreshDatabase;
25+
use WithWorkbench;
26+
27+
/**
28+
* @param Application $app
29+
*/
30+
protected function defineEnvironment($app): void
31+
{
32+
tap($app['config'], function (Repository $config): void {
33+
$config->set('app.debug', true);
34+
$config->set('api-platform.formats', ['jsonapi' => ['application/vnd.api+json']]);
35+
$config->set('api-platform.docs_formats', ['jsonapi' => ['application/vnd.api+json']]);
36+
});
37+
}
38+
39+
public function testLinkHeader(): void
40+
{
41+
$response = $this->get('/api/', ['accept' => ['application/vnd.api+json']]);
42+
$response->assertStatus(200);
43+
$response->assertHeaderMissing('link');
44+
}
45+
}

0 commit comments

Comments
 (0)
Please sign in to comment.