Skip to content

Change annotations to attributes #1428

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 8 commits into from
Oct 12, 2021
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
4 changes: 2 additions & 2 deletions admin/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
#[ApiResource]
#[ApiResource]
class Author
{
/**
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
* @ORM\Id
* @ApiFilter(SearchFilter::class, strategy="exact")
*/
#[ApiFilter(SearchFilter::class, strategy: "exact")]
public $id;

/**
Expand Down
4 changes: 1 addition & 3 deletions admin/schema.org.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ To configure which property should be shown to represent your entity, map the pr
```php
// api/src/Entity/Person.php

/**
* @ApiProperty(iri="http://schema.org/name")
*/
#[ApiProperty(iri="http://schema.org/name")]
private $name;
```

Expand Down
16 changes: 4 additions & 12 deletions admin/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ For instance, with API Platform Core as backend, if you write the following:
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ApiResource
*/
#[ApiResource]
class Book
{
/**
* @Assert\NotBlank
*/
#[Assert\NotBlank]
public ?string $title = null;
}
```
Expand Down Expand Up @@ -51,14 +47,10 @@ For example if you have this code:
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ApiResource
*/
#[ApiResource]
class Book
{
/**
* @Assert\Isbn
*/
#[Assert\Isbn]
public ?string $isbn = null;
}
```
Expand Down
2 changes: 1 addition & 1 deletion core/data-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Both implementations can also implement a third, optional, interface called
if you want to limit their effects to a single resource or operation.

In the following examples we will create custom data providers for an entity class called `App\Entity\BlogPost`.
Note, that if your entity is not Doctrine-related, you need to flag the identifier property by using `@ApiProperty(identifier=true)` for things to work properly (see also [Entity Identifier Case](serialization.md#entity-identifier-case)).
Note, that if your entity is not Doctrine-related, you need to flag the identifier property by using `#[ApiProperty(identifier: true)` for things to work properly (see also [Entity Identifier Case](serialization.md#entity-identifier-case)).

## Custom Collection Data Provider

Expand Down
33 changes: 18 additions & 15 deletions core/default-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(attributes={"order"={"foo": "ASC"}})
*/
#[ApiResource(order: ["foo" => "ASC"])]
class Book
{
// ...
Expand Down Expand Up @@ -52,8 +50,7 @@ namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(attributes={"order"={"foo", "bar"}})
#[ApiResource(order: ["foo", "bar"])]
*/
class Book
{
Expand Down Expand Up @@ -93,9 +90,7 @@ namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(attributes={"order"={"author.username"}})
*/
#[ApiResource(order: ["author.username"])]
class Book
{
// ...
Expand Down Expand Up @@ -123,13 +118,21 @@ Another possibility is to apply the default order for a specific collection oper
[codeSelector]

```php
/**
* collectionOperations={
* "get",
* "get_desc_custom"={"method"="GET", "path"="custom_collection_desc_foos", "order"={"name"="DESC"}},
* "get_asc_custom"={"method"="GET", "path"="custom_collection_asc_foos", "order"={ "name"="ASC"}},
* }
*/
#[ApiResource(
collectionOperations: [
"get",
"get_desc_custom" => [
"method" => "GET",
"path" => "custom_collection_desc_foos",
"order" => ["name" => "DESC"]
],
"get_asc_custom" => [
"method" => "GET",
"path" => "custom_collection_asc_foos",
"order" => ["name" => "ASC"]
],
]
)]
class Book
{
// ...
Expand Down
42 changes: 14 additions & 28 deletions core/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(deprecationReason="Create a Book instead")
*/
#[ApiResource(deprecationReason: "Create a Book instead")]
class Parchment
{
// ...
Expand Down Expand Up @@ -60,11 +58,7 @@ namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(itemOperations={
* "get"={"deprecation_reason"="Retrieve a Book instead"}
* })
*/
#[ApiResource(itemOperations: [ "get" => ["deprecation_reason" => "Retrieve a Book instead"])]
class Parchment
{
// ...
Expand All @@ -84,16 +78,12 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource
*/
#[ApiResource]
class Review
{
// ...

/**
* @ApiProperty(deprecationReason="Use the rating property instead")
*/
#[ApiProperty(deprecationReason: "Use the rating property instead")]
public $letter;

// ...
Expand Down Expand Up @@ -133,12 +123,10 @@ namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(
* deprecationReason="Create a Book instead",
* sunset="01/01/2020"
* )
*/
#[ApiResource(
deprecationReason: "Create a Book instead",
sunset: "01/01/2020"
)]
class Parchment
{
// ...
Expand All @@ -158,14 +146,12 @@ namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(itemOperations={
* "get"={
* "deprecation_reason"="Retrieve a Book instead",
* "sunset"="01/01/2020"
* }
* })
*/
#[ApiResource(itemOperations: [
"get" => [
"deprecation_reason" => "Retrieve a Book instead",
"sunset" => "01/01/2020"
]
])]
class Parchment
{
// ...
Expand Down
47 changes: 19 additions & 28 deletions core/dto.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Using Data Transfer Objects (DTOs)

As stated in [the general design considerations](design.md), in most cases [the DTO pattern](https://en.wikipedia.org/wiki/Data_transfer_object) should be implemented using an API Resource class representing the public data model exposed through the API and [a custom data provider](data-providers.md). In such cases, the class marked with `@ApiResource` will act as a DTO.
As stated in [the general design considerations](design.md), in most cases [the DTO pattern](https://en.wikipedia.org/wiki/Data_transfer_object) should be implemented using an API Resource class representing the public data model exposed through the API and [a custom data provider](data-providers.md). In such cases, the class marked with `#[ApiResource]` will act as a DTO.

However, it's sometimes useful to use a specific class to represent the input or output data structure related to an operation.

Expand All @@ -21,12 +21,7 @@ use ApiPlatform\Core\Annotation\ApiResource;
use App\Dto\BookInput;
use App\Dto\BookOutput;

/**
* @ApiResource(
* input=BookInput::class,
* output=BookOutput::class
* )
*/
#[ApiResource(input: BookInput::class, output: BookOutput::class)]
final class Book
{
public $id;
Expand Down Expand Up @@ -375,24 +370,22 @@ use App\Dto\BookOutput;
use App\Dto\CreateBook;
use App\Dto\UpdateBook;

/**
* @ApiResource(
* collectionOperations={
* "create"={
* "method"="POST",
* "input"=CreateBook::class,
* "output"=BookOutput::class
* }
* },
* itemOperations={
* "update"={
* "method"="PUT",
* "input"=UpdateBook::class,
* "output"=BookOutput::class
* }
* }
* )
*/
#[ApiResource(
collectionOperations: [
"create" => [
"method" => "POST",
"input" => CreateBook::class,
"output" => BookOutput::class
],
],
itemOperations: [
"update" => [
"method" => "PUT",
"input" => UpdateBook::class,
"output" => BookOutput::class,
],
],
)]
final class Book
{
}
Expand Down Expand Up @@ -464,9 +457,7 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Model\Attribute;

/**
* @ApiResource
*/
#[ApiResource]
final class Book
{
/**
Expand Down
42 changes: 17 additions & 25 deletions core/extending-jsonld-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,24 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(iri="http://schema.org/Book")
*/
#[ApiResource(iri: "http://schema.org/Book")]
class Book
{
// ...

/**
* ...
* @ApiProperty(
* iri="http://schema.org/name",
* attributes={
* "jsonld_context"={
* "@id"="http://yourcustomid.com",
* "@type"="http://www.w3.org/2001/XMLSchema#string",
* "someProperty"={
* "a"="textA",
* "b"="textB"
* }
* }
* }
* )
*/
#[ApiProperty(
iri: "http://schema.org/name",
attributes: [
"jsonld_context" => [
"@id" => "http://yourcustomid.com",
"@type" => "http://www.w3.org/2001/XMLSchema#string",
"someProperty" => [
"a" => "textA",
"b" => "textB"
]
]
]
)]
public $name;

// ...
Expand Down Expand Up @@ -83,12 +78,9 @@ It's also possible to replace the Hydra context used by the documentation genera

use ApiPlatform\Core\Annotation\ApiResource;

/**
* ...
* @ApiResource(itemOperations={
* "get"={"hydra_context"={"foo"="bar"}}
* })
*/
#[ApiResource(itemOperations: [
"get" => ["hydra_context" => ["foo" => "bar"]]
])]
class Book
{
//...
Expand Down
8 changes: 2 additions & 6 deletions core/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource
*/
#[ApiResource]
class User
{
// ...
Expand All @@ -45,9 +43,7 @@ namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource
*/
#[ApiResource]
class Offer
{
/**
Expand Down
9 changes: 2 additions & 7 deletions core/external-vocabularies.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@ namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(iri="http://schema.org/Book")
*/
#[ApiResource(iri: "http://schema.org/Book")]
class Book
{
// ...

/**
* ...
* @ApiProperty(iri="http://schema.org/name")
*/
#[ApiProperty(iri: "http://schema.org/name")]
public $name;

// ...
Expand Down
Loading