Skip to content

Commit e70fc5c

Browse files
authored
Add mandatory id and annotations (#1080)
1 parent 29d5610 commit e70fc5c

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

core/dto.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use App\Dto\BookOutput;
2727
*/
2828
final class Book
2929
{
30+
public $id;
3031
}
3132
```
3233

@@ -63,6 +64,8 @@ Similarly, the `output` attribute is used during [the serialization process](ser
6364

6465
The `input` and `output` attributes are taken into account by all the documentation generators (GraphQL and OpenAPI, Hydra).
6566

67+
Note that `Book` entity needs an id property. The simplest way is adding a public property called `$id`, as in the example. However, as in any other entity, you can use a private property, with getter and setter functions, and/or named it as you wish, provided you annotate it with `@ApiProperty(identifier=true)`. For instance, you could have a property called `$code`. So the `InputDataTransformer` actually transforms the isbn into a code. And then in the `OutputDataTransformer`, from this code into the name.
68+
6669
To create a `Book`, we `POST` a data structure corresponding to the `BookInput` class and get back in the response a data structure corresponding to the `BookOutput` class:
6770

6871
![Diagram post input output](images/diagrams/api-platform-post-i-o.png)
@@ -78,7 +81,10 @@ We have the following `BookInput`:
7881
namespace App\Dto;
7982
8083
final class BookInput {
81-
public $isbn;
84+
/**
85+
* @var string
86+
*/
87+
public $isbn;
8288
}
8389
```
8490

@@ -142,7 +148,10 @@ To manage the output, it's exactly the same process. For example, we have the fo
142148
namespace App\Dto;
143149
144150
final class BookOutput {
145-
public $name;
151+
/**
152+
* @var string
153+
*/
154+
public $name;
146155
}
147156
```
148157

0 commit comments

Comments
 (0)