@@ -70,10 +70,10 @@ use ApiPlatform\Core\Annotation\ApiResource;
70
70
use Symfony\C omponent\S erializer\A nnotation\G roups;
71
71
72
72
/**
73
- * @ApiResource(attributes={
74
- * "normalization_context" ={"groups"={"read"}},
75
- * "denormalization_context" ={"groups"={"write"}}
76
- * } )
73
+ * @ApiResource(
74
+ * normalizationContext ={"groups"={"read"}},
75
+ * denormalizationContext ={"groups"={"write"}}
76
+ * )
77
77
*/
78
78
class Book
79
79
{
@@ -91,7 +91,21 @@ class Book
91
91
}
92
92
` ` `
93
93
94
- With the config of the previous example, the `name` property will be accessible in read and write, but the `author` property
94
+ Alternatively, you can use the verbose syntax that is strictly equivalent :
95
+
96
+ ` ` ` php
97
+ <?php
98
+ // ...
99
+
100
+ /**
101
+ * @ApiResource(attributes={
102
+ * "normalization_context"={"groups"={"read"}},
103
+ * "denormalization_context"={"groups"={"write"}}
104
+ * })
105
+ */
106
+ ` ` `
107
+
108
+ With the config of the previous examples, the `name` property will be accessible in read and write, but the `author` property
95
109
will be write only, therefore the `author` property will never be included in documents returned by the API.
96
110
97
111
The value of the `normalization_context` is passed to the Symfony Serializer during the normalization process. In the same
@@ -122,12 +136,12 @@ use Symfony\Component\Serializer\Annotation\Groups;
122
136
123
137
/**
124
138
* @ApiResource(
125
- * attributes={
126
- * "normalization_context"={"groups"={"get"}}
127
- * },
139
+ * normalizationContext={"groups"={"get"}},
128
140
* itemOperations={
129
- * "get",
130
- * "put"={"normalization_context"={"groups"={"put"}}}
141
+ * "get",
142
+ * "put"={
143
+ * "normalization_context"={"groups"={"put"}}
144
+ * }
131
145
* }
132
146
* )
133
147
*/
@@ -187,9 +201,7 @@ use ApiPlatform\Core\Annotation\ApiResource;
187
201
use Symfony\C omponent\S erializer\A nnotation\G roups;
188
202
189
203
/**
190
- * @ApiResource(attributes={
191
- * "normalization_context"={"groups"={"book"}}
192
- * })
204
+ * @ApiResource(normalizationContext={"groups"={"book"}})
193
205
*/
194
206
class Book
195
207
{
@@ -265,9 +277,7 @@ namespace App\Entity;
265
277
use ApiPlatform\C ore\A nnotation\A piResource;
266
278
267
279
/**
268
- * @ApiResource(attributes={
269
- * "denormalization_context"={"groups"={"book"}}
270
- * })
280
+ * @ApiResource(denormalizationContext={"groups"={"book"}})
271
281
*/
272
282
class Book
273
283
{
@@ -297,10 +307,10 @@ use ApiPlatform\Core\Annotation\ApiResource;
297
307
use Symfony\C omponent\S erializer\A nnotation\G roups;
298
308
299
309
/**
300
- * @ApiResource(attributes={
301
- * "normalization_context" ={"groups"={"book_output "}},
302
- * "denormalization_context" ={"groups"={"book_input "}}
303
- * } )
310
+ * @ApiResource(
311
+ * normalizationContext ={"groups"={"book:output "}},
312
+ * denormalizationContext ={"groups"={"book:input "}}
313
+ * )
304
314
*/
305
315
class Book
306
316
{
@@ -311,7 +321,7 @@ class Book
311
321
*
312
322
* @var bool
313
323
*
314
- * @Groups({"book_output ", "admin_input "})
324
+ * @Groups({"book:output ", "admin:input "})
315
325
*/
316
326
private $active = false;
317
327
@@ -320,7 +330,7 @@ class Book
320
330
*
321
331
* @var string
322
332
*
323
- * @Groups({"book_output ", "book_input "})
333
+ * @Groups({"book:output ", "book:input "})
324
334
*/
325
335
private $name;
326
336
@@ -329,7 +339,7 @@ class Book
329
339
` ` `
330
340
331
341
All entry points are the same for all users, so we should find a way to detect if authenticated user is admin, and if so
332
- dynamically add `admin_input ` to deserialization groups.
342
+ dynamically add `admin:input ` to deserialization groups.
333
343
334
344
API Platform implements a `ContextBuilder`, which prepares the context for serialization & deserialization. Let's
335
345
[decorate this service](http://symfony.com/doc/current/service_container/service_decoration.html) to override the
@@ -612,9 +622,7 @@ namespace App\Entity;
612
622
use ApiPlatform\C ore\A nnotation\A piResource;
613
623
614
624
/**
615
- * @ApiResource(
616
- * attributes={"normalization_context"={"jsonld_embed_context"=true}
617
- * })
625
+ * @ApiResource(normalizationContext={"jsonld_embed_context"=true})
618
626
*/
619
627
class Book
620
628
{
0 commit comments