Skip to content

Fix duplicate Link header when sunset has passed or brownout is active - #14

Open
rflavien wants to merge 1 commit into
mainfrom
fix/duplicate-link-header
Open

rflavien wants to merge 1 commit into
mainfrom
fix/duplicate-link-header

Conversation

@rflavien

Copy link
Copy Markdown
Contributor

Summary

  • Fix a bug where the Link header value was appended twice when sunset had passed (or during brownout), because onKernelResponse re-added headers that were already attached via GoneHttpException or custom event responses.
  • Remove _api_deprecated request attribute before each of the 4 exit points in onKernelController (sunset custom response, sunset exception, brownout custom response, brownout exception) so onKernelResponse no longer re-processes them.
  • Add 4 regression tests covering the full controller+response lifecycle for both sunset and brownout paths.

Root cause

onKernelController stores _api_deprecated on the request (line 55) before checking sunset/brownout. When the controller phase handles the response (custom response or exception with headers), the attribute remains on the request. onKernelResponse then finds it, rebuilds headers, and appends the Link value again — producing a duplicated header like:

Link: </api/v2>; rel="deprecation"; type="text/html", </api/v2>; rel="deprecation"; type="text/html"

Quality gates

  • PHPUnit: 74 tests, 2383 assertions — all pass
  • PHPStan: level max, 0 errors
  • Infection: 97% MSI (threshold: 80%)

Remove the _api_deprecated request attribute before each exit point in
onKernelController (sunset/brownout custom response and exception paths).
This prevents onKernelResponse from rebuilding and re-appending the Link
header to responses that already carry it.
Copilot AI review requested due to automatic review settings March 13, 2026 16:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a bug in the deprecation header listener where the Link header could be appended twice during the sunset/brownout “gone” flows, and adds regression tests intended to cover the controller+response lifecycle for those paths.

Changes:

  • Clear _api_deprecated from the request before returning a custom sunset/brownout response or throwing GoneHttpException.
  • Add 4 new regression tests simulating the controller + response event lifecycle for sunset/brownout, covering both custom-response and exception paths.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/EventListener/DeprecationHeaderListener.php Removes _api_deprecated before sunset/brownout early exits to prevent onKernelResponse() from re-processing headers.
tests/EventListener/DeprecationHeaderListenerTest.php Adds regression tests to ensure Link is not duplicated in sunset/brownout custom response and exception flows.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines 69 to 72
if ($customResponse !== null) {
$event->getRequest()->attributes->remove('_api_deprecated');
$event->setController(static fn () => $customResponse);

self::assertSame(
'</api/v2/users>; rel="deprecation"; type="text/html"',
$customResponse->headers->get('Link'),
);
Comment on lines +658 to +666
// After the exception, the attribute should have been removed
// so onKernelResponse won't re-add headers to the error response
self::assertNull($request->attributes->get('_api_deprecated'));

// The exception itself should carry the headers
$headers = $e->getHeaders();
self::assertArrayHasKey('Link', $headers);
self::assertArrayHasKey('Sunset', $headers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants