1

I am building a web app using Symfony 4.0. I have created an authentication system using a JWT and I have an endpoint that returns a token when a username and password is matched in the database.

I created a function to get User details for the current user, which I created so I can use JavaScript to build the header in my web app (i.e. appropriate links according to the logged in user privileges).

However, when I make a call to /api/users/current this gives the following error:

Maximum function nesting level of '512' reached, aborting!

The endpoint seems to be returning all entities related to the user, but I have no idea why this is happening.

UserController:

<?php

namespace App\Controller\Api;

use App\Controller\BaseController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\User\UserInterface;

class UserController extends BaseController
{
/**
 * @Route("/api/users/current")
 * @Method("GET")
 * @param UserInterface $user
 * @return Response
 */
public function getCurrentUser(UserInterface $user){
    $response = $this->createApiResponse($user, 200);

    return $response;
}
}

BaseController:

<?php

namespace App\Controller;

use JMS\Serializer\SerializationContext;
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

abstract class BaseController extends AbstractController
{
private $serializer;

public function __construct(SerializerInterface $serializer)
{
    $this->serializer = $serializer;
}

/**
 * @param $data
 * @param int $statusCode
 * @return Response
 */
protected function createApiResponse($data, $statusCode = 200)
{
    $json = $this->serialize($data);
    return new Response($json, $statusCode, array(
        'Content-Type' => 'application/json'
    ));
}

/**
 * @param Serializer $serializer
 * @param $data
 * @param string $format
 * @return mixed
 */
protected function serialize($data, $format = 'json')
{
    $context = new SerializationContext();
    $context->setSerializeNull(true);

    return $this->serializer->serialize($data, $format, $context);
}
}
Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
Shaun
  • 526
  • 4
  • 13
  • 1
    Sounds like a xdebug issue so either raise the limit or don't use xdebug. Possible duplicate of [Solution for "Fatal error: Maximum function nesting level of '100' reached, aborting!" in PHP](https://stackoverflow.com/questions/8656089/solution-for-fatal-error-maximum-function-nesting-level-of-100-reached-abor) – MonkeyZeus Jul 09 '18 at 16:56
  • 1
    Look at the stacktrace, it's possible you have some recursive call loop somewhere. – Furgas Jul 09 '18 at 17:25
  • 4
    Yep. Something in your serialize is calling itself. Or possibly your $user is pointing to a collection of users one of which is the original user. In other words, a circular reference. – Cerad Jul 09 '18 at 18:24
  • Thank you, yes this was a circular reference, I was able to resolve this using an exclusion policy of JMS Serializer - https://jmsyst.com/libs/serializer/master/cookbook/exclusion_strategies – Shaun Jul 10 '18 at 08:19

0 Answers0