json-api: Relationship data generator is invoked more than once if data is empty

I’ve been getting this error:

Exception: Cannot traverse an already closed generator
src/Parser/RelationshipData/RelationshipDataIsCollection.php:132

It only occurs when relationship data is empty. The cause of the bug is these lines here: https://github.com/neomerx/json-api/blob/master/src/Parser/RelationshipData/RelationshipDataIsCollection.php#L130-L131

As it does not assign an empty array to $this->parsedResources before iterating over the resource, the parsedResources property will remain null. Therefore when it is called a second time, there is no cache to use and the closed generator is used.

I’ll submit a PR with a test reproducing the problem and a fix.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 16 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Yeah I’m happy to maintain my fork because it’s a critical part of Laravel JSON:API. I’m not up for doing any re-writing, more just keeping it up-to-date for PHP versions and with the spec if that does change (though it’s taking them ages to get to 1.1 any way, so doubt we’ll be talking about 1.2 any time soon).

Hey @neomerx, do you need some help maintain this repo?