Skip to content
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
1 change: 1 addition & 0 deletions features/hydra/entrypoint.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Feature: Entrypoint support
I need to access to an entrypoint listing top-level resources

Scenario: Retrieve the Entrypoint
When I add "Accept" header equal to "application/ld+json"
When I send a "GET" request to "/"
Then the response status code should be 200
And the response should be in JSON
Expand Down
6 changes: 6 additions & 0 deletions features/openapi/docs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,9 @@ Feature: Documentation support
And I send a "GET" request to "/docs"
Then the response status code should be 200
And the header "Content-Type" should be equal to "application/vnd.openapi+yaml; charset=utf-8"

Scenario: Retrieve the OpenAPI documentation
Given I add "Accept" header equal to "text/html"
And I send a "GET" request to "/"
Then the response status code should be 200
And the header "Content-Type" should be equal to "text/html; charset=utf-8"
16 changes: 14 additions & 2 deletions src/Documentation/Action/EntrypointAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Documentation\Entrypoint;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceNameCollection;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\ProviderInterface;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -27,6 +28,8 @@
*/
final class EntrypointAction
{
private static ResourceNameCollection $resourceNameCollection;

public function __construct(
private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory,
private readonly ProviderInterface $provider,
Expand All @@ -35,12 +38,21 @@ public function __construct(
) {
}

public function __invoke(Request $request = null)
public function __invoke(Request $request)
{
static::$resourceNameCollection = $this->resourceNameCollectionFactory->create();
$context = ['request' => $request];
$operation = new Get(outputFormats: $this->documentationFormats, read: true, serialize: true, class: Entrypoint::class, provider: fn () => new Entrypoint($this->resourceNameCollectionFactory->create()));
$request->attributes->set('_api_platform_disable_listeners', true);
$operation = new Get(outputFormats: $this->documentationFormats, read: true, serialize: true, class: Entrypoint::class, provider: [self::class, 'provide']);
$request->attributes->set('_api_operation', $operation);
$body = $this->provider->provide($operation, [], $context);
$operation = $request->attributes->get('_api_operation');

return $this->processor->process($body, $operation, [], $context);
}

public static function provide(): Entrypoint
{
return new Entrypoint(static::$resourceNameCollection);
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/Resources/config/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<service id="ApiPlatform\Action\NotFoundAction" alias="api_platform.action.not_found" public="true" />
<service id="ApiPlatform\Action\NotExposedAction" alias="api_platform.action.not_exposed" public="true" />

<service id="api_platform.action.entrypoint" class="ApiPlatform\Action\EntrypointAction" public="true">
<service id="api_platform.action.entrypoint" class="ApiPlatform\Documentation\Action\EntrypointAction" public="true">
<argument type="service" id="api_platform.metadata.resource.name_collection_factory" />
<argument type="service" id="api_platform.state_provider.main" on-invalid="null" />
<argument type="service" id="api_platform.state_processor.main" on-invalid="null" />
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/Resources/config/legacy/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<argument>%api_platform.formats%</argument>
<argument>%api_platform.error_formats%</argument>
<argument>%api_platform.docs_formats%</argument>
<argument>%api_platform.event_listeners_backward_compatibility_layer%</argument>

<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="28" />
</service>
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/EventListener/AddFormatListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class AddFormatListener
{
use OperationRequestInitiatorTrait;

public function __construct(private readonly Negotiator $negotiator, ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, private readonly array $formats = [], private readonly array $errorFormats = [], private readonly array $docsFormats = [])
public function __construct(private readonly Negotiator $negotiator, ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, private readonly array $formats = [], private readonly array $errorFormats = [], private readonly array $docsFormats = [], private readonly bool $eventsBackwardCompatibility = true)
{
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
}
Expand All @@ -50,7 +50,7 @@ public function onKernelRequest(RequestEvent $event): void
$request = $event->getRequest();
$operation = $this->initializeOperation($request);

if ('api_platform.symfony.main_controller' === $operation?->getController()) {
if ('api_platform.symfony.main_controller' === $operation?->getController() || ($this->eventsBackwardCompatibility && 'api_platform.action.entrypoint' === $request->attributes->get('_controller')) || $request->attributes->get('_api_platform_disable_listeners')) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/EventListener/AddHeadersListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(private readonly bool $etag = false, private readonl
public function onKernelResponse(ResponseEvent $event): void
{
$request = $event->getRequest();
if (!$request->isMethodCacheable()) {
if (!$request->isMethodCacheable() || $request->attributes->get('_api_platform_disable_listeners')) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/EventListener/AddLinkHeaderListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function onKernelResponse(ResponseEvent $event): void
$operation = $this->initializeOperation($request);

// API Platform 3.2 has a MainController where everything is handled by processors/providers
if ('api_platform.symfony.main_controller' === $operation?->getController() || $this->isPreflightRequest($request)) {
if ('api_platform.symfony.main_controller' === $operation?->getController() || $this->isPreflightRequest($request) || $request->attributes->get('_api_platform_disable_listeners')) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/EventListener/AddTagsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function onKernelResponse(ResponseEvent $event): void
!$request->isMethodCacheable()
|| !$response->isCacheable()
|| (!$attributes = RequestAttributesExtractor::extractAttributes($request))
|| $request->attributes->get('_api_platform_disable_listeners')
) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/EventListener/DenyAccessListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function onSecurityPostValidation(ViewEvent $event): void
*/
private function checkSecurity(Request $request, string $attribute, array $extraVariables = []): void
{
if (!$this->resourceAccessChecker || !$attributes = RequestAttributesExtractor::extractAttributes($request)) {
if ($request->attributes->get('_api_platform_disable_listeners') || !$this->resourceAccessChecker || !$attributes = RequestAttributesExtractor::extractAttributes($request)) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/EventListener/DeserializeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function onKernelRequest(RequestEvent $event): void
|| $request->isMethodSafe()
|| !($attributes = RequestAttributesExtractor::extractAttributes($request))
|| !$attributes['receive']
|| $request->attributes->get('_api_platform_disable_listeners')
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function onKernelRequest(RequestEvent $event): void
!$request->isMethodSafe()
|| !($attributes = RequestAttributesExtractor::extractAttributes($request))
|| 'GET' !== $request->getMethod()
|| $request->attributes->get('_api_platform_disable_listeners')
) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/EventListener/ReadListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function onKernelRequest(RequestEvent $event): void
$request = $event->getRequest();
$operation = $this->initializeOperation($request);

if ('api_platform.symfony.main_controller' === $operation?->getController()) {
if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/EventListener/RespondListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function onKernelView(ViewEvent $event): void
$controllerResult = $event->getControllerResult();
$operation = $this->initializeOperation($request);

if ('api_platform.symfony.main_controller' === $operation?->getController()) {
if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/EventListener/SerializeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function onKernelView(ViewEvent $event): void

$operation = $this->initializeOperation($request);

if ('api_platform.symfony.main_controller' === $operation?->getController()) {
if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/EventListener/ValidateListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function onKernelView(ViewEvent $event): void
$controllerResult = $event->getControllerResult();
$request = $event->getRequest();
$operation = $this->initializeOperation($request);
if ('api_platform.symfony.main_controller' === $operation?->getController()) {
if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/EventListener/WriteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function onKernelView(ViewEvent $event): void
$operation = $this->initializeOperation($request);

// API Platform 3.2 has a MainController where everything is handled by processors/providers
if ('api_platform.symfony.main_controller' === $operation?->getController()) {
if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
return;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Symfony/Bundle/Twig/ApiPlatformProfilerPanelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ protected function tearDown(): void
parent::tearDown();
}

/**
* TODO: remove openapiContext to get rid of the legacy.
*
* @group legacy
*/
public function testDebugBarContentNotResourceClass(): void
{
$client = static::createClient();
Expand Down Expand Up @@ -94,6 +99,11 @@ public function testDebugBarContent(): void
$this->assertSame('mongodb' === $this->env ? DocumentDummy::class : Dummy::class, $block->filterXPath('//div[@class="sf-toolbar-info-piece"][./b[contains(., "Resource Class")]]/span')->html());
}

/**
* TODO: remove openapiContext to get rid of the legacy.
*
* @group legacy
*/
public function testProfilerGeneralLayoutNotResourceClass(): void
{
$client = static::createClient();
Expand Down