Skip to content

Version changes ​

A version change is a type implementing VersionChange, registered under the version that introduced it with Versions::version. Handlers only ever see the latest shape: each change upgrades older clients' request bodies and downgrades responses back to the shape they were built against.

rust
use fusion_stack::versioning::{VersionChange, document};
use serde_json::{Map, Value};

pub struct RenameCustomerNameToFullName;

impl VersionChange for RenameCustomerNameToFullName {
    fn description(&self) -> &str {
        "customer.name renamed to customer.full_name"
    }

    fn resources(&self) -> &[&'static str] {
        &["customers"]
    }

    fn transform_request(&self, payload: &mut Map<String, Value>) {
        let mut body = Value::Object(std::mem::take(payload));
        document::rename_attribute(&mut body, self.resources(), "name", "full_name");
        if let Value::Object(map) = body {
            *payload = map;
        }
    }

    fn transform_response(&self, document: &mut Value) {
        document::rename_attribute(document, self.resources(), "full_name", "name");
    }
}

Properties ​

PHP's properties are trait methods with defaults. Laravel for Rust routes have no names, so a route is named by its method and URI pattern, such as GET /v1/orders/{order}/summary, and * matches anything.

PHP propertyRust methodEffect
$descriptiondescription()The line in api:version:list and the changelog. Required.
$resourcesresources()The types or route patterns the transforms apply to. Empty means every response.
$addedRoutesadded_routes()Older versions get a 404.
$removedRoutesremoved_routes()This version and newer get a 404.
$unavailableToOlderVersionsunavailable_to_older_versions()Older versions get a 410 endpoint_unavailable_for_version.
$legacyControllerslegacy_handlers()Older versions are served by a frozen handler.

Methods ​

transform_request() ​

Upgrades a request body from the older shape to this version's shape, in place. It runs for each change between the client's version and the latest, oldest first.

transform_response() ​

Downgrades a response document from this version's shape to the older shape, in place, newest change first. It also runs on error documents, so pointers can follow renamed fields, and on webhook payloads for endpoints pinned to older versions.

Helpers ​

The fusion_stack::versioning::document module works on JSON:API documents and on plain JSON objects, which count as one resource's attributes. Each takes the types to act on; pass self.resources().

HelperWhat it doesPHP helper
types(&document)The resource types in data and included.Document::types
map_attributes(&mut document, types, callback)Runs a callback on each matching resource's attributes.mapAttributes
rename_key(&mut map, from, to)Renames a key in place, keeping the key order.
rename_attribute(&mut document, types, from, to)Renames an attribute, and moves error pointers that name it.renameAttribute()
remove_attribute(&mut document, types, name)Removes an attribute.removeAttribute()
add_attribute(&mut document, types, name, value)Adds an attribute where it is missing, computed from the others.addAttribute()

PHP's moveAttribute(), renameType(), renameRelationship(), mapEnumValues(), nestAttributes() and flattenAttributes() are short map_attributes callbacks in Rust:

rust
fn transform_response(&self, document: &mut Value) {
    // mapEnumValues: `shipped` was called `sent` before this version.
    document::map_attributes(document, self.resources(), |attributes| {
        if attributes.get("status").and_then(Value::as_str) == Some("shipped") {
            attributes.insert("status".into(), "sent".into());
        }
    });
}

Legacy handlers ​

When a handler changes too much to transform, keep the old one for older clients:

rust
use fusion_stack::versioning::{LegacyHandler, legacy};

fn legacy_handlers(&self) -> Vec<(&'static str, LegacyHandler)> {
    vec![("GET /v1/orders/{order}/invoice", legacy(crate::api::legacy_order_invoice))]
}

Testing ​

fusion_stack::testing runs one change on its own: upgrade_request, downgrade_response and assert_round_trip. See Testing helpers.

Released under the MIT License.