Releases: laravel-json-api/laravel
Releases · laravel-json-api/laravel
Countable relations, id encoding and non-Eloquent resources
Added
- #18 Added a
withCountquery parameter. For Eloquent resources, this allows a client to request the relationship count for the primary data's relationships. Refer to documentation for implementation details. - #55 Encoding and decoding of resource IDs is now supported. The
IDfield needs to implement theLaravelJsonApi\Contracts\Schema\IdEncoderinterface for this to work. - #41 Hash IDs are now supported by installing the
laravel-json-api/hashidspackage and using theHashIdfield instead of the standard EloquentIDfield. Refer to documentation for details. - #30 Non-Eloquent resources are now supported via the
laravel-json-api/non-eloquentpackage. Refer to documentation for implementation details. - There is now a
Core\Reponses\RelatedResponseclass for returning the result for a related resources endpoint. For example, the/api/v1/posts/1/commentsendpoint. Previously theDataResponseclass was used. While this class can still be used, the newRelatedResponseclass merges relationship meta into the top-levelmetamember of the response document. For to-many relationships that are countable, this will mean the top-levelmetamember will contain the count of the relationship. - The schema generator Artisan command now has a
--non-eloquentoption to generate a schema for a non-Eloquent resource.
Changed
- The
LaravelJsonApi::registerQuery(),LaravelJsonApi::registerCollectionQuery()andLaravelJsonApi::registerRequest()methods must now be used to register custom HTTP request classes for specified resource types. Previously methods could be called on theRequestResolverclasses, but these have now been removed.
Fixed
- Relationship endpoints that return resource identifiers now correctly include page meta in the top-level meta member of the document, if the results are paginated. Previously the page meta was incorrectly omitted.
Soft deletes, polymorphic to-many and multi-resource models
Added
- #43 The package now supports soft-deleting resources. For full details on how to apply this to resource schemas, refer to the new Soft Deleting chapter in the documentation.
- Multi-resource models are now supported. This allows developers to represent a single model class as multiple different JSON:API resource types within an API. Refer to documentation for details of how to implement.
- #8 The new
MorphToManyrelation field can now be used to add polymorphic to-many relations to a schema. Refer to documentation for details. - Developers can now type-hint dependencies in their server's
serving()method. - Can now manually register request, query and collection query classes using the
RequestResolver::registerRequest(),RequestResolver::registerQuery()andRequestResolver::registerCollectionQuery()static methods.
Extensive additions and fixes
Added
- Added missing
jsonapi:authorizergenerator command. - The Eloquent schema now has
indexQueryandrelatableQuerymethods. These allow filtering for authorization purposes when a list of resources is being retrieved. For instance, it could filter those queries so that only models belonging to the authenticated user are returned. - #23 The resource request class now does not need to exist for the destroy controller action. Previously the implementation was expecting the resource request class to exist, even though delete validation was optional.
- #24 Controller actions will now stop executing and return a response if one is returned by the before action hooks: i.e.
searching,reading,saving,creating,updating,deleting,readingRelated<Name>,reading<Name>,updating<Name>,attaching<Name>anddetaching<Name>. - #37 Can now use constructor dependency injection in
Serverclasses. - #40 There is now a new
MetaResponseclass that can be used when returning meta-only responses. In addition, response classes have been updated to add awithServermethod. This can be used to specify the named server the response should use to encode the JSON:API document. This has to be used when returning responses from routes that have not run the JSON:API middleware (i.e. there is no default server available via the service container). - #9 The Laravel route registrar is now passed through to the
resources,relationshipsandactionscallbacks as the second function argument. - #36 Eloquent schemas now support complex singular filter logic, via the
Schema::isSingular()method. - #33 Specification compliance will now reject an incorrect resource type in a relationship. For example, if a relationship expects
tagsbut the client sendsposts, the request will be rejected with an error message thatpostsare not supported.
Changed
- #22 BREAKING The
indexandstoremethods on the authorizer contract now receive the model class as their second argument. This is useful for authorizers that are used for multiple resource types. - BREAKING When querying or modifying models via the schema repository or store, calls to
using()must be replaced withwithRequest(). This change was made to make it clearer that the request class can be passed into query builders. - #28 The sparse field sets validation rule will now reject with a specific message identifying any resource types in the parameter that do not exist.
- #35 The
Relation::type()method must now be used when setting the inverse resource type for the relation.
Fixed
- Optional parameters to generator commands that require values now work correctly. Previously these were incorrectly set up as optional parameters that expected no values.
- #25 The encoder now correctly handles conditional fields when iterating over a resource's relationships.
- #26 Fix parsing the
fieldsquery parameter to field set value objects. - #34 Do not require server option when generating a generic authorizer with multiple servers present.
- #29 Do not reject delete requests without a
Content-Typeheader. - #11 Fixed iterating over an empty to-many generator twice in the underlying compound document encoder.
Deprecated
- The
Relation::inverseType()method is deprecated and will be removed in1.0-stable. UseRelation::type()instead.
Custom resource controller actions and more
Added
- #12 Can now register routes for custom actions on a resource, using the
actions()helper method when registering resources. See the PR for examples. - The
JsonApiControllernow has the LaravelAuthorizesRequests,DispatchesJobsandValidatesRequeststraits applied. - #6 Resource class can now use conditional fields in their relationships. This works in the same way as conditional attributes: the resource's
when()andmergeWhen()method should be used to add conditional relationships. - #13 Added French translations for JSON:API errors generated by specification parsing and resource/query parameter validation.
- #7 Eloquent schemas now support default eager loading via their
$withproperty. - #15 When parsing a JSON:API document for compliance with the specification, the client will now receive a clearer error message if they submit a to-one relationship object for a to-many relationship (and vice-versa).
Changed
- #2 BREAKING Improved the extraction of existing resource field values when constructing validation data for update requests:
- The
existingAttributes()andexistingRelationships()methods on the resource request class has been removed. If you need to modify the existing values before the client values are merged, implement thewithExisting()method instead. This receives the model and its JSON representation (as an array). - The
mustValidate()method must now be called on a schema relationship field. (Previously this was on the resource relation.) By default, belongs-to and morph-to relations will be included when extracting existing values; all other relations will not. Use themustValidate()ornotValidated()method on the schema relation to alter whether a relation is included in the extracted values.
- The
Optional Resources
Added
- #1 Resource classes are now optional. If one is not defined, the implementation falls-back to using the Eloquent schema to serialize a model. Eloquent schema fields now have new
hiddenandserializeUsingmethods to customise the serialization of models by the schema. - Resource classes now support using conditional attributes in their
meta()method. - New field classes
ArrayListandArrayHashhave been added, to distinguish between PHP zero-indexed arrays that serialize to JSON arrays (ArrayList) and PHP associative arrays that serialize to JSON objects (ArrayHash). The distinction is required because an empty array list can be serialized to[]in JSON whereas an empty associative array must be serialized tonullin JSON.
Changed
- BREAKING The JsonApiResource method signatures for the
attributes(),relationships(),meta(), andlinks()methods have been changed so that they receive the HTTP request as the first (and only) parameter. This brings the implementation in line with Laravel's Eloquent resources, which receive the request to theirtoArray()method. The slight difference is our implementation allows the request to benull- this is to cover encoding resources outside of HTTP requests, e.g. queued broadcasting. When upgrading, you will need to either delete resource classes (as they are now optional), or update the method signatures on any classes you are retaining.
Fixed
- #3 Example server registration in the published configuration file prevented developer from creating a
v1server after adding this package to their Laravel application. - Package discovery for sub-packages that have service providers now works correctly.
Removed
- BREAKING The
Arrschema field has been removed - use the newArrayListorArrayHashfields instead. - BREAKING The
urimethod on resource and relationship routes has been removed:- The resource type URI can now be set on the resource's schema (using the
$uriTypeproperty). - Relationship URIs are now set on the schema field for the relationship (via the
withUriFieldNamemethod).
- The resource type URI can now be set on the resource's schema (using the