Skip to content

Add HttpClient decorator to detect deprecation headers - #15

Open
rflavien wants to merge 3 commits into
mainfrom
trigger_deprec
Open

rflavien wants to merge 3 commits into
mainfrom
trigger_deprec

Conversation

@rflavien

@rflavien rflavien commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add DeprecationDetectorClient, a Symfony HttpClient decorator that detects RFC 9745/8594 deprecation headers (Deprecation, Sunset, Link rel="deprecation") in HTTP responses and triggers trigger_deprecation()
  • Registered as an optional bridge (like NelmioApiDoc): only active when symfony/http-client is installed and http_client service exists
  • Uses AsyncDecoratorTrait — the standard Symfony pattern for HTTP client decoration

Test plan

  • 6 unit tests covering all header combinations (each individually, all together, absent, Link without rel="deprecation")
  • PHPStan level max passes
  • Infection MSI 97% (threshold 80%)
  • Full make qa green

🤖 Generated with Claude Code

…ders

Add a bridge for Symfony HttpClient that triggers `trigger_deprecation()`
when a response contains RFC 9745/8594 deprecation headers (Deprecation,
Sunset, Link rel="deprecation"). Registered conditionally when
symfony/http-client is installed.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 2, 2026 09:03

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

This PR adds an optional Symfony HttpClient bridge to the bundle that detects RFC deprecation-related response headers and emits E_USER_DEPRECATED via trigger_deprecation(), enabling consumers to notice deprecated remote endpoints at runtime.

Changes:

  • Introduces DeprecationDetectorClient (HttpClient decorator) that checks Deprecation, Sunset, and Link (rel="deprecation") headers on responses.
  • Registers the decorator automatically when symfony/http-client is installed and the http_client service exists.
  • Adds unit tests covering expected header combinations and updates dev dependencies/suggestions.

Reviewed changes

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

File Description
src/Bridge/HttpClient/DeprecationDetectorClient.php Adds the HttpClient decorator and deprecation-header detection logic.
src/ApiDeprecationBundle.php Conditionally decorates the http_client service with the new detector.
tests/Bridge/HttpClient/DeprecationDetectorClientTest.php Adds unit tests to assert deprecation triggering behavior for header combinations.
composer.json Adds symfony/http-client to require-dev and suggests it for consumers.

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

Comment on lines +70 to +72
if (str_contains($link, 'rel="deprecation"')) {
return $link;
}

Copilot AI Apr 2, 2026

Copy link

Choose a reason for hiding this comment

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

findDeprecationLink() only matches the exact substring rel="deprecation". Valid Link headers can use an unquoted token (e.g. rel=deprecation), different casing, or multiple relation types (e.g. rel="foo deprecation"). This implementation will miss those legitimate deprecation links and fail to trigger deprecations.

Consider parsing the Link header parameters per RFC 8288 (tokenizing rel values case-insensitively and supporting both token and quoted-string forms), or at least using a regex that matches rel containing the deprecation token among other rel values.

Suggested change
if (str_contains($link, 'rel="deprecation"')) {
return $link;
}
// Split into URI-reference and parameters
$parts = explode(';', $link);
// No parameters, cannot contain a rel attribute
if (\count($parts) < 2) {
continue;
}
// Inspect parameters after the URI-reference
foreach (\array_slice($parts, 1) as $param) {
$param = \trim($param);
if ($param == '') {
continue;
}
$equalsPos = \strpos($param, '=');
if ($equalsPos === false) {
continue;
}
$name = \substr($param, 0, $equalsPos);
$value = \substr($param, $equalsPos + 1);
if (\strcasecmp(\trim($name), 'rel') !== 0) {
continue;
}
// Normalize value: trim whitespace and optional surrounding quotes
$value = \trim($value);
if ($value !== '' && $value[0] === '"' && \substr($value, -1) === '"') {
$value = \substr($value, 1, -1);
}
// rel can contain multiple space-separated relation types
$tokens = \preg_split('/\s+/', $value) ?: [];
foreach ($tokens as $token) {
if ($token !== '' && \strcasecmp($token, 'deprecation') === 0) {
return $link;
}
}
}

Copilot uses AI. Check for mistakes.
Comment on lines +16 to +18
$client = new DeprecationDetectorClient(new MockHttpClient(new MockResponse('', [
'response_headers' => ['Deprecation' => '@1718236800'],
])));

Copilot AI Apr 2, 2026

Copy link

Choose a reason for hiding this comment

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

These tests build MockResponse with response_headers as an associative array (['Deprecation' => '...']). Symfony’s documented MockResponse format is a list of raw header lines (and it’s the only format that consistently supports multiple headers with the same name).

To avoid coupling the tests to MockResponse’s internal normalization quirks across Symfony versions, prefer using the canonical list-of-strings form (e.g. ['HTTP/1.1 200 OK', 'Deprecation: ...']).

Copilot uses AI. Check for mistakes.
Comment thread composer.json Resolved

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

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


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

Comment on lines +59 to +62
}

trigger_deprecation('', '', implode(' ', $parts));
}

Copilot AI Apr 2, 2026

Copy link

Choose a reason for hiding this comment

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

trigger_deprecation() is called with empty package and version strings. This makes the emitted deprecation harder to attribute/filter and can produce awkward messages in tooling expecting a package/version. Pass the actual package name (e.g., tiime/api-deprecation-bundle) and a meaningful version string (or a constant) instead of empty values.

Copilot uses AI. Check for mistakes.
Comment on lines +47 to +51
$parts = [\sprintf('%s %s is deprecated.', $method, $url)];

if ($deprecation !== null) {
$parts[] = \sprintf('Deprecated since: %s.', $deprecation);
}

Copilot AI Apr 2, 2026

Copy link

Choose a reason for hiding this comment

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

The deprecation message includes the full request URL verbatim. Since URLs can contain sensitive data (query params, userinfo), this can leak secrets into logs/error handlers when trigger_deprecation() is captured. Consider sanitizing the URL before including it (e.g., stripping userinfo and query/fragment, or logging only method + host + path).

Copilot uses AI. Check for mistakes.
Comment on lines +40 to +50
public function testTriggersDeprecationOnLinkHeaderWithDeprecationRel(): void
{
$client = new DeprecationDetectorClient(new MockHttpClient(new MockResponse('', [
'response_headers' => ['Link' => '</api/v2/users>; rel="deprecation"; type="text/html"'],
])));

$deprecation = $this->catchDeprecation(fn () => $client->request('GET', 'https://api.example.com/users')->getHeaders());

self::assertNotNull($deprecation);
self::assertStringContainsString('rel="deprecation"', $deprecation);
}

Copilot AI Apr 2, 2026

Copy link

Choose a reason for hiding this comment

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

The test fixtures/assertions use escaped quotes in Link headers (rel=\"deprecation\" / rel=\"next\"). In PHP single-quoted strings the backslashes are preserved, so this doesn’t represent a real HTTP header value and will mask the production bug in the matching logic. Update these to use actual header syntax (rel="deprecation", rel="next" without backslashes).

Copilot uses AI. Check for mistakes.
Comment thread composer.json
Comment on lines 11 to 13
"symfony/dependency-injection": "^6.4|^7.0|^8.0",
"symfony/deprecation-contracts": "^3.6",
"symfony/event-dispatcher-contracts": "^3.0",

Copilot AI Apr 2, 2026

Copy link

Choose a reason for hiding this comment

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

PR description says Infection thresholds are 80%, but the repository’s infection.json5 config enforces minMsi/minCoveredMsi = 95. Please update the PR description (or the config/claim) so they’re consistent.

Copilot uses AI. Check for mistakes.
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