Skip to content

[Serializer][Mime] Fix Mime message serialization #37847

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

Merged
merged 1 commit into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\MimeMessageNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;

class TemplatedEmailTest extends TestCase
{
Expand Down Expand Up @@ -33,4 +40,77 @@ public function testSerialize()
$this->assertEquals('text.html.twig', $email->getHtmlTemplate());
$this->assertEquals($context, $email->getContext());
}

public function testSymfonySerialize()
{
// we don't add from/sender to check that validation is not triggered to serialize an email
$e = new TemplatedEmail();
$e->to('you@example.com');
$e->textTemplate('email.txt.twig');
$e->htmlTemplate('email.html.twig');
$e->context(['foo' => 'bar']);
$e->attach('Some Text file', 'test.txt');
$expected = clone $e;

$expectedJson = <<<EOF
{
"htmlTemplate": "email.html.twig",
"textTemplate": "email.txt.twig",
"context": {
"foo": "bar"
},
"text": null,
"textCharset": null,
"html": null,
"htmlCharset": null,
"attachments": [
{
"body": "Some Text file",
"name": "test.txt",
"content-type": null,
"inline": false
}
],
"headers": {
"to": [
{
"addresses": [
{
"address": "you@example.com",
"name": ""
}
],
"name": "To",
"lineLength": 76,
"lang": null,
"charset": "utf-8"
}
]
},
"body": null,
"message": null
}
EOF;

$extractor = new PhpDocExtractor();
$propertyNormalizer = new PropertyNormalizer(null, null, $extractor);
$serializer = new Serializer([
new ArrayDenormalizer(),
new MimeMessageNormalizer($propertyNormalizer),
new ObjectNormalizer(null, null, null, $extractor),
$propertyNormalizer,
], [new JsonEncoder()]);

$serialized = $serializer->serialize($e, 'json');
$this->assertSame($expectedJson, json_encode(json_decode($serialized), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

$n = $serializer->deserialize($serialized, TemplatedEmail::class, 'json');
$serialized = $serializer->serialize($e, 'json');
$this->assertSame($expectedJson, json_encode(json_decode($serialized), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

$n->from('fabien@symfony.com');
$expected->from('fabien@symfony.com');
$this->assertEquals($expected->getHeaders(), $n->getHeaders());
$this->assertEquals($expected->getBody(), $n->getBody());
}
}
5 changes: 4 additions & 1 deletion src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,24 @@
},
"require-dev": {
"egulias/email-validator": "^2.1.10",
"phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0",
"symfony/asset": "^4.4|^5.0",
"symfony/dependency-injection": "^4.4|^5.0",
"symfony/finder": "^4.4|^5.0",
"symfony/form": "^5.1",
"symfony/http-foundation": "^4.4|^5.0",
"symfony/http-kernel": "^4.4|^5.0",
"symfony/mime": "^4.4|^5.0",
"symfony/mime": "^5.2",
"symfony/polyfill-intl-icu": "~1.0",
"symfony/property-info": "^4.4|^5.1",
"symfony/routing": "^4.4|^5.0",
"symfony/translation": "^5.0",
"symfony/yaml": "^4.4|^5.0",
"symfony/security-acl": "^2.8|^3.0",
"symfony/security-core": "^4.4|^5.0",
"symfony/security-csrf": "^4.4|^5.0",
"symfony/security-http": "^4.4|^5.0",
"symfony/serializer": "^5.2",
"symfony/stopwatch": "^4.4|^5.0",
"symfony/console": "^4.4|^5.0",
"symfony/expression-language": "^4.4|^5.0",
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
use Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
use Symfony\Component\Serializer\Normalizer\MimeMessageNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ProblemNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
Expand Down Expand Up @@ -76,6 +78,10 @@
->args([[], service('serializer.name_converter.metadata_aware')])
->tag('serializer.normalizer', ['priority' => -915])

->set('serializer.normalizer.mime_message', MimeMessageNormalizer::class)
->args([service('serializer.normalizer.property')])
->tag('serializer.normalizer', ['priority' => -915])

->set('serializer.normalizer.datetimezone', DateTimeZoneNormalizer::class)
->tag('serializer.normalizer', ['priority' => -915])

Expand Down Expand Up @@ -114,6 +120,18 @@

->alias(ObjectNormalizer::class, 'serializer.normalizer.object')

->set('serializer.normalizer.property', PropertyNormalizer::class)
->args([
service('serializer.mapping.class_metadata_factory'),
service('serializer.name_converter.metadata_aware'),
service('property_info')->ignoreOnInvalid(),
service('serializer.mapping.class_discriminator_resolver')->ignoreOnInvalid(),
null,
[],
])

->alias(PropertyNormalizer::class, 'serializer.normalizer.property')

->set('serializer.denormalizer.array', ArrayDenormalizer::class)
->tag('serializer.normalizer', ['priority' => -990])

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"symfony/security-bundle": "^5.1",
"symfony/security-csrf": "^4.4|^5.0",
"symfony/security-http": "^4.4|^5.0",
"symfony/serializer": "^4.4|^5.0",
"symfony/serializer": "^5.2",
"symfony/stopwatch": "^4.4|^5.0",
"symfony/string": "^5.0",
"symfony/translation": "^5.0",
Expand All @@ -62,7 +62,7 @@
"symfony/yaml": "^4.4|^5.0",
"symfony/property-info": "^4.4|^5.0",
"symfony/web-link": "^4.4|^5.0",
"phpdocumentor/reflection-docblock": "^3.0|^4.0",
"phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0",
"paragonie/sodium_compat": "^1.8",
"twig/twig": "^2.10|^3.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Mime/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public function attachPart(DataPart $part)
}

/**
* @return DataPart[]
* @return array|DataPart[]
*/
public function getAttachments(): array
{
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Mime/Header/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ final class Headers
'return-path' => PathHeader::class,
];

/**
* @var HeaderInterface[][]
*/
private $headers = [];
private $lineLength = 76;

Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Mime/Part/TextPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class TextPart extends AbstractPart
private $body;
private $charset;
private $subtype;
/**
* @var ?string
*/
private $disposition;
private $name;
private $encoding;
Expand Down
74 changes: 74 additions & 0 deletions src/Symfony/Component/Mime/Tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
use Symfony\Component\Mime\Part\Multipart\MixedPart;
use Symfony\Component\Mime\Part\Multipart\RelatedPart;
use Symfony\Component\Mime\Part\TextPart;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\MimeMessageNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;

class EmailTest extends TestCase
{
Expand Down Expand Up @@ -384,4 +391,71 @@ public function testSerialize()
$this->assertEquals($expected->getHeaders(), $n->getHeaders());
$this->assertEquals($e->getBody(), $n->getBody());
}

public function testSymfonySerialize()
{
// we don't add from/sender to check that validation is not triggered to serialize an email
$e = new Email();
$e->to('you@example.com');
$e->text('Text content');
$e->html('HTML <b>content</b>');
$e->attach('Some Text file', 'test.txt');
$expected = clone $e;

$expectedJson = <<<EOF
{
"text": "Text content",
"textCharset": "utf-8",
"html": "HTML <b>content</b>",
"htmlCharset": "utf-8",
"attachments": [
{
"body": "Some Text file",
"name": "test.txt",
"content-type": null,
"inline": false
}
],
"headers": {
"to": [
{
"addresses": [
{
"address": "you@example.com",
"name": ""
}
],
"name": "To",
"lineLength": 76,
"lang": null,
"charset": "utf-8"
}
]
},
"body": null,
"message": null
}
EOF;

$extractor = new PhpDocExtractor();
$propertyNormalizer = new PropertyNormalizer(null, null, $extractor);
$serializer = new Serializer([
new ArrayDenormalizer(),
new MimeMessageNormalizer($propertyNormalizer),
new ObjectNormalizer(null, null, null, $extractor),
$propertyNormalizer,
], [new JsonEncoder()]);

$serialized = $serializer->serialize($e, 'json');
$this->assertSame($expectedJson, json_encode(json_decode($serialized), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

$n = $serializer->deserialize($serialized, Email::class, 'json');
$serialized = $serializer->serialize($e, 'json');
$this->assertSame($expectedJson, json_encode(json_decode($serialized), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

$n->from('fabien@symfony.com');
$expected->from('fabien@symfony.com');
$this->assertEquals($expected->getHeaders(), $n->getHeaders());
$this->assertEquals($expected->getBody(), $n->getBody());
}
}
Loading