Skip to content

Commit 7f70473

Browse files
authored
Merge pull request #526 from dunglas/short-attribute-syntax
Document and use the new short attributes syntax
2 parents a8164a8 + 39320b7 commit 7f70473

File tree

5 files changed

+50
-39
lines changed

5 files changed

+50
-39
lines changed

core/fosuser-bundle.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ use Symfony\Component\Serializer\Annotation\Groups;
5959
6060
/**
6161
* @ORM\Entity
62-
* @ApiResource(attributes={
63-
* "normalization_context"={"groups"={"user", "user-read"}},
64-
* "denormalization_context"={"groups"={"user", "user-write"}}
62+
* @ApiResource(
63+
* normalizationContext={"groups"={"user", "user:read"}},
64+
* denormalizationContext"={"groups"={"user", "user:write"}}
6565
* })
6666
*/
6767
class User extends BaseUser
@@ -85,7 +85,7 @@ class User extends BaseUser
8585
protected $fullname;
8686
8787
/**
88-
* @Groups({"user-write"})
88+
* @Groups({"user:write"})
8989
*/
9090
protected $plainPassword;
9191

core/graphql.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,8 @@ use Symfony\Component\Serializer\Annotation\Groups;
219219

220220
/**
221221
* @ApiResource(
222-
* attributes={
223-
* "normalization_context"={"groups"={"read"}},
224-
* "denormalization_context"={"groups"={"write"}}
225-
* },
222+
* normalizationContext={"groups"={"read"}},
223+
* denormalizationContext={"groups"={"write"}},
226224
* graphql={
227225
* "query"={"normalization_context"={"groups"={"query"}}},
228226
* "create"={

core/operations.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,12 @@ namespace App\Entity;
381381
use ApiPlatform\Core\Annotation\ApiResource;
382382
383383
/**
384-
* @ApiResource(collectionOperations={"api_questions_answer_get_subresource"={"method"="GET", "normalization_context"={"groups"={"foobar"}}}})
384+
* @ApiResource(collectionOperations={
385+
* "api_questions_answer_get_subresource"={
386+
* "method"="GET",
387+
* "normalization_context"={"groups"={"foobar"}}
388+
* }
389+
* })
385390
*/
386391
class Answer
387392
{
@@ -397,7 +402,7 @@ App\Entity\Answer:
397402
collectionOperations:
398403
api_questions_answer_get_subresource:
399404
method: 'GET' # nothing more to add if we want to keep the default controller
400-
normalization_context: {'groups': ['foobar']}
405+
normalization_context: {groups: ['foobar']}
401406
```
402407

403408
Or in XML:
@@ -446,7 +451,7 @@ You can control the path of subresources with the `path` option of the `subresou
446451
* subresourceOperations={
447452
* "answer_get_subresource"= {
448453
* "method"="GET",
449-
* "path"="/questions/{id}/all-answers",
454+
* "path"="/questions/{id}/all-answers"
450455
* },
451456
* },
452457
* )

core/serialization.md

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ use ApiPlatform\Core\Annotation\ApiResource;
7070
use Symfony\Component\Serializer\Annotation\Groups;
7171
7272
/**
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+
* )
7777
*/
7878
class Book
7979
{
@@ -91,7 +91,21 @@ class Book
9191
}
9292
```
9393

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
95109
will be write only, therefore the `author` property will never be included in documents returned by the API.
96110

97111
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;
122136
123137
/**
124138
* @ApiResource(
125-
* attributes={
126-
* "normalization_context"={"groups"={"get"}}
127-
* },
139+
* normalizationContext={"groups"={"get"}},
128140
* itemOperations={
129-
* "get",
130-
* "put"={"normalization_context"={"groups"={"put"}}}
141+
* "get",
142+
* "put"={
143+
* "normalization_context"={"groups"={"put"}}
144+
* }
131145
* }
132146
* )
133147
*/
@@ -187,9 +201,7 @@ use ApiPlatform\Core\Annotation\ApiResource;
187201
use Symfony\Component\Serializer\Annotation\Groups;
188202
189203
/**
190-
* @ApiResource(attributes={
191-
* "normalization_context"={"groups"={"book"}}
192-
* })
204+
* @ApiResource(normalizationContext={"groups"={"book"}})
193205
*/
194206
class Book
195207
{
@@ -265,9 +277,7 @@ namespace App\Entity;
265277
use ApiPlatform\Core\Annotation\ApiResource;
266278
267279
/**
268-
* @ApiResource(attributes={
269-
* "denormalization_context"={"groups"={"book"}}
270-
* })
280+
* @ApiResource(denormalizationContext={"groups"={"book"}})
271281
*/
272282
class Book
273283
{
@@ -297,10 +307,10 @@ use ApiPlatform\Core\Annotation\ApiResource;
297307
use Symfony\Component\Serializer\Annotation\Groups;
298308
299309
/**
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+
* )
304314
*/
305315
class Book
306316
{
@@ -311,7 +321,7 @@ class Book
311321
*
312322
* @var bool
313323
*
314-
* @Groups({"book_output", "admin_input"})
324+
* @Groups({"book:output", "admin:input"})
315325
*/
316326
private $active = false;
317327
@@ -320,7 +330,7 @@ class Book
320330
*
321331
* @var string
322332
*
323-
* @Groups({"book_output", "book_input"})
333+
* @Groups({"book:output", "book:input"})
324334
*/
325335
private $name;
326336
@@ -329,7 +339,7 @@ class Book
329339
```
330340

331341
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.
333343

334344
API Platform implements a `ContextBuilder`, which prepares the context for serialization & deserialization. Let's
335345
[decorate this service](http://symfony.com/doc/current/service_container/service_decoration.html) to override the
@@ -612,9 +622,7 @@ namespace App\Entity;
612622
use ApiPlatform\Core\Annotation\ApiResource;
613623
614624
/**
615-
* @ApiResource(
616-
* attributes={"normalization_context"={"jsonld_embed_context"=true}
617-
* })
625+
* @ApiResource(normalizationContext={"jsonld_embed_context"=true})
618626
*/
619627
class Book
620628
{

core/swagger.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ thanks to the `swagger_definition_name` option:
192192
* collectionOperations={
193193
* "post"={
194194
* "denormalization_context"={
195-
* "groups"={"user_read"},
195+
* "groups"={"user:read"},
196196
* "swagger_definition_name": "Read",
197197
* },
198198
* },
@@ -219,7 +219,7 @@ It's also possible to re-use the (`de`)`normalization_context`:
219219
class User
220220
{
221221
const API_WRITE = [
222-
'groups' => ['user_read'],
222+
'groups' => ['user:read'],
223223
'swagger_definition_name' => 'Read',
224224
];
225225
}

0 commit comments

Comments
 (0)