Skip to content

Update annotations of Subresources #1262

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
Jan 28, 2021
Merged
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
74 changes: 33 additions & 41 deletions core/subresources.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ApiResource
*/
#[ApiResource]
class Answer
{
/**
Expand Down Expand Up @@ -60,8 +60,8 @@ use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ApiResource
*/
#[ApiResource]
class Question
{
/**
Expand All @@ -79,8 +79,8 @@ class Question
/**
* @ORM\OneToOne(targetEntity="Answer", inversedBy="question")
* @ORM\JoinColumn(referencedColumnName="id", unique=true)
* @ApiSubresource
*/
#[ApiSubresource]
public $answer;

public function getId(): ?int
Expand All @@ -104,7 +104,7 @@ App\Entity\Question:
```
[/codeSelector]
Note that all we had to do is to set up `@ApiSubresource` on the `Question::answer` relation. Because the `answer` is a to-one relation, we know that this subresource is an item. Therefore the response will look like this:
Note that all we had to do is to set up `#[ApiSubresource]` on the `Question::answer` relation. Because the `answer` is a to-one relation, we know that this subresource is an item. Therefore the response will look like this:

```json
{
Expand Down Expand Up @@ -136,14 +136,16 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ApiResource(subresourceOperations={
* "api_questions_answer_get_subresource"={
* "method"="GET",
* "normalization_context"={"groups"={"foobar"}}
* }
* })
*/
#[ApiResource(
subresourceOperations: [
'api_questions_answer_get_subresource': [
'method' => 'GET',
'normalization_context': [
'groups': ['foobar'],
],
],
],
)]
class Answer
{
// ...
Expand Down Expand Up @@ -198,17 +200,14 @@ You can control the path of subresources with the `path` option of the `subresou
<?php
// api/src/Entity/Question.php
/**
* ...
* @ApiResource(
* subresourceOperations={
* "api_questions_answer_get_subresource"={
* "method"="GET",
* "path"="/questions/{id}/all-answers"
* },
* },
* )
*/
#[ApiResource(
subresourceOperations: [
'api_questions_answer_get_subresource': [
'method' => 'GET',
'path' => '/questions/{id}/all-answers',
],
],
)]
class Question
{
}
Expand All @@ -222,16 +221,13 @@ The `subresourceOperations` attribute also allows you to add an access control o
<?php
// api/src/Entity/Answer.php
/**
* ...
* @ApiResource(
* subresourceOperations={
* "api_questions_answer_get_subresource"= {
* "security"="has_role('ROLE_AUTHENTICATED')"
* }
* }
* )
*/
#[ApiResource(
subresourceOperations: [
'api_questions_answer_get_subresource': [
'security' => "has_role('ROLE_AUTHENTICATED')",
],
],
)]
class Answer
{
}
Expand All @@ -250,16 +246,12 @@ use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
/**
* ...
* @ApiResource
*/
#[ApiResource]
class Question
{
/**
* ...
* @ApiSubresource(maxDepth=1)
*/
#[ApiSubresource(
maxDepth: 1,
)]
public $answer;
// ...
Expand Down