Skip to content

Version changes ​

A version change is one breaking change: a class extending FusionStack\Laravel\Versioning\VersionChange, listed under the version that introduced it in api.versions.changes. php artisan api:version:make creates and registers one. For why and when to write one, see Versioning.

php
use FusionStack\Laravel\Versioning\RequestContext;
use FusionStack\Laravel\Versioning\ResponseContext;
use FusionStack\Laravel\Versioning\VersionChange;

final class RenameCustomerNameToFullName extends VersionChange
{
    public string $description = 'customer.name renamed to customer.full_name';

    public array $resources = ['customers'];

    public function transformRequest(array $payload, RequestContext $context): array
    {
        return $this->renameAttribute($payload, 'name', 'full_name');
    }

    public function transformResponse(array $document, ResponseContext $context): array
    {
        return $this->renameAttribute($document, 'full_name', 'name');
    }
}

Your controllers only ever see the latest shape. A request from an older client runs through each newer change's transformRequest(), oldest first, before it reaches the controller. The response runs back through each transformResponse(), newest first.

Properties ​

PropertyTypeWhat it is
$descriptionstringWhat changed, written for API consumers. It appears in api:version:list, api:changelog and the API reference's changelog.
$resourceslist<string>The resource types, or route names with patterns such as orders.*, this change touches. Requests and responses matching none of them skip it. Empty means every one.
$addedRouteslist<string>Route names (patterns allowed) this version added. Older versions get a 404, as if the route did not exist.
$removedRouteslist<string>Route names (patterns allowed) this version removed. They stay in your routes for older clients; this version and newer get a 404.
$unavailableToOlderVersionslist<string>Route names (patterns allowed) whose responses cannot be translated to the old shape. Older versions get 410 endpoint_unavailable_for_version for them; the rest of the API keeps working.
$legacyControllersarray<string, action>A last resort for an endpoint whose change cannot be a transform: route name ⇒ a frozen action that serves older versions. See below.

Methods ​

transformRequest() ​

php
public function transformRequest(array $payload, RequestContext $context): array

Migrates a request body from the shape before this change to the shape after it. Override it when the change touches what clients send.

Query parameters that name fields, such as fields[customers]=name, sort=-name or filter[name]=…, are migrated with the same method, so you do not transform them separately.

transformResponse() ​

php
public function transformResponse(array $document, ResponseContext $context): array

Migrates a response document back to the shape before this change. It also runs on error documents, so the helpers below move source.pointer along with the attributes they rename.

Both methods must be pure array operations: no database queries and no container lookups. Their contexts carry the request and the client's version; ResponseContext also has the response's status.

Helpers ​

Each helper takes the document or payload and returns the changed copy. They act on the resource objects in data and included whose type is in $resources, or on all of them when the change matched by route name alone. In a plain JSON payload, the attributes are the top-level keys.

renameAttribute() ​

php
$this->renameAttribute($document, 'full_name', 'name');

Renames an attribute. Error pointers follow it: /data/attributes/full_name becomes /data/attributes/name.

removeAttribute() ​

php
$this->removeAttribute($payload, 'legacy_flag');

Removes an attribute, such as one older clients still send.

addAttribute() ​

php
$this->addAttribute($document, 'total', fn (array $attributes) => $attributes['subtotal'] + $attributes['tax']);

Adds an attribute when it is missing, such as one a newer version removed. The value can be a closure that computes it from the resource's attributes.

moveAttribute() ​

php
$this->moveAttribute($document, 'address.postcode', 'postcode');

Moves an attribute to another path, with dot notation for nested attributes.

renameType() ​

php
$this->renameType($document, 'carts', 'baskets');

Renames a resource type, in resource objects and in relationship linkage, whatever $resources lists.

renameRelationship() ​

php
$this->renameRelationship($document, 'buyer', 'customer');

Renames a relationship. Error pointers under /data/relationships/ follow it.

mapEnumValues() ​

php
$this->mapEnumValues($document, 'status', ['dispatched' => 'shipped']);

Replaces an attribute's values. Values missing from the map are kept.

nestAttributes() ​

php
$this->nestAttributes($payload, ['street', 'city', 'postcode' => 'postal_code'], 'address');

Moves top-level attributes into an object attribute. Give a name, or attribute => nested name to rename it on the way.

flattenAttributes() ​

php
$this->flattenAttributes($document, 'address');

The reverse: moves an object attribute's members to the top level and removes the object. Pass a list of members, or member => attribute name, to move only some; empty moves every member.

Legacy controllers ​

php
public array $legacyControllers = [
    'orders.invoice' => [LegacyInvoiceController::class, 'show'],
];

Older versions get the frozen action instead of the route's own, with the same route parameters after binding. It speaks the shape from before this version, so only older changes still transform its requests and responses. When several versions freeze a route, the oldest serves the versions before it. An action is an invokable controller class, [Controller::class, 'method'] or 'Controller@method'.

Frozen code must be kept working for as long as those versions are supported, so prefer a transform or $unavailableToOlderVersions.

Testing ​

TransformsVersionChanges runs a change's transforms on a document in a test, without a request.

Released under the MIT License.