swisnl / json-api-client

A PHP package for mapping remote {json:api} resources to Eloquent like models and collections.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

json-api-client doesn't support relationships without data section

Fivell opened this issue · comments

Detailed description

While declaring item class developer should declare all the relations
if at least one of the relationships is skipped or added later by server

after more deep investigation it was discovered that JsonApiClient doesn't work by specification

According to spec https://jsonapi.org/format/#document-resource-object-relationships

Relationship should contain at least 1 of links, data, meta

So relationship with only links or meta considered to be valid by spec, but JsonApiClient doesn't work if there is no data section

client will fail with

Fatal error: Uncaught Art4\JsonApiClient\Exception\AccessException: "data" doesn't exist in Relationship. in .../vendor/art4/json-api-client/src/Relationship.php on line 119

Context

Developer should have ability to skip / not use not important relationships while developing

Possible implementation

json-api-client should check if relationship exist inside specific item class and skip building relationships instead of crashing.

Hydrator.php

$data = $relationship->get('data');

// patched here
          if(!$relationship->has('data')) {
            continue;
          }
          // patched here

Your environment

PHP 7.1
json-api-client from master (forked from https://github.com/didww/json-api-client/tree/Fivell-patch-1)

Hi @Fivell, thanks for the report, we will look into it.