Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(laravel): remove link header when jsonld is not enabled #6768

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <dunglas@gmail.com>
*
* 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 <dunglas@gmail.com>
*
* 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');
}
}
Loading