-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Mailer] Fix: do not clear context of message after rendering #48300
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
Conversation
Hey! I see that this is your first PR. That is great! Welcome! Symfony has a contribution guide which I suggest you to read. In short:
Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change. When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor! I am going to sit back now and wait for the reviews. Cheers! Carsonbot |
Hey! Thanks for your PR. You are targeting branch "6.2" but it seems your PR description refers to branch "6.2 for bug fixes". Cheers! Carsonbot |
Oh I see, the cloning of the email and envelope happens first.
Once the correct fix is established I could update the failing tests. Edit: Hmm.. sorry this fix wouldn't work. Just tried it. Probably have to be what I've tried in the PR. |
@@ -67,8 +67,6 @@ public function render(Message $message): void | ||
$message->htmlTemplate(null); | ||
} | ||
|
||
$message->context([]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's useful, see #47075 (comment) for the explanation of why we need to empty the context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably have a test covering that use case, to avoid regressions on that use case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, thanks for clarifying, I hadn't seen that.
I wonder whether by doing this we end up removing the context from the debug profiler as well though. I wonder if there is a way to keep this information in the logs/profiler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it perhaps be preferable to clear the context at the time of serializing for the messenger dispatch, perhaps by cloning the message and clearing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Omitting the context when serializing a TemplatedEmail that is already rendered might indeed be an option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK cool, as I'm working in test environment for behat functional testing in my project, I don't have a test which triggers the PhpSerializer to serialize the envelope. I'll try and work out how to make a failing test in this PR when the context is not cleared first and then where (that would make sense) to hook in the logic to check for a TemplatedEmail before the serializer is called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the PR request to clear context before sending to the bus instead as an alternative option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your change would break valid use cases. There are 2 supported use cases: rendering emails before sending them to the bus and rendering them when consumed from the queue. So, we can only clear the context when the email is rendered, which is why the current code is here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there another alternative? This code change would break tests so would the advice to anyone upgrade from 6.1 to 6.2 be to adjust their tests and perhaps am I wrong in thinking that by clearing the context the profiler would not longer be able to output the context set in the email too?
Would the change in RC1 as it stands not constitute a breaking change?
I guess my questions would be:
|
Just doing some checks to reproduce again, and having some bugs with |
Interestingly enough I've adjusted my application's functional tests to use the in-memory transport with serialization enabled and my doctrine entity does serialize. But I'm able to trace through this process now to see where might be best to remove the context before serializing in any case. |
Hey! I see that this is your first PR. That is great! Welcome! Symfony has a contribution guide which I suggest you to read. In short:
Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change. When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor! I am going to sit back now and wait for the reviews. Cheers! Carsonbot |
How about this as an update/fix? I have set it to clear the context just before sending the message to the bus if it is a But this should not affect the message dispatched to the profiler as far as I can see. |
I still wonder whether the email in the profiler that is not queued would have the context, but I also wonder whether that's a problem or not as usually async and serialization is disabled during tests and for the profiler there would show a queued message with the context still.. I think. |
I've noticed the new 6.3 branch pop up today. Thanks! |
To be honest, I'm not sure there is anything to fix here. I could argue that testing emails should be done on the rendered email and not on the context passed to it. If you want to test the context, you can add a listener on Symfony\Component\Mailer\Event\MessageEvent that triggers early on (at least before the renderer). In any case, the only place where to clear the context is just after having rendered the email, anywhere else won't work. |
I agree that the tests could be done on the result of the context, also in a unit test to ensure the context is being set correctly. As there seems to be no alternative to where to clear the context, and as it's deemed necessary to do so, I'll adjust my tests accordingly. Thanks for your time in considering this in any case. |
When running tests in 6.1 I am checking the message context has been set correctly. The email that is collected in the logger is updated after rendering. This line doesn't seem necessary, but if it is, perhaps we should be looking here and to why the message, when cloned still has the context modified in the profiler.
https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/Mailer/Mailer.php#L54
Thoughts?
//cc @fabpot as this change was a commit by him. Happy to discuss.
Thank you