-
Notifications
You must be signed in to change notification settings - Fork 67
Upgrade Notes
Overall, versions 3 and 4 are nearly identical and the main changes were made in SchemaInterface. An extra parameter ContextInterface was added to SchemaInterface::getAttributes and SchemaInterface::getRelationships. Thus the following changes are needed for migration
// add `use`
use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
class YourSchema extends BaseSchema
{
// ...
// add `ContextInterface $context`
public function getAttributes($author, ContextInterface $context): iterable
{
}
// add `ContextInterface $context`
public function getRelationships($resource, ContextInterface $context): iterable
{
}
}The context contains an extra information about resource such as its position in the data tree and encoder options such as filter field set and included paths. The context might be used for smarter loading resources from a database. Here is an example how a context might look in a debugger

Overall, versions 2 and 3 are very similar, though some of the classes, interfaces, constants and methods were renamed. Here is a list of things that should help you to migrate.
- Replace
Neomerx\JsonApi\Contracts\Document\DocumentInterface;withNeomerx\JsonApi\Contracts\Schema\DocumentInterface; - Replace
Neomerx\JsonApi\Contracts\Document\ErrorInterface;withNeomerx\JsonApi\Contracts\Schema\ErrorInterface; - Replace
Neomerx\JsonApi\Exceptions\ErrorCollection;withNeomerx\JsonApi\Schema\ErrorCollection;
Resource type should be declared with method getType(): string.
Signature has been changed for the following methods
-
getAttributeshas changed togetAttributes($resource): iterable -
getRelationshipshas changed togetRelationships($resource): iterable
Relationship constants has been renamed
- relationship key
self::DATArenamed toself::RELATIONSHIP_DATA - relationship key
self::SHOW_SELFrenamed toself::RELATIONSHIP_LINKS_SELF - relationship key
self::SHOW_RELATEDrenamed toself::RELATIONSHIP_LINKS_RELATED
Link constructor has been changed completely. See Links section for more.
Error and methods for adding errors to ErrorCollection has new parameter ?iterable $typeLinks. Please make sure you use the new signatures.
PHP strong types were added to all applicable interface and class methods. Actual methods to be updated depending on your usage however many users will need to change only the following ones
class YourSchema extends BaseSchema
{
public function getId($resource): ?string
{
// `: ?string` is added
...
}
public function getAttributes($resource, array $fieldKeysFilter = null): ?array
{
// `array $fieldKeysFilter = null` and `: ?array` are added
...
}
public function getRelationships($resource, bool $isPrimary, array $includeRelationships): ?array
{
// `: ?array` is added
...
}
}QueryParametersParser and RestrictiveQueryChecker were replaced with BaseQueryParser which parses defined by JSON API spec parts (e.g. fieldsets, includes, sorts) and provides helpful methods to work with not defined parts (e.g. filters, pagination).
Adapter class Request for non-PSR7 requests has been dropped.
Code related to JSON API Extensions has been deprecated and removed from the library.
Extensions and codec matcher were removed from class Responses which was renamed to BaseResponses.
Header parsers were simplified and changed significantly. Please read the related documentation in parsing API parameters section.
Added serialization to arrays which required refactoring protected members of class Encoder. If you do not inherit Encoder class in order change/extend its functionality you will migrate to 1.0 without changes in your code.
If you do inherit Encoder class and use protected members such as $container, $encoderOptions and etc you should use new methods getContainer(), getEncoderOptions() and etc. Also method encodeDataInternal() was renamed to encodeDataToArray() and now returns array instead of string. In order to convert array to json string method encodeToJson() should be used.
- Method
getRelationshipsofSchemahas changed its signature fromgetRelationships($resource, array $includeList = [])togetRelationships($resource, $isPrimary, array $includeList). New parameter $isPrimary informs if$resourcewill be put into primary data section rather than included. Parameter$includeListdo not have default value now. For more see. - If you declared manual
Linkadding ingetRelationshipsofSchemaand those links had relative paths it should be changed from'boo' => new Link('some/link')to'boo' => new Link($this->getSelfSubUrl($resource) . '/some/link'). For more see.
Both changes are made for better paging support in relationships.
-
Optional field
$selfSubUrlinSchemashould now have only starting slash (no ending slash anymore). If not set manually it will be composed as$this->selfSubUrl = '/' . $this->resourceType;. For more see. -
LinkInterfacewas moved fromNeomerx\JsonApi\Contracts\Schemanamespace toNeomerx\JsonApi\Contracts\Document. -
Linkwas moved fromNeomerx\JsonApi\Schemanamespace toNeomerx\JsonApi\Document. -
EncodingParametersInterfacewas moved fromNeomerx\JsonApi\Contracts\Http\Parametersnamespace toNeomerx\JsonApi\Contracts\Encoder. -
EncodingParameterswas moved fromNeomerx\JsonApi\Http\Parametersnamespace toNeomerx\JsonApi\Encoder.
These changes were inspired by this enhancement request.
HTTP classes for parsing and checking headers/query parameters are separated for better structure. It's a small part of the project though it was significantly refactored. Some of the highlights below. If you have any questions/difficulties while upgrading please don't hesitate to ask.
Class EncodingParameters now contains all parameters that come from HTTP query string (pagination, filters, sorting, etc). Also EncodingParameters moved from \Neomerx\JsonApi\Encoder\EncodingParameters to \Neomerx\JsonApi\Encoder\Parameters\EncodingParameters. That's probably the most frequent change you will have to do in your code.
HTTP headers related methods were moved from ParametersInterface to HeaderParametersInterface (\Neomerx\JsonApi\Contracts\Http\Headers\HeaderParametersInterface).
All HTTP query related methods from ParametersInterface were merged to EncodingParametersInterface.
- Homegrown HTTP interfaces (e.g.
CurrentRequestInterface) were replaced with PSR-7 (\Psr\Http\Message\ServerRequestInterfaceparticularly). See #109 for details. Small changes on your side will be required if you use HTTP part from this package (ParameterParser,CodecMatcher,ParametersCheckerand etc). - Class for HTTP
Responsecreation (Responses) was significantly refactored. Now it provides full variety of JSON API responses and has a few abstract method which allows integrate it with any framework/application. For more see here and here. - Error handling was improved.
RendererContainerInterfaceandRendererInterface. New classJsonApiExceptionandErrorCollectionwere added. For more see. - PSR-3 logging was added. For usage sample see.
-
CodecMatcherInterface::findDecoderwas renamed toCodecMatcherInterface::matchDecoder.
Field $selfSubUrl in Schema has become optional. By default it is constructed as
$this->selfSubUrl = '/' . $this->resourceType . '/';If you specify $selfSubUrl it will be checked that it starts and ends with /. Thus for performance reasons it is recommended to not set $selfSubUrl (unless it is different from default value).
Passing Links, Meta and JSON API version information via Encoder parameters have been deprecated and removed from the code. They should be passed via with... methods e.g.
$encoder
->withLinks($links)
->withMeta($docMeta)
->withJsonApiVersion($versionMeta)
->encodeData(...);A new parameter array[] $includeRelationships was added to method Schema::getRelationships. The parameter contains a list of relationship names which will be included in output result as full resources (attributes and relationships). It could be used for more optimal data fetch requests to a database. For example if a model Comment contains field author_id and $includeRelationships do not contain author an Author instance with empty attributes could be returned. It eliminates the necessity to make request(s) to a database for such cases.
The new parameter should be added to existing Schemas
class CommentSchema extends BaseSchema
{
...
public function getRelationships($comment, array $includeRelationships)
{
...
}
...
}Fields $isShowSelf (default true) and $isShowSelfInIncluded (default false) were replaced with methods getResourceLinks and getIncludedResourceLinks (default implementation below). If you have not changed $isShowSelf and $isShowSelfInIncluded you do not need to make any changes in your code.
public function getResourceLinks($resource)
{
$links = [
LinkInterface::SELF => $this->getSelfSubLink($resource),
];
return $links;
}
public function getIncludedResourceLinks($resource)
{
return [];
}