API revisions

Checking & retiring a revision

Check every revision in CI, freeze their contract, and retire the oldest one mechanically.

Checking every revision

php artisan jsonapi:check checks the resources at every revision: their definition, then the check of ark4ne/laravel-api-revisions, which reads the history of their fields (a conversion that is missing, a was() outside the lifetime of its field…), and the rules of JSON:API this package adds:

CodeCause
jsonapi-reserved-namea field named id or type at a revision
jsonapi-name-collisionan attribute and a relationship sharing a name at a revision
jsonapi-absent-resourcea relationship to a resource absent from the revision

--revision=Labels keeps the findings of one revision. php artisan revision:check runs the same check on every declaring class of the application (requests included) and compares them with the lock.

In the test suite:

use Ark4ne\JsonApi\Testing\ResourceAssertions;

ResourceAssertions::assertValidAtEveryRevision(UserResource::class);
ResourceAssertions::assertValid(
    UserResource::class,
    revision: ApiRevision::Labels,
);

Caching

php artisan jsonapi:cache compiles the definitions of every revision and php artisan revision:cache their shapes; php artisan optimize runs both. An old client then costs one OPcache read, as the head does.

A contract test

ark4ne/laravel-api-revisions freezes the shapes of every revision in a lock file (php artisan revision:lock), which revision:check compares with the code: a change of what an old client receives fails the check until the lock is written again.

The lock holds the values. To freeze what is JSON:API too (related resources, links, loads), compare the definitions of each supported revision on every change:

tests/Feature/ContractTest.php
use Ark4ne\JsonApi\Compilation\Definitions;
use Ark4ne\JsonApi\Testing\ResourceAssertions;

public function test_the_contract_of_every_revision_is_frozen(): void
{
    foreach (ApiRevision::cases() as $revision) {
        foreach ([UserResource::class, PostResource::class] as $resource) {
            if (!Definitions::exists($resource, $revision)) {
                continue;
            }

            $file = __DIR__ . "/contracts/{$revision->name}/"
                . class_basename($resource) . '.php';
            $definition = ResourceAssertions::definitionAt(
                $resource,
                $revision,
            );

            $this->assertSame(
                file_get_contents($file),
                var_export($definition, true),
            );
        }
    }
}

A change of an old revision then shows in the diff of the review; a change of the head only touches the head.

Retiring a revision

Bounds are inclusive, so retiring the oldest revision is mechanical:

Delete its case

Remove Initial from the enum and from order().

Run the static analysis

composer stan (Larastan) reports every declaration that served only its clients: was(Initial), until(Initial), #[JsonApiUntil(Initial)].

Delete what it reports

Every reported line, and every resource marked #[JsonApiUntil(Initial)].

Check

php artisan jsonapi:check and php artisan revision:check report what the removal left inconsistent.