When using ApiSubresource annotation with maxDepth option in release v2.2.1, if I have multiple properties in a class (all properties are collections and are ManyToMany or OneToMany), only the first property would come up in the swagger doc and the routes of other subResources are not even generated.
But if maxDepth option is omitted, then all the subResources' routes are generated correctly.
My User class:
...
/**
* @var Poet[] List of all users favorite poets.
* @ORM\ManyToMany(targetEntity="App\Entity\Poet", fetch="EXTRA_LAZY")
* @ApiSubresource(maxDepth=1)
*/
protected $poets;
/**
* @var Book[] List of all users favorite books.
* @ORM\ManyToMany(targetEntity="App\Entity\Book", fetch="EXTRA_LAZY")
* @ApiSubresource(maxDepth=1)
*/
protected $books;
/**
* @var Poem[] List of all users favorite poems.
* @ORM\ManyToMany(targetEntity="App\Entity\Poem", fetch="EXTRA_LAZY")
* @ApiSubresource(maxDepth=1)
*/
protected $poems;
...
and I just figured out that if I increase the maxDepth of each property by 1 it will work. that is a very strange behavior.
I changed the User class to this and all the routes for these 3 subresources generated successfully:
...
/**
* @var Poet[] List of all users favorite poets.
* @ORM\ManyToMany(targetEntity="App\Entity\Poet", fetch="EXTRA_LAZY")
* @ApiSubresource(maxDepth=1)
*/
protected $poets;
/**
* @var Book[] List of all users favorite books.
* @ORM\ManyToMany(targetEntity="App\Entity\Book", fetch="EXTRA_LAZY")
* @ApiSubresource(maxDepth=2)
*/
protected $books;
/**
* @var Poem[] List of all users favorite poems.
* @ORM\ManyToMany(targetEntity="App\Entity\Poem", fetch="EXTRA_LAZY")
* @ApiSubresource(maxDepth=3)
*/
protected $poems;
...
And if for example I set the $poems maxDepth to 2 or 1, it would not work.
hey, look at this if it can help you: https://github.com/api-platform/core/issues/1717#issuecomment-370349445
Hey, I look forward and yes, I confirmed the bug.
I have the same issue with 2 @ApiSubresource.
If I set @ApiSubresource(maxDepth=1), it generate only the first Subresource route and the second ApiSubresource route is not generated.
The problem seems to be here: https://github.com/api-platform/core/blob/master/src/Operation/Factory/SubresourceOperationFactory.php#L90
I think we should create an array of $depth indexed by $visiting for all Subresource depth became independent from each other.
What do you think about this @soyuka ?
I think we should create an array of $depth indexed by $visiting for all Subresource depth became independent from each other.
Definitely. I won't have time to take care of this though.
@Nightbr Do you have time to do it ?
@Simperfit Hey, I will try to find some time to fix this next week.
But it seems it requires to change the $depth and $maxDepth to arrays indexed by subresources in order to keep a relative depth for each subresources in an Entity.
@Simperfit @soyuka I just created a PR which resolved the problem. Seems to be simpler that I think, we just need to reset the $depth when we return to rootResourceClass. I created a test for this case in order to fix it for good!
Thanks for the fix @Nightbr !
Hi, same behavior here, i have updated apiplatform/core to dev-master but, cleared cache and issue still occurs. Maybe i have something wrong in my implementation: i have relation betwen user-> ProjectLike (custom n:m with extra fields; ProjectLike is subresource of User and Project) -> Project (with subresources). I would like to control depth only to project (instead of project and all of his subresources, so i limited depth to 2) - i expected that only the user/{id}/project_likes (which represents Project) will be available, but also there is available section subresource of Project. If i change depth to 1, then subresource is not visible at all. When i change depth to more than 2, then also another subresource (next to section) of Project are available. So i don't get it...

Hey, can you give us the exact config of your subresources with the param maxDepth. I though I have fixed this and write tests for this case but I can have missed a special case.
Hey @KamilBanaszkiewicz, the fix is in the last release https://github.com/api-platform/core/releases/tag/v2.3.1
Hope this will fix your issue :+1:
Most helpful comment
Hey, I look forward and yes, I confirmed the bug.
I have the same issue with 2 @ApiSubresource.
If I set @ApiSubresource(maxDepth=1), it generate only the first Subresource route and the second ApiSubresource route is not generated.
The problem seems to be here: https://github.com/api-platform/core/blob/master/src/Operation/Factory/SubresourceOperationFactory.php#L90
I think we should create an array of $depth indexed by $visiting for all Subresource depth became independent from each other.
What do you think about this @soyuka ?