Skip to content

Commit 7d0224b

Browse files
docs(serializer): deprecation of annotations and fix broken link (#2118)
- Added detailed documentation about the deprecation of annotations in favor of attributes for Symfony >= 7.0. - Updated configuration examples for both annotation-based and attribute-based serializers. - Fixed a broken link to Symfony Flex documentation.
1 parent c6670dc commit 7d0224b

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

core/serialization.md

+53-5
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,51 @@ to limit the serialization depth.
4949
Just like other Symfony and API Platform components, the Serializer component can be configured using attributes, XML
5050
or YAML. Since attributes are easy to understand, we will use them in the following examples.
5151

52-
Note: if you aren't using the API Platform distribution, you will need to enable annotation support in the serializer configuration:
52+
> [!NOTE]
53+
> If you are not using the API Platform distribution, you need to enable annotation support in the serializer
54+
> configuration as outlined below, depending on your Symfony version.
55+
56+
#### Configuration for Symfony `<= 6.4`
57+
58+
##### General Case
59+
60+
Add the following configuration to your `framework.yaml` file:
5361

5462
```yaml
5563
# api/config/packages/framework.yaml
5664
framework:
5765
serializer: { enable_annotations: true }
5866
```
5967
60-
If you use [Symfony Flex](https://github.com/symfony/flex), just execute `composer req doctrine/annotations` and you are
61-
all set!
68+
##### Using Symfony Flex
69+
70+
If you use [Symfony Flex](https://github.com/symfony/flex) and Symfony `<= 6.4`, simply run the following command:
71+
72+
```console
73+
composer req doctrine/annotations
74+
```
75+
76+
You're all set!
77+
78+
#### Configuration for Symfony `>= 7.0`
79+
80+
If you are using Symfony >= 7.0, [annotations have been replaced by attributes](https://www.doctrine-project.org/2022/11/04/annotations-to-attributes.html).
81+
82+
Update your configuration as follows:
83+
84+
```diff
85+
# api/config/packages/framework.yaml
86+
87+
framework:
88+
- serializer: { enable_annotations: true }
89+
+ serializer: { enable_attributes: true }
90+
```
91+
92+
#### Additional Syntax Configuration for All Versions
93+
94+
If you want to use YAML or XML for serialization, add the mapping path to the serializer configuration:
6295

63-
If you want to use YAML or XML, please add the mapping path in the serializer configuration:
96+
<code-selector>
6497

6598
```yaml
6699
# api/config/packages/framework.yaml
@@ -70,6 +103,21 @@ framework:
70103
paths: ['%kernel.project_dir%/config/serialization']
71104
```
72105

106+
```xml
107+
<!-- api/config/packages/framework.xml -->
108+
<framework>
109+
<!-- ... -->
110+
<serializer>
111+
<mapping>
112+
<path>%kernel.project_dir%/config/serialization</path>
113+
</mapping>
114+
</serializer>
115+
</framework>
116+
117+
```
118+
119+
</code-selector>
120+
73121
## Using Serialization Groups
74122

75123
It is simple to specify what groups to use in the API system:
@@ -435,7 +483,7 @@ The generated JSON using previous settings is below:
435483
```
436484

437485
In order to optimize such embedded relations, the default Doctrine state provider will automatically join entities on relations
438-
marked as [`EAGER`](https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/annotations-reference.html#manytoone).
486+
marked as [`EAGER`](https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/attributes-reference.html#manytoone).
439487
This avoids the need for extra queries to be executed when serializing the related objects.
440488

441489
Instead of embedding relations in the main HTTP response, you may want [to "push" them to the client using HTTP/2 server push](push-relations.md).

0 commit comments

Comments
 (0)