Skip to content

Commit f4da9bd

Browse files
julienfalquedunglas
authored andcommitted
Document per operation DTO usage (#960)
* Document per operation DTO usage * Update dto.md
1 parent 5a224f4 commit f4da9bd

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

core/dto.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,91 @@ services:
262262
Both the `input` and the `output` attributes can be set to `false`. If `input` is `false`, the deserialization process
263263
will be skipped. If `output` is `false`, the serialization process will be skipped.
264264

265+
## Per Operation `input` and `output`
266+
267+
`input` and `output` attributes can be set on a per operation basis:
268+
269+
```php
270+
<?php
271+
// api/src/Entity/Book.php
272+
273+
namespace App\Entity;
274+
275+
use ApiPlatform\Core\Annotation\ApiResource;
276+
use App\Dto\BookOutput;
277+
use App\Dto\CreateBook;
278+
use App\Dto\UpdateBook;
279+
280+
/**
281+
* @ApiResource(
282+
* collectionOperations={
283+
* "create"={
284+
* "method"="POST",
285+
* "input"=CreateBook::class,
286+
* "output"=BookOutput::class
287+
* }
288+
* },
289+
* itemOperations={
290+
* "update"={
291+
* "method"="PUT",
292+
* "input"=UpdateBook::class,
293+
* "output"=BookOutput::class
294+
* }
295+
* }
296+
* )
297+
*/
298+
final class Book
299+
{
300+
}
301+
```
302+
303+
Or using XML:
304+
305+
```xml
306+
<?xml version="1.0" encoding="UTF-8" ?>
307+
<!-- api/config/api_platform/resources.xml -->
308+
309+
<resources xmlns="https://api-platform.com/schema/metadata"
310+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
311+
xsi:schemaLocation="https://api-platform.com/schema/metadata
312+
https://api-platform.com/schema/metadata/metadata-2.0.xsd">
313+
<resource class="App\Entity\Book">
314+
<collectionOperations>
315+
<collectionOperation name="create">
316+
<attribute name="method">POST</attribute>
317+
<attribute name="input">App\Dto\CreateBook</attribute>
318+
<attribute name="output">App\Dto\BookOutput</attribute>
319+
</collectionOperation>
320+
</collectionOperations>
321+
<itemOperations>
322+
<itemOperation name="update">
323+
<attribute name="method">PUT</attribute>
324+
<attribute name="input">App\Dto\UpdateBook</attribute>
325+
<attribute name="output">App\Dto\BookOutput</attribute>
326+
</itemOperation>
327+
</itemOperations>
328+
</resource>
329+
</resources>
330+
```
331+
332+
Or using YAML:
333+
334+
```yaml
335+
# api/config/api_platform/resources.yaml
336+
resources:
337+
App\Entity\Book:
338+
collectionOperations:
339+
create:
340+
method: POST,
341+
input: App\Dto\CreateBook,
342+
output: App\Dto\BookOutput
343+
itemOperations:
344+
update:
345+
method: PUT,
346+
input: App\Dto\UpdateBook,
347+
output: App\Dto\BookOutput
348+
```
349+
265350
## Input/Output Metadata
266351

267352
When specified, `input` and `output` attributes support:

0 commit comments

Comments
 (0)