Skip to content

Commit dc0a421

Browse files
docs(serialization): Add XML mappings (#1549)
* docs(serialization): Add XML mappings Add example XML mappings to show how to set (de)normalizationContext in APIP 2.7/3.0 * Apply suggestions from code review Co-authored-by: Vincent <[email protected]>
1 parent ea85336 commit dc0a421

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

core/serialization.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,53 @@ App\Entity\Book:
118118
groups: ['write']
119119
```
120120

121+
```xml
122+
<!-- api/config/api_platform/resources.xml -->
123+
<?xml version="1.0" encoding="UTF-8" ?>
124+
<resources xmlns="https://api-platform.com/schema/metadata/resources-3.0"
125+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
126+
xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
127+
https://api-platform.com/schema/metadata/resources-3.0.xsd">
128+
<resource class="App\Entity\Book">
129+
<normalizationContext>
130+
<values>
131+
<value name="groups">
132+
<values>
133+
<value>read</value>
134+
</values>
135+
</value>
136+
</values>
137+
</normalizationContext>
138+
<denormalizationContext>
139+
<values>
140+
<value name="groups">
141+
<values>
142+
<value>write</value>
143+
</values>
144+
</value>
145+
</values>
146+
</denormalizationContext>
147+
</resource>
148+
</resources>
149+
150+
<!-- api/config/serialization/Book.xml -->
151+
<?xml version="1.0" encoding="UTF-8" ?>
152+
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
153+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
154+
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
155+
http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd">
156+
<class name="App\Entity\Book">
157+
<attribute name="name">
158+
<group>read</group>
159+
<group>write</group>
160+
</attribute>
161+
<attribute name="author">
162+
<group>write</group>
163+
</attribute>
164+
</class>
165+
</serializer>
166+
```
167+
121168
[/codeSelector]
122169

123170
In the previous example, the `name` property will be visible when reading (`GET`) the object, and it will also be available
@@ -191,6 +238,58 @@ App\Entity\Book:
191238
groups: ['get']
192239
```
193240

241+
```xml
242+
<!-- api/config/api_platform/resources.xml -->
243+
<?xml version="1.0" encoding="UTF-8" ?>
244+
<resources xmlns="https://api-platform.com/schema/metadata/resources-3.0"
245+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
246+
xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
247+
https://api-platform.com/schema/metadata/resources-3.0.xsd">
248+
<resource class="App\Entity\Book">
249+
<normalizationContext>
250+
<values>
251+
<value name="groups">
252+
<values>
253+
<value>get</value>
254+
</values>
255+
</value>
256+
</values>
257+
</normalizationContext>
258+
<operations>
259+
<operation class="ApiPlatform\Metadata\Get" />
260+
<operation class="ApiPlatform\Metadata\Put">
261+
<normalizationContext>
262+
<values>
263+
<value name="groups">
264+
<values>
265+
<value>put</value>
266+
</values>
267+
</value>
268+
</values>
269+
</normalizationContext>
270+
</operation>
271+
</operations>
272+
</resource>
273+
</resources>
274+
275+
<!-- api/config/serialization/Book.xml -->
276+
<?xml version="1.0" encoding="UTF-8" ?>
277+
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
278+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
279+
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
280+
http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd">
281+
<class name="App\Entity\Book">
282+
<attribute name="name">
283+
<group>get</group>
284+
<group>put</group>
285+
</attribute>
286+
<attribute name="author">
287+
<group>get</group>
288+
</attribute>
289+
</class>
290+
</serializer>
291+
```
292+
194293
[/codeSelector]
195294

196295
The `name` and `author` properties will be included in the document generated during a `GET` operation because the configuration

0 commit comments

Comments
 (0)