mezzio / mezzio-hal

Hypertext Application Language implementation for PHP and PSR-7
https://docs.mezzio.dev/mezzio-hal/
BSD 3-Clause "New" or "Revised" License
19 stars 20 forks source link

Unable to convert self-referring instances to resources #1

Closed weierophinney closed 3 years ago

weierophinney commented 4 years ago

Imagine the following example:

class Parent {
    /** @var Child[] */
    public $childs;
    public function addChild(Child $child) {
        $this->childs[] = $child;
        $child->parent = $this;
    }
}
class Child {
    /** @var Parent */
    public $parent;
}

$parent = new Parent();
$parent->addChild(new Child());
$parent->addChild(new Child());

$generator->fromObject($parent, $request);
// the call above will result in an error:
// Error : Maximum function nesting level of '256' reached, aborting!

Other examples: One-To-One, Bidirectional One-To-One, Self-referencing One-To-Many, Bidirectional One-To-Many, Self-referencing Many-To-Many, Bidirectional Many-To-Many, Self-referencing

The zfcampus/zf-hal component solved this issue by using a $maxDepth property in metadata which is then passed through here and here.

Using this approach would result in a change of the \Zend\Expressive\Hal\ResourceGenerator\StrategyInterface interface which would be a BC break.

I would love if someone comes up with an alternate solution which will not break BC.


Originally posted by @tobias-trozowski at https://github.com/zendframework/zend-expressive-hal/pull/57

weierophinney commented 4 years ago

still smells. but should fit now.


Originally posted by @tobias-trozowski at https://github.com/zendframework/zend-expressive-hal/pull/57#issuecomment-486763165

weierophinney commented 4 years ago

@tobias-trozowski I've rebased your branch to squash several commits and remove both the revert commits and the commits they were reverting; the diff ends up the same as you had committed previously.

In the future, feel free to use git rebase -i + git push -f liberally when working on a patch; these help ensure the history is easier to follow, particularly when removing patches.

I'll review again now to see where we are in terms of ability to merge.


Originally posted by @weierophinney at https://github.com/zendframework/zend-expressive-hal/pull/57#issuecomment-491986956

guliano commented 4 years ago

Any news on this issue?

weierophinney commented 4 years ago

@guliano Not yet. We're still finishing out tasks related to the Laminas migration, so it may be a few weeks.