0

I'm having some issues dealing with recursion in a custom API endpoint I have written for Magento 2.

I have an endpoint that returns an entity which contains a number of fields, one of these fields is related_entities which is an array of entities.

The problem is the code that handles API responses in Magento will iterate over each field in the response, identify their type as defined in the docblocks of the interface and will execute all methods on the entity. This means that if I have Entity A and Entity B and they are related to one another Magento will recursively call getRelated() on each of the children, resulting in an endless cycle (until the configure php stack limit is reached)

I know that strictly speaking the "correct" way to design a REST API should be to really just return the entity requested and provide URLs or IDs for these related entities, however in this case that will massively increase the complexity of the implementation for this.

Currently to combat this I am having to determine if the call is recursive using the Magento registry and some strategically placed $entity->getData() calls.

Are there any other ways to combat this? The other other way I can think of is using something like count(debug_backtrace()) which would result in really messy code.

Any thoughts on this would be greatly appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.