New in Symfony 5.4: Serializer improvements

Symfony 5.4 was released yesterday, but we still have some blog posts pending to show its main new features. In this post we're highlighting the improvements added to the Serializer component.

Globally configured serializer context

        Contributed by Antoine Bluchet 
        in #38542.

The Serializer context controls the (de)serialization of resources. In current Symfony versions, this context is passed to all normalizers. In Symfony 5.4 we're improving the Serializer component configuration to allow you configure the default context globally. For example:

    1

2 3 4

config/packages/serializer.yaml

serializer: default_context: enable_max_depth: true

This example shows the YAML configuration, but you can also use XML and PHP.

Custom serializer for Symfony Messenger

        Contributed by Mathieu Santostefano 
        in #42257.

JSON-encoded messages consumed by Symfony Messenger are expected to have the following structure:

    1

2 3 4 5 6 { "message": { "body": {}, "headers": [] } }

However, when consuming messages generated by different third-parties, you won't get that message structure. That's why in Symfony 5.4 you can use your own serializer to JSON-decode messages.

Collect Denormalization Type Errors

        Contributed by Grégoire Pineau 
        in #42502.

In previous Serializer versions, when using typed PHP properties you could see errors in certain situations. For example, consider the following simple DTO:

    1

2 3 4 5 6 class MyDto { public string $property1; public int $property2; public array $property3; }

If your JSON data is like the following:

    1

2 3 4 5 { "property1": null, "property2": 7, "property3": [] }

When trying to deserialize that data you'll see a 500 error because the type of property1 is string and you're passing a null value. In Symfony 5.4 we've improved this behavior thanks to the new COLLECT_DENORMALIZATION_ERRORS option. If you pass that option, the PHP exception will include the detailed list of errors. Then you can process it like in the following example that handles some API:

    1

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

[Route('/api', methods:['POST'])]

public function apiPost(SerializerInterface $serializer, Request $request): Response { try { $dto = $serializer->deserialize($request->getContent(), MyDto::class, 'json', [ DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true, ]); } catch (PartialDenormalizationException $e) { $violations = new ConstraintViolationList(); /* @var NotNormalizableValueException / foreach ($e->getErrors() as $exception) { $message = sprintf('The type must be one of "%s" ("%s" given).', implode(', ', $exception->getExpectedTypes()), $exception->getCurrentType()); $parameters = []; if ($exception->canUseMessageForUser()) { $parameters['hint'] = $exception->getMessage(); } $violations->add(new ConstraintViolation($message, '', $parameters, null, $exception->getPath(), null)); };

    return $this->json($violations, 400);
}

return $this->json($dto);

}

                Sponsor the Symfony project.

https://symfony.com/blog/new-in-symfony-5-4-serializer-improvements?utm_source=Symfony%20Blog%20Feed&utm_medium=feed

Created 3y | Nov 30, 2021, 12:20:11 PM


Login to add comment

Other posts in this group

SymfonyOnline June 2025: Inside a Financial App Breach: Debugging a Million-Dollar Bug

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

Apr 22, 2025, 1:50:03 PM | Symfony
New in Symfony 7.3: Invokable Commands and Input Attributes

This is the first article in a series showcasing the most important new features introduced by Symfony 7.3, which will be released at the end of May 2025.

Apr 22, 2025, 9:10:36 AM | Symfony
A Week of Symfony #955 (April 14–20, 2025)

This week, the upcoming Symfony 7.3 version improved the AsAlias attribute by adding a new argument, introduced Clock support for UriSigner, and refined the return type of the ContainerInterface::get(

Apr 20, 2025, 8:30:06 AM | Symfony
SymfonyOnline June 2025: Rethinking File Handling in Symfony

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

Apr 16, 2025, 4:30:02 PM | Symfony
SymfonyLive Paris 2025: Recap and replay!

SymfonyLive Paris 2025 took place three weeks ago — a big thank you to everyone who joined us! The conference was held entirely in French, and now you can relive the best moments: replays, hig

Apr 15, 2025, 2:50:24 PM | Symfony
A Week of Symfony #954 (April 7–13, 2025)

This week, Symfony 7.3 entered its "feature freeze" period in preparation for its release at the end of May 2025. Development activity focused on refining and polishing its new features, including a n

Apr 13, 2025, 9:40:02 AM | Symfony
SymfonyOnline June 2025: What's New in Symfony 7.3

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

Apr 10, 2025, 4:40:34 PM | Symfony