From ec46cd4442a99a2136c6061877b70f23a5df9cfc Mon Sep 17 00:00:00 2001 From: rflavien Date: Fri, 13 Mar 2026 12:52:20 +0100 Subject: [PATCH 1/3] Add Symfony events for customizable error responses on sunset/brownout Dispatch ApiSunsetEvent and ApiBrownoutEvent before throwing GoneHttpException, allowing applications to set a custom Response (e.g. RFC 9457 Problem Details) instead of the default plain-text 410. Falls back to existing behavior when no listener handles the event or no dispatcher is available. Co-Authored-By: Claude Opus 4.6 --- src/ApiDeprecationBundle.php | 1 + src/Event/ApiBrownoutEvent.php | 9 ++ src/Event/ApiDeprecationEvent.php | 32 +++++ src/Event/ApiSunsetEvent.php | 9 ++ .../DeprecationHeaderListener.php | 51 ++++++++ .../DeprecationHeaderListenerTest.php | 116 ++++++++++++++++++ 6 files changed, 218 insertions(+) create mode 100644 src/Event/ApiBrownoutEvent.php create mode 100644 src/Event/ApiDeprecationEvent.php create mode 100644 src/Event/ApiSunsetEvent.php diff --git a/src/ApiDeprecationBundle.php b/src/ApiDeprecationBundle.php index fe143a6..8845077 100644 --- a/src/ApiDeprecationBundle.php +++ b/src/ApiDeprecationBundle.php @@ -74,6 +74,7 @@ public function loadExtension(array $config, ContainerConfigurator $container, C $config['response_on_sunset'], $builder->has('logger') ? service('logger') : null, $builder->has('clock') ? service('clock') : null, + service('event_dispatcher')->nullOnInvalid(), ]) ->tag('kernel.event_subscriber') ; diff --git a/src/Event/ApiBrownoutEvent.php b/src/Event/ApiBrownoutEvent.php new file mode 100644 index 0000000..8b1fbbf --- /dev/null +++ b/src/Event/ApiBrownoutEvent.php @@ -0,0 +1,9 @@ + $headers + */ + public function __construct( + public readonly ApiDeprecated $attribute, + public readonly array $headers, + ) { + } + + public function getResponse(): ?Response + { + return $this->response; + } + + public function setResponse(Response $response): void + { + $this->response = $response; + } +} diff --git a/src/Event/ApiSunsetEvent.php b/src/Event/ApiSunsetEvent.php new file mode 100644 index 0000000..a1ef896 --- /dev/null +++ b/src/Event/ApiSunsetEvent.php @@ -0,0 +1,9 @@ +responseOnSunset && $sunsetPassed) { $this->logger?->notice('Sunset endpoint called, returning 410 Gone.', ['sunset' => $attribute->sunset]); + $customResponse = $this->dispatchSunsetEvent($attribute, $headers); + + if ($customResponse !== null) { + $event->setController(static fn () => $customResponse); + + return; + } + throw new GoneHttpException( 'This endpoint has been removed.', headers: $headers, @@ -82,6 +95,14 @@ public function onKernelController(ControllerEvent $event): void $this->logger?->warning('Brownout active, returning 410 Gone.', ['brownout' => $attribute->brownout, 'sunset' => $attribute->sunset]); + $customResponse = $this->dispatchBrownoutEvent($attribute, $headers); + + if ($customResponse !== null) { + $event->setController(static fn () => $customResponse); + + return; + } + throw new GoneHttpException( 'This endpoint is deprecated and currently unavailable (brownout).', headers: $headers, @@ -114,6 +135,36 @@ public function onKernelResponse(ResponseEvent $event): void } } + /** + * @param array $headers + */ + private function dispatchSunsetEvent(ApiDeprecated $attribute, array $headers): ?Response + { + if ($this->eventDispatcher === null) { + return null; + } + + $sunsetEvent = new ApiSunsetEvent($attribute, $headers); + $this->eventDispatcher->dispatch($sunsetEvent); + + return $sunsetEvent->getResponse(); + } + + /** + * @param array $headers + */ + private function dispatchBrownoutEvent(ApiDeprecated $attribute, array $headers): ?Response + { + if ($this->eventDispatcher === null) { + return null; + } + + $brownoutEvent = new ApiBrownoutEvent($attribute, $headers); + $this->eventDispatcher->dispatch($brownoutEvent); + + return $brownoutEvent->getResponse(); + } + private function resolveAttribute(ControllerEvent $event): ?ApiDeprecated { $controller = $event->getController(); diff --git a/tests/EventListener/DeprecationHeaderListenerTest.php b/tests/EventListener/DeprecationHeaderListenerTest.php index af82df1..4f86b5d 100644 --- a/tests/EventListener/DeprecationHeaderListenerTest.php +++ b/tests/EventListener/DeprecationHeaderListenerTest.php @@ -15,7 +15,10 @@ use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\KernelEvents; use Tiime\ApiDeprecationBundle\Attribute\ApiDeprecated; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Tiime\ApiDeprecationBundle\Brownout\BrownoutChecker; +use Tiime\ApiDeprecationBundle\Event\ApiBrownoutEvent; +use Tiime\ApiDeprecationBundle\Event\ApiSunsetEvent; use Tiime\ApiDeprecationBundle\EventListener\DeprecationHeaderListener; use Tiime\ApiDeprecationBundle\Header\HeaderBuilder; @@ -481,6 +484,119 @@ public function __invoke(): void {} } } + public function testSunsetEventDispatchedWithCustomResponse(): void + { + $checker = $this->createBrownoutChecker(false); + $customResponse = new Response('{"error": "gone"}', Response::HTTP_GONE, ['Content-Type' => 'application/json']); + + $dispatcher = $this->createMock(EventDispatcherInterface::class); + $dispatcher->expects(self::once()) + ->method('dispatch') + ->with(self::isInstanceOf(ApiSunsetEvent::class)) + ->willReturnCallback(function (ApiSunsetEvent $event) use ($customResponse) { + $event->setResponse($customResponse); + + return $event; + }); + + $listener = new DeprecationHeaderListener($this->headerBuilder, $checker, true, $dispatcher); + + $controller = new #[ApiDeprecated(sunset: '2020-01-01')] class { + public function __invoke(): void {} + }; + + $request = new Request(); + $kernel = $this->createMock(HttpKernelInterface::class); + $event = new ControllerEvent($kernel, $controller, $request, HttpKernelInterface::MAIN_REQUEST); + + $listener->onKernelController($event); + + // Controller should have been replaced + $result = ($event->getController())(); + self::assertSame($customResponse, $result); + } + + public function testSunsetEventFallsBackToExceptionWhenNoResponseSet(): void + { + $checker = $this->createBrownoutChecker(false); + + $dispatcher = $this->createMock(EventDispatcherInterface::class); + $dispatcher->expects(self::once()) + ->method('dispatch') + ->with(self::isInstanceOf(ApiSunsetEvent::class)); + + $listener = new DeprecationHeaderListener($this->headerBuilder, $checker, true, $dispatcher); + + $controller = new #[ApiDeprecated(sunset: '2020-01-01')] class { + public function __invoke(): void {} + }; + + $request = new Request(); + $kernel = $this->createMock(HttpKernelInterface::class); + $event = new ControllerEvent($kernel, $controller, $request, HttpKernelInterface::MAIN_REQUEST); + + $this->expectException(GoneHttpException::class); + $this->expectExceptionMessage('removed'); + + $listener->onKernelController($event); + } + + public function testBrownoutEventDispatchedWithCustomResponse(): void + { + $checker = $this->createBrownoutChecker(true); + $customResponse = new Response('{"error": "brownout"}', Response::HTTP_GONE, ['Content-Type' => 'application/json']); + + $dispatcher = $this->createMock(EventDispatcherInterface::class); + $dispatcher->expects(self::once()) + ->method('dispatch') + ->with(self::isInstanceOf(ApiBrownoutEvent::class)) + ->willReturnCallback(function (ApiBrownoutEvent $event) use ($customResponse) { + $event->setResponse($customResponse); + + return $event; + }); + + $listener = new DeprecationHeaderListener($this->headerBuilder, $checker, false, $dispatcher); + + $controller = new #[ApiDeprecated(since: '2024-06-01', sunset: '2030-01-01', brownout: 'progressive')] class { + public function __invoke(): void {} + }; + + $request = new Request(); + $kernel = $this->createMock(HttpKernelInterface::class); + $event = new ControllerEvent($kernel, $controller, $request, HttpKernelInterface::MAIN_REQUEST); + + $listener->onKernelController($event); + + $result = ($event->getController())(); + self::assertSame($customResponse, $result); + } + + public function testBrownoutEventFallsBackToExceptionWhenNoResponseSet(): void + { + $checker = $this->createBrownoutChecker(true); + + $dispatcher = $this->createMock(EventDispatcherInterface::class); + $dispatcher->expects(self::once()) + ->method('dispatch') + ->with(self::isInstanceOf(ApiBrownoutEvent::class)); + + $listener = new DeprecationHeaderListener($this->headerBuilder, $checker, false, $dispatcher); + + $controller = new #[ApiDeprecated(since: '2024-06-01', sunset: '2030-01-01', brownout: 'progressive')] class { + public function __invoke(): void {} + }; + + $request = new Request(); + $kernel = $this->createMock(HttpKernelInterface::class); + $event = new ControllerEvent($kernel, $controller, $request, HttpKernelInterface::MAIN_REQUEST); + + $this->expectException(GoneHttpException::class); + $this->expectExceptionMessage('brownout'); + + $listener->onKernelController($event); + } + private function createBrownoutChecker(bool $inWindow): BrownoutChecker&\PHPUnit\Framework\MockObject\MockObject { $checker = $this->createMock(BrownoutChecker::class); From 562f13ad4e8a011d1eba0f55862c28e39e181691 Mon Sep 17 00:00:00 2001 From: rflavien Date: Fri, 13 Mar 2026 15:36:25 +0100 Subject: [PATCH 2/3] refine --- README.md | 41 +++++++++++++++++++ .../DeprecationHeaderListenerTest.php | 8 ++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 876da0a..08ae5b8 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,47 @@ Outside the window, the endpoint works normally with deprecation headers. If `response_on_sunset` is enabled (default), the endpoint permanently returns `410 Gone` once the sunset date has passed. +## Customizing the error response + +Two Symfony events are dispatched before the default `410 Gone` response is returned: + +- `ApiSunsetEvent` — dispatched when the sunset date has passed +- `ApiBrownoutEvent` — dispatched during a brownout window + +An event listener can call `$event->setResponse()` to replace the default 410 with a custom response. Both events expose the `ApiDeprecated` attribute and the deprecation headers that will be added to the response. + +### Example: returning a Problem Details response (RFC 9457) + +```php +use Symfony\Component\EventDispatcher\Attribute\AsEventListener; +use Symfony\Component\HttpFoundation\JsonResponse; +use Tiime\ApiDeprecationBundle\Event\ApiBrownoutEvent; +use Tiime\ApiDeprecationBundle\Event\ApiSunsetEvent; + +#[AsEventListener] +final class DeprecationProblemDetailsListener +{ + public function __invoke(ApiSunsetEvent|ApiBrownoutEvent $event): void + { + $response = new JsonResponse([ + 'type' => 'https://docs.example.com/errors/gone', + 'title' => 'Gone', + 'status' => 410, + 'detail' => sprintf( + 'This endpoint was deprecated on %s and is no longer available.', + $event->attribute->since, + ), + ], 410, ['Content-Type' => 'application/problem+json']); + + $event->setResponse($response); + } +} +``` + +The `$event->headers` array contains the computed deprecation headers (`Deprecation`, `Sunset`, `Link`, `Retry-After`) — they are automatically added to whatever response is returned. + +If no listener sets a response, the default `410 Gone` with a plain-text body is returned (backward compatible). + ## Attribute parameters | Parameter | Type | Default | Description | diff --git a/tests/EventListener/DeprecationHeaderListenerTest.php b/tests/EventListener/DeprecationHeaderListenerTest.php index 4f86b5d..6235bac 100644 --- a/tests/EventListener/DeprecationHeaderListenerTest.php +++ b/tests/EventListener/DeprecationHeaderListenerTest.php @@ -499,7 +499,7 @@ public function testSunsetEventDispatchedWithCustomResponse(): void return $event; }); - $listener = new DeprecationHeaderListener($this->headerBuilder, $checker, true, $dispatcher); + $listener = new DeprecationHeaderListener($this->headerBuilder, $checker, true, eventDispatcher: $dispatcher); $controller = new #[ApiDeprecated(sunset: '2020-01-01')] class { public function __invoke(): void {} @@ -525,7 +525,7 @@ public function testSunsetEventFallsBackToExceptionWhenNoResponseSet(): void ->method('dispatch') ->with(self::isInstanceOf(ApiSunsetEvent::class)); - $listener = new DeprecationHeaderListener($this->headerBuilder, $checker, true, $dispatcher); + $listener = new DeprecationHeaderListener($this->headerBuilder, $checker, true, eventDispatcher: $dispatcher); $controller = new #[ApiDeprecated(sunset: '2020-01-01')] class { public function __invoke(): void {} @@ -556,7 +556,7 @@ public function testBrownoutEventDispatchedWithCustomResponse(): void return $event; }); - $listener = new DeprecationHeaderListener($this->headerBuilder, $checker, false, $dispatcher); + $listener = new DeprecationHeaderListener($this->headerBuilder, $checker, false, eventDispatcher: $dispatcher); $controller = new #[ApiDeprecated(since: '2024-06-01', sunset: '2030-01-01', brownout: 'progressive')] class { public function __invoke(): void {} @@ -581,7 +581,7 @@ public function testBrownoutEventFallsBackToExceptionWhenNoResponseSet(): void ->method('dispatch') ->with(self::isInstanceOf(ApiBrownoutEvent::class)); - $listener = new DeprecationHeaderListener($this->headerBuilder, $checker, false, $dispatcher); + $listener = new DeprecationHeaderListener($this->headerBuilder, $checker, false, eventDispatcher: $dispatcher); $controller = new #[ApiDeprecated(since: '2024-06-01', sunset: '2030-01-01', brownout: 'progressive')] class { public function __invoke(): void {} From 847549128e21975c0ddffd114ac5d54eeb48155f Mon Sep 17 00:00:00 2001 From: rflavien Date: Fri, 13 Mar 2026 15:38:16 +0100 Subject: [PATCH 3/3] composer --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index ecaf9db..6f9c451 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ "dragonmantank/cron-expression": "^3.3", "symfony/config": "^6.4|^7.0|^8.0", "symfony/dependency-injection": "^6.4|^7.0|^8.0", + "symfony/event-dispatcher-contracts": "^3.0", "symfony/http-kernel": "^6.4|^7.0|^8.0" }, "require-dev": {