Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

getRelationship method gets called twice per each resource in response #103

Closed
bebor opened this issue Oct 27, 2016 · 1 comment
Closed
Labels

Comments

@bebor
Copy link

bebor commented Oct 27, 2016

suppose we have a request url http://xxx/jsonapi/speakers/1654?include=sessions

the result will be one primary resource of speakers, and it will include sessions, which will come from (in this test scenario) from SpeakerSerializer->sessions()

but that sessions() method gets called twice (and it should be called only once, since there is only 1 primary element). if the request would call for a list of speakers, like so: http://xxx/jsonapi/speakers/?include=sessions, then the SpeakerSerializer->sessions() will get called twice the time of the count of the speakers items

i debugged the code, and found that the SpeakerSerializer->sessions() gets called:

  1. here
    AbstractSerializer.php
    line 75, inside of public function getRelationship($model, $name)
$relationship = $this->$method($model);

this AbstractSerializer.php:getRelationship() gets called from Resource.php:toArray()

  1. and again the same line (meaning AbstractSerializer.php:getRelationship()) will be called from Document.php::toArray() from within the line saying:
$resources = $this->getIncluded($this->data);

so to sum it up:
Documen.php:toArray() calls the same code twice per each resource, and does so on the line saying
$document['data'] = $this->data->toArray();
and just next to it
$resources = $this->getIncluded($this->data);

@tobyzerner
Copy link
Owner

Thanks for the detailed report @bebor. I've pushed a fix, can you confirm it's working as expected?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants