tobyzerner / json-api-php

JSON-API (http://jsonapi.org) responses in PHP.
MIT License
436 stars 79 forks source link

data and errors MUST NOT coexist #142

Open langeuh opened 6 years ago

langeuh commented 6 years ago

The members data and errors MUST NOT coexist in the same document. see http://jsonapi.org/format/#document-top-level

I quickly solved it this way as any exception gets turned in to an error in my code

I extended Tobscure\JsonApi\Document and overwrote the toArray() function

public function toArray()
{
    // The members data and errors MUST NOT coexist in the same document.
    if (!empty($this->errors)) {
        $this->data = null;
        $this->links = null;
    }

    return parent::toArray();
}

Ideally this gets combined with #130