Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Closed
wants to merge 5 commits into from

Conversation

silverbackdan
Copy link

@silverbackdan silverbackdan commented Nov 23, 2022

Q A
Branch? 6.2
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #47993
License MIT
Doc PR n/a

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

@carsonbot
Copy link

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:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.2 branch.

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!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot
Copy link

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".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@silverbackdan
Copy link
Author

silverbackdan commented Nov 23, 2022

Oh I see, the cloning of the email and envelope happens first.
Another solution could be here:
https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/Mailer/EventListener/MessageLoggerListener.php#L40

public function onMessage(MessageEvent $event): void
    {
        $event->setMessage(clone $event->getMessage());
        $this->events->add($event);
    }

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([]);
Copy link
Member

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.

Copy link
Member

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

Copy link
Author

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.

Copy link
Author

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?

Copy link
Member

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.

Copy link
Author

@silverbackdan silverbackdan Nov 24, 2022

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.

Copy link
Author

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.

Copy link
Member

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.

Copy link
Author

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?

@silverbackdan
Copy link
Author

I guess my questions would be:

  1. Is it the responsibility of the renderer to prepare the message appropriately for the possibility of sending via messenger/async?
  2. Could we do this perhaps in Symfony\Component\Mailer\Messenger\SendEmailMessage by checking if it's an instance of TemplatedEmail instead and clreating a clone there and clearing context?
  3. Is it useful/necessary to have the context still for debugging and testing purposes and would this constitute a BC break?

@silverbackdan
Copy link
Author

Just doing some checks to reproduce again, and having some bugs with Mime\Email::ensureBodyValid for an email which is valid too.
But context is either showing now, or only when I'm testing with messenger transport enabled. Tired head so may be silly mistakes. Leave it with me I'll do some digging on what's going on.

@silverbackdan
Copy link
Author

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.

@silverbackdan silverbackdan reopened this Nov 24, 2022
@silverbackdan silverbackdan marked this pull request as ready for review November 24, 2022 19:51
@carsonbot
Copy link

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:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.2 branch.

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!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@silverbackdan
Copy link
Author

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 TemplatedEmail and added a test to ensure the context is empty for the message bus.

But this should not affect the message dispatched to the profiler as far as I can see.

@silverbackdan
Copy link
Author

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.

@silverbackdan silverbackdan changed the base branch from 6.3 to 6.2 November 25, 2022 11:18
@silverbackdan
Copy link
Author

I've noticed the new 6.3 branch pop up today.
This would be great to resolve before the official 6.2 release and I'm happy to work today on any further adjustments you may deem necessary to avoid this breaking tests on the 6.2 release
I know release days are pretty intense, so appreciate your time if you're able to give any to resolving this one :)

Thanks!

@fabpot
Copy link
Member

fabpot commented Nov 26, 2022

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.

@silverbackdan
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Mailer][Profiler] 6.2-dev Email context is empty message collected form Profiler
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.