From 6c9b508b030976741a68f84e226502e6c218e896 Mon Sep 17 00:00:00 2001 From: Valentin Dassonville <129871973+valentin-dassonville@users.noreply.github.com> Date: Tue, 5 Nov 2024 09:54:12 +0100 Subject: [PATCH] fix(laravel): remove link header when jsonld is not enabled (#6768) --- src/Laravel/ApiPlatformProvider.php | 23 ++++++---- src/Laravel/Tests/LinkHeaderTest.php | 45 +++++++++++++++++++ .../Tests/LinkHeaderWithoutJsonldTest.php | 45 +++++++++++++++++++ 3 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 src/Laravel/Tests/LinkHeaderTest.php create mode 100644 src/Laravel/Tests/LinkHeaderWithoutJsonldTest.php diff --git a/src/Laravel/ApiPlatformProvider.php b/src/Laravel/ApiPlatformProvider.php index a71a43ebb92..d30598bfe53 100644 --- a/src/Laravel/ApiPlatformProvider.php +++ b/src/Laravel/ApiPlatformProvider.php @@ -529,6 +529,14 @@ public function register(): void return new CallableProcessor(new ServiceLocator($tagged)); }); + $this->app->singleton(RespondProcessor::class, function () { + return new AddLinkHeaderProcessor(new RespondProcessor(), new HttpHeaderSerializer()); + }); + + $this->app->singleton(SerializeProcessor::class, function (Application $app) { + return new SerializeProcessor($app->make(RespondProcessor::class), $app->make(Serializer::class), $app->make(SerializerContextBuilderInterface::class)); + }); + $this->app->singleton(WriteProcessor::class, function (Application $app) { return new WriteProcessor($app->make(SerializeProcessor::class), $app->make(CallableProcessor::class)); }); @@ -547,19 +555,18 @@ public function register(): void ); }); - $this->app->singleton(SerializeProcessor::class, function (Application $app) { - return new SerializeProcessor($app->make(RespondProcessor::class), $app->make(Serializer::class), $app->make(SerializerContextBuilderInterface::class)); - }); - $this->app->singleton(HydraLinkProcessor::class, function (Application $app) { return new HydraLinkProcessor($app->make(WriteProcessor::class), $app->make(UrlGeneratorInterface::class)); }); - $this->app->singleton(RespondProcessor::class, function () { - return new AddLinkHeaderProcessor(new RespondProcessor(), new HttpHeaderSerializer()); - }); + $this->app->bind(ProcessorInterface::class, function (Application $app) { + $config = $app['config']; + if ($config->has('api-platform.formats.jsonld')) { + return $app->make(HydraLinkProcessor::class); + } - $this->app->bind(ProcessorInterface::class, HydraLinkProcessor::class); + return $app->make(WriteProcessor::class); + }); $this->app->singleton(ObjectNormalizer::class, function (Application $app) { $config = $app['config']; diff --git a/src/Laravel/Tests/LinkHeaderTest.php b/src/Laravel/Tests/LinkHeaderTest.php new file mode 100644 index 00000000000..4aa1a41e9c5 --- /dev/null +++ b/src/Laravel/Tests/LinkHeaderTest.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait; +use Illuminate\Contracts\Config\Repository; +use Illuminate\Foundation\Application; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Orchestra\Testbench\Concerns\WithWorkbench; +use Orchestra\Testbench\TestCase; + +class LinkHeaderTest extends TestCase +{ + use ApiTestAssertionsTrait; + use RefreshDatabase; + use WithWorkbench; + + /** + * @param Application $app + */ + protected function defineEnvironment($app): void + { + tap($app['config'], function (Repository $config): void { + $config->set('app.debug', true); + $config->set('api-platform.formats', ['jsonld' => ['application/ld+json']]); + $config->set('api-platform.docs_formats', ['jsonld' => ['application/ld+json']]); + }); + } + + public function testLinkHeader(): void + { + $response = $this->get('/api/', ['accept' => ['application/ld+json']]); + $response->assertStatus(200); + $response->assertHeader('link', '; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"'); + } +} diff --git a/src/Laravel/Tests/LinkHeaderWithoutJsonldTest.php b/src/Laravel/Tests/LinkHeaderWithoutJsonldTest.php new file mode 100644 index 00000000000..edc2178f24f --- /dev/null +++ b/src/Laravel/Tests/LinkHeaderWithoutJsonldTest.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait; +use Illuminate\Contracts\Config\Repository; +use Illuminate\Foundation\Application; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Orchestra\Testbench\Concerns\WithWorkbench; +use Orchestra\Testbench\TestCase; + +class LinkHeaderWithoutJsonldTest extends TestCase +{ + use ApiTestAssertionsTrait; + use RefreshDatabase; + use WithWorkbench; + + /** + * @param Application $app + */ + protected function defineEnvironment($app): void + { + tap($app['config'], function (Repository $config): void { + $config->set('app.debug', true); + $config->set('api-platform.formats', ['jsonapi' => ['application/vnd.api+json']]); + $config->set('api-platform.docs_formats', ['jsonapi' => ['application/vnd.api+json']]); + }); + } + + public function testLinkHeader(): void + { + $response = $this->get('/api/', ['accept' => ['application/vnd.api+json']]); + $response->assertStatus(200); + $response->assertHeaderMissing('link'); + } +}