Skip to content

Commit

Permalink
fix(laravel): remove link header when jsonld is not enabled (#6768)
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-dassonville authored Nov 5, 2024
1 parent aaaaa2c commit 6c9b508
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/Laravel/ApiPlatformProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
Expand All @@ -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'];
Expand Down
45 changes: 45 additions & 0 deletions src/Laravel/Tests/LinkHeaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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', '<http://localhost/api/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"');
}
}
45 changes: 45 additions & 0 deletions src/Laravel/Tests/LinkHeaderWithoutJsonldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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');
}
}

0 comments on commit 6c9b508

Please sign in to comment.