swisnl / json-api-client

A PHP package for mapping remote {json:api} resources to Eloquent like models and collections.
MIT License
205 stars 24 forks source link

Unable to fetch resource with empty to-one relationship #62

Closed ammarfrahm closed 5 years ago

ammarfrahm commented 5 years ago

When resource have an empty to-one relationship, the package unable to parse the document.

Detailed description

I believe this is a bug. I tried to fetch resource which had empty to-one relationship defined. But the package unable to parse the document because it has null object given when it expect a class which implement ItemInterface. For the record, i'm using custom model with TypeMapper.

Argument 1 passed to Swis\JsonApi\Client\Parsers\DocumentParser::getItemKey() must implement interface Swis\JsonApi\Client\Interfaces\ItemInterface, null given, called in C:\Users\afrastgeek\Projects\Playground\blog-composer\vendor\swisnl\json-api-client\src\Parsers\DocumentParser.php on line 213

However, empty relationship are recognized in the specification.

Sample resource i tried to fetch:

{
    data: {
        type: "cars",
        id: "1",
        attributes: {
            brand: "Voluptatum ab perspiciatis asperiores eos.",
            color: "Sapiente est libero cumque corporis voluptate odio.",
            created-at: 1567417355,
            updated-at: 1567417355
        },
        relationships: {
            user: {
                data: null,
                links: {
                    self: "http://blog-api.localhost/api/v1/cars/1/relationships/user",
                    related: "http://blog-api.localhost/api/v1/cars/1/user"
                }
            },
            garages: {
                data: [
                    {
                        type: "garages",
                        id: "1"
                    }
                ],
                links: {
                    self: "http://blog-api.localhost/api/v1/cars/1/relationships/garages",
                    related: "http://blog-api.localhost/api/v1/cars/1/garages"
                }
            }
        },
        links: {
            self: "http://blog-api.localhost/api/v1/cars/1"
        }
    }
}

Environment

JaZo commented 5 years ago

Thanks, fixed in 1.0.0-beta.3!