Upgrade guide
Upgrade guide
From 2.0 to 3.0
The design behind these changes is docs/rfc/api-revisions-integration.md.
Requirements
ark4ne/laravel-api-revisions ^1.0 is required and installed with the package; JsonApiServiceProvider registers its
provider. An application that declares no revision configures nothing: it keeps one shape, the head.
Values are declared, and rendered by ark4ne/laravel-api-revisions
The helpers keep their names and return the descriptors of the base. What changes in a resource:
| 2.0 | 3.0 |
|---|---|
'name' => $this->resource->name (a plain value) | 'name' => $this->string(), or a closure: 'name' => fn() => $this->resource->name |
toResourceMeta() returning ['version' => 3] | ['version' => fn() => 3], or $this->integer(fn() => 3) |
a member without name ($this->string('email')) | 'email' => $this->string() |
$this->when($condition, $value), $this->whenHas('email') in a hook | $this->string()->when(fn() => …), ->whenHas() |
$this->mergeWhen($condition, [...]) | $this->applyWhen(fn() => $condition, [...]) |
$this->applyWhen($bool, [...]) | $this->applyWhen(fn() => $bool, [...]): a closure, read for each resource |
->when(true), ->when(fn(Request $request, $model, string $attribute) => …) | ->when(fn($model, string $attribute) => …), run before the value as in 2.0; the request is the one of the hook (use ($request)) |
$this->whenInFields($request, 'name', $value) | the value is a descriptor or a closure: $this->whenInFields($request, 'name', fn() => $value) |
$this->enum()->of(State::class), $this->enum() | $this->enum(State::class) (a backed enum) |
$this->struct(fn() => ['city' => $this->string()]) | $this->struct(['city' => $this->string()]); with a source, the members are read on its value |
$this->array()->of($this->string()), ->of('money') | $this->arrayOf($this->string()), $this->arrayOf($this->money()) |
$this->float()->precision(1), ->truncate() | unchanged: $this->float() returns Descriptors\Schemas\FloatSchema, a float of the base that rounds |
->nullable(false) rendering 0 or '' for null | null is a value error (revisions.strict_values) |
->was(X, value: $this->integer()) | ->was(X, schema: $this->integer()), with down: when the base knows no conversion |
->was(X, value: $this->one(OldResource::class)) on a relationship | ->was(X, schema: $this->one(OldResource::class)) |
a descriptor with was() in toLinks() | a plain link; compute it with ApiRevision::current() if it changes |
protected static function toDefinition() | removed: the definition is compiled from the hooks |
JsonApi::valueType('money', MoneyValue::class), describer.types | a Descriptor of the base registered with TypeRegistry::register('money', MoneySchema::class) |
describer.datetime, describer.strict | date_time_format, strict_values of config/revisions.php |
The hooks of values run once per response, on a template instance whose $this->resource is set to each model
before its values are read: a closure reading $this->resource works as before, an expression evaluated in the hook
($this->resource->name, $request->user()->isAdmin() as a plain boolean) is the same for every resource and is
refused where it would be a value. php artisan jsonapi:check names every member to rewrite.
Definitions
Definitions::for($class, $revision) gives one definition per revision; ResourceDefinition::$revision is null for
the head (the newest revision). Its values are the model of ark4ne/laravel-api-revisions: $attributes, $meta
and $documentMeta are Shapes (null where the resource has no values), attribute($name) a FieldDefinition
read by its name at the revision. Definition\AttributeDefinition and ValueType are removed:
| 2.0 | 3.0 |
|---|---|
array_keys($definition->attributes) | $definition->attributes?->names() |
$attribute->type === ValueType::String | $attribute->type->kind === Kind::String (Ark4ne\ApiRevisions\Definition\Kind) |
$attribute->format, ->enum, ->items, ->children | $attribute->type->format, ->enum, ->items, ->children |
a custom type: its ValueType and format | Kind::Custom, $attribute->type->name and its JSON Schema ->fragment |
$attribute->sparse | $definition->isSparse($name) |
$definition->links (definitions by name) | $definition->links, the names |
$attribute->source, ->computed | removed |
jsonapi:cache writes every revision, without the shapes (those of the base): run it again after the upgrade; a file
of 2.0 is ignored.
php artisan jsonapi:check and ResourceAssertions run the check of ark4ne/laravel-api-revisions on the
resources: the history of their fields is checked with its codes, and the diagnostics reserved-name,
name-collision and unavailable-resource are jsonapi-reserved-name, jsonapi-name-collision and
jsonapi-absent-resource. They are no longer among ResourceDefinition::$diagnostics: a tool reading them there
calls Compilation\ResourceChecker. php artisan revision:check checks every declaring class of the application,
requests included, and its lock.
A body that changes across the revisions is declared in the body() of a request using RevisionedRequest
($this->resourceObject(attributes: …, relationships: …), Http\Body): see the README, "Write requests across
revisions".
From 1.x to 2.0
This section gives the code of 2.0. Upgrading from 1.x to 3.0, apply it, then "From 2.0 to 3.0" above, whose tables replace what 3.0 changed again (values, value types, configuration of the dates, definitions).
Coding agents can follow the laravel-json-api-upgrade skill shipped in the package
(vendor/ark4ne/laravel-json-api/skills/laravel-json-api-upgrade/): the same changes as a procedure, with a mapping
of every 1.x construct and tested examples.
Requirements
PHP ^8.3 and Laravel ^12.46 or ^13.0.
Descriptors are immutable
Every fluent method of a descriptor returns a copy. Chained calls are unchanged; a descriptor modified on its own line must keep the returned value:
// 1.x
$title = $this->string();
$title->nullable(false);
// 2.0
$title = $this->string()->nullable(false);
Service provider
The provider is now Ark4ne\JsonApi\Laravel\JsonApiServiceProvider. Package discovery registers it; if your
application lists Ark4ne\JsonApi\Providers\LaravelJsonApiProvider in bootstrap/providers.php or
config/app.php, replace it:
// 1.x
Ark4ne\JsonApi\Providers\LaravelJsonApiProvider::class,
// 2.0
Ark4ne\JsonApi\Laravel\JsonApiServiceProvider::class,
Configuration
config/jsonapi.php is validated at boot: a value of the wrong type throws an InvalidConfigurationException that
names the key. Compare your published file with the package one for the new keys.
Values
$this->date()renders a date without time (2026-10-31,Y-m-d); a date and time is$this->datetime(), in the format ofdescriber.datetime(ex-describer.date, ATOM by default). Replace->date(by->datetime(wherever the time is rendered: a resource left withdate()loses the time without error. A publishedconfig/jsonapi.phpstill settingdescriber.datethrows anInvalidConfigurationExceptionat boot: rename itdescriber.datetime.Values\ValueDateis the date without time; the 1.x class isValues\ValueDateTime.// 1.x: 2026-10-31T10:00:00+00:00 'published-at' => $this->date(), // 2.0: the same output 'published-at' => $this->datetime(),- A float with a precision is rounded instead of floored:
$this->float()->precision(2)renders1.24for1.239(1.x:1.23),1.15for1.15(1.x:1.14). Add->truncate()to truncate. - A date declared
nullable(false)rendersnullfornull(1.x:1970-01-01T00:00:00+00:00). Withdescriber.strict, true by default whenAPP_DEBUGis, it throws aRenderingExceptioninstead: make the value nullable, or give it a value. enum()->of(State::class)validates the value: a raw value is cast through the enum, and a value that is not one of its cases throws aRenderingException. Removeof(), or fix the data, where a value outside the enum is expected.- A custom value extending
Descriptors\Values\Valuereceives the configuration:protected function value(mixed $of, Request $request, Configuration $configuration): mixedSupport\Configis removed: read the defaults ($configuration->nullable,datetimeFormat,precision) from it.
Collections
collection() returns a JsonApiCollection, no longer an anonymous subclass. $preserveKeys is ignored: data is a
JSON list whatever the keys of the collection. To return a collection class of your own, override collection():
public static function collection($resource): UserCollection
{
return new UserCollection($resource);
}
float() is protected, like the other value helpers: call it from the resource only.
Relationships and loading
relationship.when-includedis replaced byrelationship.linkage:'included'fortrue; remove the key forfalse('always', the default). A published configuration keeping the old key throws at boot.- A relationship read from a relation (
$this->one(UserResource::class)) loads it, in batch, even when it is not included. Add->withLoad(false)where an application loads it itself, or->whenIncluded()to render itsdataonly when included.// 1.x: the relation is read as it is, loaded resource by resource if it is not 'author' => $this->one(UserResource::class), // 2.0: loaded in batch; to load it yourself, or render it only when included 'author' => $this->one(UserResource::class)->withLoad(false), 'author' => $this->one(UserResource::class)->whenIncluded(), ->withLoad(true)loads the relation the relationship is read from:$this->one(PostResource::class, 'post')under the namemain-postloadspost(1.x:main-post).- A
withLoad(['comments' => fn($query) => …])constraint now filters the linkage too: the identifiers listed indataare those ofincluded. - A relationship with
->whenLoaded()is loaded when included. Add->withLoad(false)to keep the 1.x behaviour. whenIncluded($request, $type, $value)of the resources is renamedwhenInIncludes($request, $relationship, $value).RelationshipDefinition::$whenIncludedis$linkage(Configuration\Linkage|null).RelationshipDefinition::$relatedis a list of resource classes ($relationship->related[0]for a relationship that is not polymorphic), empty for a relationship to Laravel's native resources (isNative(),$native). Runphp artisan jsonapi:cacheagain after upgrading.Filtersis immutable:->filters(fn(Filters $filters) => $filters->can('view'))works as before, but a closure callingcan()/when()without returning their result loses the rules. Chain them and return the result.Relationship::links()islinks(?Closure $links = null, string|Closure|null $self = null, string|Closure|null $related = null): a subclass overridinglinks(Closure $links)must take the new signature.links()without any link throws.
Static helpers
Support\Includes, Support\Fields, Support\With, Support\Supported and Support\Traits\HasLocalCache are
removed, with PrepareData::prepareData() and the Octane RequestReceived listener (each request gets its own
context, nothing to reset). Read the requested include and fields from the context of the request:
// 1.x
Includes::include($request, 'posts');
Fields::has($request, 'title', 'post');
// 2.0
$context = app(JsonApiContext::class);
$context->include->has('posts'); // IncludeTree
$context->fields->allows('post', 'title'); // SparseFieldsets
Validation rules
Rules\Fields and Rules\Includes move to the Http layer: replace Ark4ne\JsonApi\Requests\Rules\ by
Ark4ne\JsonApi\Http\Rules\ in your imports:
// 1.x
use Ark4ne\JsonApi\Requests\Rules\Includes;
// 2.0
use Ark4ne\JsonApi\Http\Rules\Includes;
A form request that only validates include and fields can extend
Ark4ne\JsonApi\Http\JsonApiFormRequest instead (protected string $resource = PostResource::class;).
Validation messages
The messages of Rules\Fields and Rules\Includes are translations of the package. Messages customized under
validation.custom.jsonapi.* in lang/{locale}/validation.php are no longer read: publish the translations
(php artisan vendor:publish --tag=jsonapi-lang) and edit lang/vendor/jsonapi/{locale}/validation.php, whose keys
are the same without the custom.jsonapi. prefix (fields.invalid_fields, includes.invalid_depth…). A test
asserting ":resource" doesn't exists. reads ":resource" doesn't exist. now.
Both rules are final: compose them instead of extending them. Rules\Traits\UseTrans is removed.
Schema and assertions
JsonApiResource::schema(), JsonApiCollection::schema() and Skeleton are removed: PostResource::definition()
returns the ResourceDefinition (type, attributes, relationships with their related class and eager-loads).
Support\FakeModel, internal, is replaced by Support\Probe: code that built a resource on a FakeModel to read
what it declares reads PostResource::definition() instead.
Asserts\AssertJsonApiResource is removed: run php artisan jsonapi:check in the CI, or call
Testing\ResourceAssertions::assertValid(PostResource::class) in a test.
A condition written with an if on the request inside a hook is not seen by the definition, which is built with an
empty request: write it with ->when(), ->unless() or applyWhen() (README, section "Definition").
The members of mergeWhen() and mergeUnless() are missing from the definition whatever the condition, with a
merge-when warning: they are still rendered, but fields and include cannot request them and their relations are
not eager-loaded. Group conditional members with applyWhen():
// 1.x
$this->mergeWhen($this->is_admin, ['secret' => $this->secret]),
// 2.0
$this->applyWhen(fn() => $this->is_admin, ['secret' => $this->string()]),
merge(), mergeWhen() and mergeUnless() throw a DefinitionException when given a scalar or an object that is
neither iterable, Arrayable nor JsonSerializable; null merges nothing.
include and fields come from the query string
1.x read include and fields from all the input, body included. 2.0 reads them from the query string only, where
the specification defines them. A middleware adding a default include on a GET request
($request->merge(['include' => …])) keeps working: Laravel merges into the query of a GET.
toArray() returns the resource object
toArray($request) returns the resource object only (type, id, attributes, relationships, links, meta),
and a collection the list of them. included, the document meta and the pagination are only in the response:
return the resource from the controller, or call ->toResponse($request). with() no longer returns included nor
the meta of toDocumentMeta(); what you add in with() or additional() is still merged into the response. The second
argument of toArray() ($included / $minimal) is removed: remove it from your overrides.
// 1.x
$document = (new PostResource($post))->toArray($request, true);
// 2.0
$document = (new PostResource($post))->toResponse($request)->getData(true);
Resources\Resourceable and Relationship::toArray() are removed. A relationship points to a JsonApiResource or a
JsonApiCollection, and a collection collects JsonApiResources: anything else renders no resource object.
The JSON follows the specification
// 1.x
{"data": {"type": "post", "id": 1, "attributes": [], "relationships": {"author": []}}}
// 2.0
{"jsonapi": {"version": "1.1"}, "data": {"type": "post", "id": "1", "relationships": {"author": {"data": null}}}}
Clients reading the documents must expect:
- a top-level
jsonapimember,{"version": "1.1"}by default. Setdocument.jsonapitofalseinconfig/jsonapi.phpto leave it out; idalways a string, in resource objects and in relationship linkage;{"data": null}for an empty to-one relationship,{"data": []}for an empty to-many; a relationship that has neither data, links nor meta is left out;- without
toAttributes(), noid,type, model key, foreign keys of the declared relationships (user_id…) nor loaded relations inattributes. WritetoAttributes()if a client reads them there; - empty
attributes,relationships,linksormetaleft out instead of[]. A hook returning members without names (toAttributes()returning['a', 'b'],toMeta()returning['x']) now throws aRenderingException: give each member a name; includedwithout the primary resources and without duplicates;fields[type]restricting the relationships of the type too. To keep the 1.x behaviour:// config/jsonapi.php 'fields' => ['relationships' => 'keep'],- document meta (
toDocumentMeta()) from the primary resources only: an included resource adds none. In a collection they are merged key by key, the first resource setting a key keeping it, instead of gathered into lists. To set document meta for the whole collection, overridetoDocumentMeta()on the collection class: it wins over those of the resources.
Closures that are not requested are not run
A closure of an attribute left out by fields[type], or of a relationship whose linkage is not rendered, is never
called: 1.x called it, then dropped the value. Code that relied on the side effects of such a closure (a counter, a
log, a lazy load) must do it elsewhere.
// GET /posts?fields[post]=title
'word-count' => $this->integer(fn() => $this->countWords()), // 1.x: called; 2.0: not called
New value helpers
$this->uuid(), ulid(), decimal(), json(), time() and datetime() are value descriptors now. A resource that
called a model method of one of these names through $this must call it on the model:
// 1.x: the uuid() method of the model, through the resource
'uuid' => $this->uuid(),
// 2.0
'uuid' => $this->resource->uuid(),
Renamed methods
toMeta()(document meta) is renamedtoDocumentMeta(), on resources and collections: a class still declaringtoMeta()throws aDefinitionException(when compiled for a resource, when rendered for a collection) instead of losing its meta silently.toResourceMeta()is unchanged.- On
Relationship(PostResource::relationship(...)),withLinks()andwithMeta()are renamedlinks()andmeta(), as on the relationship descriptors.// 1.x protected function toMeta(Request $request): ?iterable { return ['total' => …]; } PostResource::relationship(fn() => …)->withLinks(fn() => […]); // 2.0 protected function toDocumentMeta(Request $request): ?iterable { return ['total' => …]; } PostResource::relationship(fn() => …)->links(fn() => […]); toId(Request $request): int|string|nullis a hook of the resources, read by the defaulttoIdentifier(): a resource declaring atoId()of another signature must rename it.
Final classes
The descriptors (RelationOne, RelationMany, ValueString and the other values), Relationship, Filters,
CallbackFilterRule and PolicyFilterRule are final. A subclass of one of them must become a wrapper, or a
Closure rule (->when(fn($request, $model, $attribute) => …)). Support\Arr, the Descriptors\Descriptors trait,
Descriptors\Resolver, Traits\HasRelationLoad and RelationMissing are removed: use Laravel's Arr, and
withLoad() on the descriptors and on Relationship.
applyWhen()
- A relationship given to
applyWhen()is now left out when the condition fails (1.x rendered it anyway). - Only a
Closuremember is called to read its value: another callable ('strlen',[$service, 'method']) is rendered as it is. Wrap it in a closure (fn() => $service->method()).
Casts
string(), integer() and float() throw a RenderingException for a value they cannot cast (an array, an object
that is not Stringable), and a resource whose identifier is neither an integer nor a string throws one too.
Static analysis
JsonApiResource<User> now types the descriptors: $this->string() is a ValueString<User>, $this->one(…) a
RelationOne<User>. A hook annotated with Relation<mixed> or Value<mixed> (@return iterable<string, Relation<mixed>>) fails PHPStan: name the model (Relation<User>), or remove the @return, inherited from the
parent. Nothing changes at runtime.
Filters is no longer generic: replace Filters<Post> with Filters; when() takes a closure typed on the model
(fn(Request $request, Post $post) => …).
Deployment
Add nothing if you already run php artisan optimize: it now compiles the resource definitions too
(php artisan jsonapi:cache).