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

Erstellt 4y | 30.11.2021, 12:20:11


Melden Sie sich an, um einen Kommentar hinzuzufügen

Andere Beiträge in dieser Gruppe

SymfonyOnline June 2025: Speakers, Stats & Replays!

Thank you for joining us at SymfonyOnline June 2025!

What a great edition of SymfonyOnline we’ve just wrapped up! 🎉

We were thrilled to welcome 300 participants from 35 different countries—a

18.06.2025, 08:50:17 | Symfony
A Week of Symfony #963 (June 9–15, 2025)

This week, Symfony celebrated the SymfonyOnline June 2025 conference with great success. Meanwhile, development efforts focused on improving invokable commands for the upcoming Symfony 7.4 version. Th

15.06.2025, 09:10:09 | Symfony
A Week of Symfony #962 (June 2–8, 2025)

This week, development activity focused on the upcoming Symfony 7.4 and 8.0 versions, which will deprecate and remove many features. In addition, we published a case study about Yousign. Finally, we'r

08.06.2025, 07:50:05 | Symfony
Case study - Yousign: Scaling Trust with Smart, Scalable Architecture

As digital signatures become the norm in modern business, Yousign has established itself as a trusted leader across Europe. Behind its simple, intuitive interface is a powerful technical engine, handl

06.06.2025, 07:10:24 | Symfony
SymfonyOnline June 2025 starts next week!

Get ready for the exciting SymfonyOnline June 2025, kicking off in a few days only! There’s still time to register and join the international online Symfony conference—along with pre-conferenc

05.06.2025, 10:20:09 | Symfony
A Week of Symfony #961 (May 26 – June 1, 2025)

This week, Symfony released the stable version of Symfony 7.3, which includes lots of amazing new features. We also published the maintenance versions 6.4.22 and 7.2.7.

Symfony development highlights

01.06.2025, 08:50:16 | Symfony
New in Symfony 7.3: DX Improvements (part 2)

This is the second part of the blog post showcasing the main DX (developer experience) features introduced in Symfony 7.3. Read the first part of this blog post.

Verify URI Signatures… https://symfon

29.05.2025, 09:10:19 | Symfony