Skip to content

Document and use the new short attributes syntax #526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions core/fosuser-bundle.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ORM\Entity
* @ApiResource(attributes={
* "normalization_context"={"groups"={"user", "user-read"}},
* "denormalization_context"={"groups"={"user", "user-write"}}
* @ApiResource(
* normalizationContext={"groups"={"user", "user:read"}},
* denormalizationContext"={"groups"={"user", "user:write"}}
* })
*/
class User extends BaseUser
Expand All @@ -85,7 +85,7 @@ class User extends BaseUser
protected $fullname;

/**
* @Groups({"user-write"})
* @Groups({"user:write"})
*/
protected $plainPassword;

Expand Down
6 changes: 2 additions & 4 deletions core/graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,8 @@ use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ApiResource(
* attributes={
* "normalization_context"={"groups"={"read"}},
* "denormalization_context"={"groups"={"write"}}
* },
* normalizationContext={"groups"={"read"}},
* denormalizationContext={"groups"={"write"}},
* graphql={
* "query"={"normalization_context"={"groups"={"query"}}},
* "create"={
Expand Down
11 changes: 8 additions & 3 deletions core/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,12 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(collectionOperations={"api_questions_answer_get_subresource"={"method"="GET", "normalization_context"={"groups"={"foobar"}}}})
* @ApiResource(collectionOperations={
* "api_questions_answer_get_subresource"={
* "method"="GET",
* "normalization_context"={"groups"={"foobar"}}
* }
* })
*/
class Answer
{
Expand All @@ -397,7 +402,7 @@ App\Entity\Answer:
collectionOperations:
api_questions_answer_get_subresource:
method: 'GET' # nothing more to add if we want to keep the default controller
normalization_context: {'groups': ['foobar']}
normalization_context: {groups: ['foobar']}
```

Or in XML:
Expand Down Expand Up @@ -446,7 +451,7 @@ You can control the path of subresources with the `path` option of the `subresou
* subresourceOperations={
* "answer_get_subresource"= {
* "method"="GET",
* "path"="/questions/{id}/all-answers",
* "path"="/questions/{id}/all-answers"
* },
* },
* )
Expand Down
60 changes: 34 additions & 26 deletions core/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ApiResource(attributes={
* "normalization_context"={"groups"={"read"}},
* "denormalization_context"={"groups"={"write"}}
* })
* @ApiResource(
* normalizationContext={"groups"={"read"}},
* denormalizationContext={"groups"={"write"}}
* )
*/
class Book
{
Expand All @@ -91,7 +91,21 @@ class Book
}
```

With the config of the previous example, the `name` property will be accessible in read and write, but the `author` property
Alternatively, you can use the verbose syntax that is strictly equivalent:

```php
<?php
// ...

/**
* @ApiResource(attributes={
* "normalization_context"={"groups"={"read"}},
* "denormalization_context"={"groups"={"write"}}
* })
*/
```

With the config of the previous examples, the `name` property will be accessible in read and write, but the `author` property
will be write only, therefore the `author` property will never be included in documents returned by the API.

The value of the `normalization_context` is passed to the Symfony Serializer during the normalization process. In the same
Expand Down Expand Up @@ -122,12 +136,12 @@ use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ApiResource(
* attributes={
* "normalization_context"={"groups"={"get"}}
* },
* normalizationContext={"groups"={"get"}},
* itemOperations={
* "get",
* "put"={"normalization_context"={"groups"={"put"}}}
* "get",
* "put"={
* "normalization_context"={"groups"={"put"}}
* }
* }
* )
*/
Expand Down Expand Up @@ -187,9 +201,7 @@ use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ApiResource(attributes={
* "normalization_context"={"groups"={"book"}}
* })
* @ApiResource(normalizationContext={"groups"={"book"}})
*/
class Book
{
Expand Down Expand Up @@ -265,9 +277,7 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(attributes={
* "denormalization_context"={"groups"={"book"}}
* })
* @ApiResource(denormalizationContext={"groups"={"book"}})
*/
class Book
{
Expand Down Expand Up @@ -297,10 +307,10 @@ use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ApiResource(attributes={
* "normalization_context"={"groups"={"book_output"}},
* "denormalization_context"={"groups"={"book_input"}}
* })
* @ApiResource(
* normalizationContext={"groups"={"book:output"}},
* denormalizationContext={"groups"={"book:input"}}
* )
*/
class Book
{
Expand All @@ -311,7 +321,7 @@ class Book
*
* @var bool
*
* @Groups({"book_output", "admin_input"})
* @Groups({"book:output", "admin:input"})
*/
private $active = false;

Expand All @@ -320,7 +330,7 @@ class Book
*
* @var string
*
* @Groups({"book_output", "book_input"})
* @Groups({"book:output", "book:input"})
*/
private $name;

Expand All @@ -329,7 +339,7 @@ class Book
```

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
dynamically add `admin_input` to deserialization groups.
dynamically add `admin:input` to deserialization groups.

API Platform implements a `ContextBuilder`, which prepares the context for serialization & deserialization. Let's
[decorate this service](http://symfony.com/doc/current/service_container/service_decoration.html) to override the
Expand Down Expand Up @@ -612,9 +622,7 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(
* attributes={"normalization_context"={"jsonld_embed_context"=true}
* })
* @ApiResource(normalizationContext={"jsonld_embed_context"=true})
*/
class Book
{
Expand Down
4 changes: 2 additions & 2 deletions core/swagger.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ thanks to the `swagger_definition_name` option:
* collectionOperations={
* "post"={
* "denormalization_context"={
* "groups"={"user_read"},
* "groups"={"user:read"},
* "swagger_definition_name": "Read",
* },
* },
Expand All @@ -219,7 +219,7 @@ It's also possible to re-use the (`de`)`normalization_context`:
class User
{
const API_WRITE = [
'groups' => ['user_read'],
'groups' => ['user:read'],
'swagger_definition_name' => 'Read',
];
}
Expand Down