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

Létrehozva 4y | 2021. nov. 30. 12:20:11


Jelentkezéshez jelentkezzen be

EGYÉB POSTS Ebben a csoportban

New in Symfony 7.3: Routing Improvements

The Routing component provides an impressive list of features to map incoming URLs to your application code. Symfony 7.3 pushes it even further with a set of new features that improve developer experi

2025. máj. 21. 11:10:07 | Symfony
New in Symfony 7.3: JsonStreamer Component

Contributed by Mathias Arlaud in

2025. máj. 20. 9:30:13 | Symfony
Symfony UX CVE-2025-47946: Unsanitized HTML attribute injection via ComponentAttributes

Affected versions

Symfony UX symfony/ux-live-component and symfony/ux-twig-component versions <2.25.1 are affected by this security issue.

The issue has been fixed in the 2.25.1 version of these

2025. máj. 19. 12:40:14 | Symfony
New in Symfony 7.3: Yaml Improvements

Symfony has been reducing the need for configuration in applications for several years now. Thanks to PHP attributes, you can now configure most things alongside the relevant code, removing the need f

2025. máj. 19. 8:10:09 | Symfony
A Week of Symfony #959 (May 12–18, 2025)

This week, development activity focused on polishing Symfony 7.3 ahead of its final release in two weeks. We also continued publishing articles highlighting the new features of Symfony 7.3 and shared

2025. máj. 18. 8:50:08 | Symfony
SymfonyOnline June 2025:  Keynote “Symfony in 2025, Scaling to Zero.”

SymfonyOnline June 2025 is almost here, starting in a few weeks on:

June 10-11: Workshop days. June 12-13: Online conference days in English. All talks will be available for replay as soon as

2025. máj. 16. 15:10:26 | Symfony
New in Symfony 7.3: Dependency Injection Improvements

Symfony 7.3 introduces several enhancements to the DependencyInjection component that simplify service configuration, make autoconfiguration more flexible, and enable environment-specific aliasing.

S

2025. máj. 16. 8:10:11 | Symfony