Evolving the API

Adding and removing fields

Fields that appear or disappear in a revision, alone or in groups, and the defaults of requests.

Add a field

since(R): the field exists from R included. Older clients do not get it in responses, and never send it in requests.

'tags' => Schema::list(Schema::string())->since(ApiRevision::Spring26),

In a request, give default:: what the controller receives when an older client cannot send the field.

'tags' => Schema::list(Schema::string())
    ->since(ApiRevision::Spring26, default: []),

A required() field added later must declare its default, or the check fails (required-since-without-default). default: null gives an explicit null. Setting missing_default to 'null' in the configuration makes null the default of every such field.

Remove a field

until(R): the field exists up to R included. The field stays in the head declaration, with its own source, until R is retired.

'name' => Schema::string(
    fn($value, Order $order): string
        => "{$order->first_name} {$order->last_name}",
)
    ->until(ApiRevision::Winter25),

In a request, the removed field is validated for older clients, then dropped from the input once the upgrade passes the revision it was removed in. Use the upgrade hook to carry its value over to the fields that replace it.

Groups

Schema::since() and Schema::until() apply one bound to several fields. They return an array: spread it with ....

...Schema::since(ApiRevision::Spring26, [
    'first_name' => Schema::string()->required(),
    'last_name' => Schema::string()->required(),
], default: null),

...Schema::until(ApiRevision::Winter25, [
    'name' => Schema::string()->required(),
]),

A member may declare a newer since() or an older until() than its group, never the reverse (since-older-than-group, until-newer-than-group). Keep removed fields at the end of the declaration, so the head reads first.

Split a field

A field replaced by others combines both: the new fields since(N), the old one until(N->previous()). In a response, each reads its own source. In a request, the upgrade() hook fills the new fields from the old one:

protected function upgrade(Revision $to, array $input): array
{
    if ($to === ApiRevision::Spring26 && is_string($input['name'] ?? null)) {
        [$input['first_name'], $input['last_name']]
            = explode(' ', $input['name'], 2) + [1 => ''];
    }

    return $input;
}