API revisions

Adding, removing, new types

Fields that appear or disappear across revisions, and resources that change type.

Removing and adding a field

// Revisions
// removed in UsersReplaceTeachers
'full_name' => $this->string(
    fn(User $user) => "{$user->first_name} {$user->last_name}",
)
    ->until(ApiRevision::Labels),
// added in UsersReplaceTeachers
'tag_names' => $this->arrayOf(
    $this->string(),
    fn(User $user) => $user->tags->pluck('name')->all(),
)
    ->since(ApiRevision::UsersReplaceTeachers),
  • until($revision), on an attribute, a relationship or a meta member: the field exists up to $revision included. It stays in the head hook until that revision is retired; keep these fields at the end of the hook, under a // Revisions comment.
  • since($revision): the field exists from $revision included. An addition breaks no client, so since() is optional; write it for an exact documentation of each revision, and when a field reuses the name of an older one.

Reusing a name

A field never changes kind: an attribute that becomes a relationship is an until() on one and a since() on the other. Attributes and relationships share one name space, checked revision by revision: a name may be reused by a field of the other kind if they coexist in no revision.

A new type

A resource never changes type: a new type is a new resource, absent from the older revisions, and the old resource is kept as it was for them.

use Ark4ne\JsonApi\Definition\JsonApiSince;
use Ark4ne\JsonApi\Definition\JsonApiUntil;

#[JsonApiType('users')]
#[JsonApiSince(ApiRevision::UsersReplaceTeachers)]
final class UserResource extends JsonApiResource { /* … */ }

#[JsonApiType('teachers')]
#[JsonApiUntil(ApiRevision::Labels)]
final class TeacherResource extends JsonApiResource
{
    /* the old resource, as it was */
}

Every relationship reaching the new resource needs a was() to the old one for the older revisions (see Changing a field).

  • Rendering a resource at a revision it does not exist at throws RenderingException::notInRevision().
  • php artisan jsonapi:check reports a relationship that would do so (jsonapi-absent-resource), a resource of a morph map included.
  • The same model reached as two old types is included once per type.