8000 [Serializer] Add ChainNormalizer and ChainDenormalizer by Nyholm · Pull Request #52900 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Add ChainNormalizer and ChainDenormalizer #52900

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

Open
wants to merge 10 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
make TwigBridge support sf 6.4
  • Loading branch information
Nyholm committed Jun 19, 2024
commit 3ef1e1c33e8aab4a5415366fe1be99dd95971cc9
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Serializer\Encoder\YamlEncoder;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\Serializer\Normalizer\ChainNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Twig\Environment;
Expand Down Expand Up @@ -50,7 +51,14 @@ public static function serializerDataProvider(): \Generator
private function getTwig(string $template): Environment
{
$meta = new ClassMetadataFactory(new AttributeLoader());
$runtime = new SerializerRuntime(new Serializer([], [new JsonEncoder(), new YamlEncoder()], new ObjectNormalizer($meta), new ObjectNormalizer($meta)));

// if Symfony 7.2
if (class_exists(ChainNormalizer::class)) {
$serializer = new Serializer([], [new JsonEncoder(), new YamlEncoder()], new ObjectNormalizer($meta), new ObjectNormalizer($meta));
} else {
$serializer = new Serializer([new ObjectNormalizer($meta)], [new JsonEncoder(), new YamlEncoder()]);
}
$runtime = new SerializerRuntime($serializer);

$mockRuntimeLoader = $this->createMock(RuntimeLoaderInterface::class);
$mockRuntimeLoader
Expand Down
44 changes: 27 additions & 17 deletions src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,33 @@ public function testSymfonySerialize()

$extractor = new PhpDocExtractor();
$propertyNormalizer = new PropertyNormalizer(null, null, $extractor);
$normalizers = [
new MimeMessageNormalizer($propertyNormalizer),
new ObjectNormalizer(null, null, null, $extractor),
$propertyNormalizer,
];
$denormalizers = [
new ArrayDenormalizer(),
new MimeMessageNormalizer($propertyNormalizer),
new ObjectNormalizer(null, null, null, $extractor),
$propertyNormalizer,
];
$serializer = new Serializer(
[],
[new JsonEncoder()],
new ChainNormalizer($normalizers),
new ChainDenormalizer($denormalizers),
);
// if Symfony 7.2
if (class_exists(ChainNormalizer::class)) {
$normalizers = [
new MimeMessageNormalizer($propertyNormalizer),
new ObjectNormalizer(null, null, null, $extractor),
$propertyNormalizer,
];
$denormalizers = [
new ArrayDenormalizer(),
new MimeMessageNormalizer($propertyNormalizer),
new ObjectNormalizer(null, null, null, $extractor),
$propertyNormalizer,
];
$serializer = new Serializer(
[],
[new JsonEncoder()],
new ChainNormalizer($normalizers),
new ChainDenormalizer($denormalizers),
);
} else {
$serializer = new Serializer([
new ArrayDenormalizer(),
new MimeMessageNormalizer($propertyNormalizer),
new ObjectNormalizer(null, null, null, $extractor),
$propertyNormalizer,
], [new JsonEncoder()]);
}

$serialized = $serializer->serialize($e, 'json', [ObjectNormalizer::IGNORED_ATTRIBUTES => ['cachedBody']]);
$this->assertStringMatchesFormat($expectedJson, json_encode(json_decode($serialized), \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"symfony/security-core": "^6.4|^7.0",
"symfony/security-csrf": "^6.4|^7.0",
"symfony/security-http": "^6.4|^7.0",
"symfony/serializer": "^7.2",
"symfony/serializer": "^6.4.3|^7.0.3",
"symfony/stopwatch": "^6.4|^7.0",
"symfony/console": "^6.4|^7.0",
"symfony/expression-language": "^6.4|^7.0",
Expand Down
0