diff --git a/README.md b/README.md index 77f90fd..90102eb 100644 --- a/README.md +++ b/README.md @@ -390,6 +390,22 @@ Link: ; rel="sunset" - Error responses get the headers too, because both describe the resource, not the outcome of one request. +A URI's query is part of what identifies the resource (RFC 3986 section 3), so a query form of a +route is a resource of its own, and can be deprecated while the route is not. Give the attributes +the query parameter, and they apply only to requests that carry it: + +```php +#[Deprecation(at: '2026-10-29T00:00:00Z', link: 'https://docs.example.com/deprecations/contact-filter', query: 'organization_id')] +#[Sunset(at: '2027-01-31T23:59:59Z', query: 'organization_id')] +final class ListContactsController +{ + // GET /contacts?organization_id=... is deprecated; GET /contacts is not. +} +``` + +Both attributes are repeatable, so one controller can carry a notice for itself and another for a +query form. A notice for a query form the request carries wins over one without a query. + The attributes are named after the header fields because `#[Deprecated]` is taken: PHP 8.4's native `#[\Deprecated]` raises `E_USER_DEPRECATED` every time the method is called, which on a controller means on every request. diff --git a/resources/boost/skills/api-toolbox-development/references/api-lifecycle.md b/resources/boost/skills/api-toolbox-development/references/api-lifecycle.md index 31368ec..41004e6 100644 --- a/resources/boost/skills/api-toolbox-development/references/api-lifecycle.md +++ b/resources/boost/skills/api-toolbox-development/references/api-lifecycle.md @@ -21,6 +21,7 @@ final class OrderController - Only controller routes are read. Closure routes are not, because `route:cache` breaks their attributes. Move a closure route to a controller to deprecate it. - A date without an offset is UTC. A deprecation date can be in the future. A sunset earlier than the deprecation throws. - `#[Deprecation(link: ...)]` with no date publishes a deprecation policy only. +- To retire a query form of a route, such as `/contacts?organization_id=...`, while the route stays, pass `query: 'organization_id'` to both attributes. They are repeatable, and the query form's notice wins when the request carries the parameter. A field has no header: deprecate it in the OpenAPI document instead. - With the `global` option on (the default), nothing is added to routes. With it off, add the `deprecation` middleware. - Reading deprecations from elsewhere, such as a manifest: bind `JustSteveKing\Toolbox\Deprecation\Contracts\DeprecationSource`, which returns a `JustSteveKing\Toolbox\Deprecation\Notice` for a route. diff --git a/src/Deprecation/AttributeSource.php b/src/Deprecation/AttributeSource.php index 30f1e15..858ace2 100644 --- a/src/Deprecation/AttributeSource.php +++ b/src/Deprecation/AttributeSource.php @@ -4,6 +4,8 @@ namespace JustSteveKing\Toolbox\Deprecation; +use Illuminate\Container\Container; +use Illuminate\Http\Request; use Illuminate\Routing\Route; use JustSteveKing\Toolbox\Deprecation\Attributes\Deprecation; use JustSteveKing\Toolbox\Deprecation\Attributes\Sunset; @@ -15,7 +17,9 @@ * Reads #[Deprecation] and #[Sunset] from a route's controller and action. * * An attribute on the action wins over the same attribute on the controller; the two kinds are - * resolved separately, so a controller-wide Sunset can sit beside a per-action Deprecation. + * resolved separately, so a controller-wide Sunset can sit beside a per-action Deprecation. An + * attribute with a query applies only when the request carries that query parameter, and wins + * over one without. * * Only controller routes are read, as with Laravel's own #[Middleware] controller attributes. * Closure routes are not, because `route:cache` serialises closures and their attributes cannot @@ -24,7 +28,9 @@ final class AttributeSource implements DeprecationSource { /** - * @var array + * The attributes on each action, read once, by kind; the notice itself depends on the request. + * + * @var array, sunset: list}> */ private array $resolved = []; @@ -43,21 +49,22 @@ public function forRoute(Route $route): ?Notice $key = $class.'@'.$method; if (! array_key_exists($key, $this->resolved)) { - $this->resolved[$key] = $this->read(new ReflectionClass($class), $method); - } + $reflection = new ReflectionClass($class); + $action = $reflection->hasMethod($method) ? $reflection->getMethod($method) : null; - return $this->resolved[$key]; - } + $this->resolved[$key] = [ + 'deprecation' => $this->attributes(Deprecation::class, $reflection, $action), + 'sunset' => $this->attributes(Sunset::class, $reflection, $action), + ]; + } - /** - * @param ReflectionClass $class - */ - private function read(ReflectionClass $class, string $method): ?Notice - { - $action = $class->hasMethod($method) ? $class->getMethod($method) : null; + // Read when asked rather than held: this source is a singleton, and under Octane one + // instance serves many requests. + $request = Container::getInstance()->bound('request') ? Container::getInstance()->make('request') : null; + $parameters = $request instanceof Request ? array_map(strval(...), $request->query->keys()) : []; - $deprecation = $this->attribute(Deprecation::class, $class, $action); - $sunset = $this->attribute(Sunset::class, $class, $action); + $deprecation = $this->choose($this->resolved[$key]['deprecation'], $parameters); + $sunset = $this->choose($this->resolved[$key]['sunset'], $parameters); if ($deprecation === null && $sunset === null) { return null; @@ -72,22 +79,52 @@ private function read(ReflectionClass $class, string $method): ?Notice } /** + * The attribute that applies to this request: one scoped to a query parameter the request + * carries, or else one for the route itself. Attributes come action first, so within each of + * those an action's attribute wins over its controller's. + * + * @template T of Deprecation|Sunset + * + * @param list $attributes + * @param list $parameters the query parameters the request carries + * @return T|null + */ + private function choose(array $attributes, array $parameters): ?object + { + foreach ($attributes as $attribute) { + if ($attribute->query !== null && in_array($attribute->query, $parameters, true)) { + return $attribute; + } + } + + foreach ($attributes as $attribute) { + if ($attribute->query === null) { + return $attribute; + } + } + + return null; + } + + /** + * Every attribute of a kind, the action's before its controller's. + * * @template T of object * * @param class-string $attribute * @param ReflectionClass $class - * @return T|null + * @return list */ - private function attribute(string $attribute, ReflectionClass $class, ?ReflectionMethod $action): ?object + private function attributes(string $attribute, ReflectionClass $class, ?ReflectionMethod $action): array { - foreach ([$action, $class] as $target) { - $found = $target?->getAttributes($attribute) ?? []; + $found = []; - if ($found !== []) { - return $found[0]->newInstance(); + foreach ([$action, $class] as $target) { + foreach ($target?->getAttributes($attribute) ?? [] as $reflected) { + $found[] = $reflected->newInstance(); } } - return null; + return $found; } } diff --git a/src/Deprecation/Attributes/Deprecation.php b/src/Deprecation/Attributes/Deprecation.php index bec349f..00e29c0 100644 --- a/src/Deprecation/Attributes/Deprecation.php +++ b/src/Deprecation/Attributes/Deprecation.php @@ -17,12 +17,21 @@ * $at may be in the future: section 2.1 says the date says when the resource "will be or has * been deprecated". A date without an offset is read as UTC. With no $at, only the Link is sent, * which section 3.1 describes as publishing a deprecation policy before deprecating anything. + * + * With $query, it applies only to requests that carry that query parameter. A URI's query is part + * of what identifies the resource (RFC 3986, section 3), so /contacts?organization_id=X is a + * resource of its own, and can be deprecated while /contacts is not: + * + * #[Deprecation(at: '2026-10-29T00:00:00Z', query: 'organization_id')] + * + * Repeatable, so one controller can carry a notice for the route and another for a query form. */ -#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)] +#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] final readonly class Deprecation { public function __construct( public ?string $at = null, public ?string $link = null, + public ?string $query = null, ) {} } diff --git a/src/Deprecation/Attributes/Sunset.php b/src/Deprecation/Attributes/Sunset.php index f591b95..a7b73be 100644 --- a/src/Deprecation/Attributes/Sunset.php +++ b/src/Deprecation/Attributes/Sunset.php @@ -12,12 +12,21 @@ * #[Sunset(at: '2027-01-01T00:00:00Z', link: 'https://docs.example.com/sunset-policy')] * * A date without an offset is read as UTC. + * + * With $query, it applies only to requests that carry that query parameter. A URI's query is part + * of what identifies the resource (RFC 3986, section 3), so /contacts?organization_id=X is a + * resource of its own, and can be deprecated while /contacts is not: + * + * #[Sunset(at: '2027-01-31T23:59:59Z', query: 'organization_id')] + * + * Repeatable, so one controller can carry a notice for the route and another for a query form. */ -#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)] +#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] final readonly class Sunset { public function __construct( public string $at, public ?string $link = null, + public ?string $query = null, ) {} } diff --git a/tests/Feature/DeprecationTest.php b/tests/Feature/DeprecationTest.php index 860b021..f4fce45 100644 --- a/tests/Feature/DeprecationTest.php +++ b/tests/Feature/DeprecationTest.php @@ -7,6 +7,7 @@ use JustSteveKing\Toolbox\Deprecation\Contracts\DeprecationSource; use JustSteveKing\Toolbox\Deprecation\Notice; use JustSteveKing\Toolbox\Tests\Fixtures\Controllers\CurrentController; +use JustSteveKing\Toolbox\Tests\Fixtures\Controllers\FilteredContactsController; use JustSteveKing\Toolbox\Tests\Fixtures\Controllers\LegacyOrdersController; use JustSteveKing\Toolbox\Tests\Fixtures\Controllers\MisconfiguredController; use JustSteveKing\Toolbox\Tests\Fixtures\Controllers\OffsetController; @@ -119,3 +120,27 @@ public function forRoute(RoutingRoute $route): ?Notice expect($this->get('api/orders')->headers->all('link'))->toHaveCount(2); }); + +it('deprecates a query form of a route on its own, since its URI is a resource of its own', function (): void { + Route::get('api/contacts', [FilteredContactsController::class, 'index']); + + $filtered = $this->get('api/contacts?organization_id=42')->assertOk(); + + expect($filtered->headers->get('Deprecation'))->toBe('@1793232000') + ->and($filtered->headers->get('Sunset'))->toBe('Sun, 31 Jan 2027 23:59:59 GMT') + ->and($filtered->headers->all('link'))->toBe(['; rel="deprecation"']); + + $plain = $this->get('api/contacts?search=ada')->assertOk(); + + expect($plain->headers->has('Deprecation'))->toBeFalse() + ->and($plain->headers->has('Sunset'))->toBeFalse() + ->and($plain->headers->has('Link'))->toBeFalse(); +}); + +it('prefers a query form\'s notice to the route\'s own, and falls back to it without the query', function (): void { + Route::get('api/contacts/export', [FilteredContactsController::class, 'export']); + + // The action's own Deprecation applies without the query; with it, the class's scoped one does. + expect($this->get('api/contacts/export')->headers->get('Deprecation'))->toBe('@1767225600') + ->and($this->get('api/contacts/export?organization_id=42')->headers->get('Deprecation'))->toBe('@1793232000'); +}); diff --git a/tests/Fixtures/Controllers/FilteredContactsController.php b/tests/Fixtures/Controllers/FilteredContactsController.php new file mode 100644 index 0000000..f5e7bd5 --- /dev/null +++ b/tests/Fixtures/Controllers/FilteredContactsController.php @@ -0,0 +1,27 @@ +