Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Messenger] No way to avoid circular reference exception #26935

Closed
davidbarratt opened this issue Apr 15, 2018 · 3 comments
Closed

[Messenger] No way to avoid circular reference exception #26935

davidbarratt opened this issue Apr 15, 2018 · 3 comments

Comments

@davidbarratt
Copy link

davidbarratt commented Apr 15, 2018

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 4.1

if you have an entity that has a circular reference like:
https://github.com/boggs-love/api/blob/87205c3ce42d82f69f6749245755c5192c1f0e0c/src/Entity/RSVP.php
and you attempt to dispatch it:

$this->messenger->dispatch($rsvp);

You'll get the following exception:

A circular reference has been detected when serializing the object of class "App\Entity\RSVP" (configured limit: 1)

Full error message:

{
	"message": "A circular reference has been detected when serializing the object of class \"App\\Entity\\RSVP\" (configured limit: 1)",
	"code": 0,
	"exception": "Symfony\\Component\\Serializer\\Exception\\CircularReferenceException",
	"previous": null,
	"file": "\/var\/www\/vendor\/symfony\/serializer\/Normalizer\/AbstractNormalizer.php",
	"line": 197,
	"trace": [
		"\/var\/www\/vendor\/symfony\/serializer\/Normalizer\/AbstractObjectNormalizer.php(83): Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer->handleCircularReference(Object(App\\Entity\\RSVP))",
		"\/var\/www\/vendor\/symfony\/serializer\/Serializer.php(133): Symfony\\Component\\Serializer\\Normalizer\\AbstractObjectNormalizer->normalize(Object(App\\Entity\\RSVP), 'json', Array)",
		"\/var\/www\/vendor\/symfony\/serializer\/Normalizer\/AbstractObjectNormalizer.php(119): Symfony\\Component\\Serializer\\Serializer->normalize(Object(App\\Entity\\RSVP), 'json', Array)",
		"\/var\/www\/vendor\/symfony\/serializer\/Serializer.php(133): Symfony\\Component\\Serializer\\Normalizer\\AbstractObjectNormalizer->normalize(Object(App\\Entity\\Guest), 'json', Array)",
		"\/var\/www\/vendor\/symfony\/serializer\/Serializer.php(143): Symfony\\Component\\Serializer\\Serializer->normalize(Object(App\\Entity\\Guest), 'json', Array)",
		"\/var\/www\/vendor\/symfony\/serializer\/Normalizer\/AbstractObjectNormalizer.php(119): Symfony\\Component\\Serializer\\Serializer->normalize(Object(Doctrine\\ORM\\PersistentCollection), 'json', Array)",
		"\/var\/www\/vendor\/symfony\/serializer\/Serializer.php(133): Symfony\\Component\\Serializer\\Normalizer\\AbstractObjectNormalizer->normalize(Object(App\\Entity\\RSVP), 'json', Array)",
		"\/var\/www\/vendor\/symfony\/serializer\/Serializer.php(106): Symfony\\Component\\Serializer\\Serializer->normalize(Object(App\\Entity\\RSVP), 'json', Array)",
		"\/var\/www\/vendor\/symfony\/messenger\/Transport\/Serialization\/Serializer.php(52): Symfony\\Component\\Serializer\\Serializer->serialize(Object(App\\Entity\\RSVP), 'json')",
		"\/var\/www\/vendor\/symfony\/messenger\/Adapter\/AmqpExt\/AmqpSender.php(38): Symfony\\Component\\Messenger\\Transport\\Serialization\\Serializer->encode(Object(App\\Entity\\RSVP))",
		"\/var\/www\/vendor\/symfony\/messenger\/Asynchronous\/Middleware\/SendMessageMiddleware.php(45): Symfony\\Component\\Messenger\\Adapter\\AmqpExt\\AmqpSender->send(Object(App\\Entity\\RSVP))",
		"\/var\/www\/vendor\/symfony\/messenger\/MessageBus.php(56): Symfony\\Component\\Messenger\\Asynchronous\\Middleware\\SendMessageMiddleware->handle(Object(App\\Entity\\RSVP), Object(Closure))",
		"\/var\/www\/vendor\/symfony\/messenger\/DataCollector\/MessengerDataCollector.php(64): Symfony\\Component\\Messenger\\MessageBus->Symfony\\Component\\Messenger\\{closure}(Object(App\\Entity\\RSVP))",
		"\/var\/www\/vendor\/symfony\/messenger\/MessageBus.php(56): Symfony\\Component\\Messenger\\DataCollector\\MessengerDataCollector->handle(Object(App\\Entity\\RSVP), Object(Closure))",
		"\/var\/www\/vendor\/symfony\/messenger\/Debug\/LoggingMiddleware.php(44): Symfony\\Component\\Messenger\\MessageBus->Symfony\\Component\\Messenger\\{closure}(Object(App\\Entity\\RSVP))",
		"\/var\/www\/vendor\/symfony\/messenger\/MessageBus.php(56): Symfony\\Component\\Messenger\\Debug\\LoggingMiddleware->handle(Object(App\\Entity\\RSVP), Object(Closure))",
		"\/var\/www\/vendor\/symfony\/messenger\/Middleware\/ValidationMiddleware.php(37): Symfony\\Component\\Messenger\\MessageBus->Symfony\\Component\\Messenger\\{closure}(Object(App\\Entity\\RSVP))",
		"\/var\/www\/vendor\/symfony\/messenger\/MessageBus.php(56): Symfony\\Component\\Messenger\\Middleware\\ValidationMiddleware->handle(Object(App\\Entity\\RSVP), Object(Closure))",
		"\/var\/www\/vendor\/symfony\/messenger\/MessageBus.php(40): Symfony\\Component\\Messenger\\MessageBus->Symfony\\Component\\Messenger\\{closure}(Object(App\\Entity\\RSVP))",
		"\/var\/www\/src\/Controller\/RespondController.php(99): Symfony\\Component\\Messenger\\MessageBus->dispatch(Object(App\\Entity\\RSVP))",
		"\/var\/www\/vendor\/symfony\/http-kernel\/HttpKernel.php(149): App\\Controller\\RespondController->createAction(Object(App\\Entity\\RSVP))",
		"\/var\/www\/vendor\/symfony\/http-kernel\/HttpKernel.php(66): Symfony\\Component\\HttpKernel\\HttpKernel->handleRaw(Object(Symfony\\Component\\HttpFoundation\\Request), 1)",
		"\/var\/www\/vendor\/symfony\/http-kernel\/Kernel.php(190): Symfony\\Component\\HttpKernel\\HttpKernel->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)",
		"\/var\/www\/html\/index.php(44): Symfony\\Component\\HttpKernel\\Kernel->handle(Object(Symfony\\Component\\HttpFoundation\\Request))",
		"{main}"
	]
}

It looks like this is because the serializer does not allow you to specify any $context (see #26900)

'body' => $this->serializer->serialize($message, $this->format),

Perhaps the routing config should also allow you to specify the serializer config?

@davidbarratt
Copy link
Author

ping @sroze

@sroze
Copy link
Contributor

sroze commented Apr 15, 2018

This is being discussed in #26900 and likely solvable by a framework.messenger configuration.

Note that it's not "impossible" though: you would just have to use your own encoder/decoder (i.e. replace the default services would work here).

sroze added a commit that referenced this issue Apr 17, 2018
This PR was squashed before being merged into the 4.1-dev branch (closes #26941).

Discussion
----------

[Messenger] Allow to configure the transport

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | ish
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #26900, #26908, #26935
| License       | MIT
| Doc PR        | ø

We allow users to configure the encoder/decoder used by the built-in adapter(s). This also adds the support of configuring the default's encoder/decoder format and context.

Commits
-------

1a3f0bb [Messenger] Allow to configure the transport
@sroze
Copy link
Contributor

sroze commented Apr 17, 2018

Fixed by #26941

@sroze sroze closed this as completed Apr 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants