Skip to content

Commit bcb31f0

Browse files
romainallanotPierreThibaudeau
andauthored
docs: Upgrade guide (#1424)
* docs: Upgrade guide * Apply suggestions from code review Co-authored-by: PierreThibaudeau <[email protected]> Co-authored-by: PierreThibaudeau <[email protected]>
1 parent d81778d commit bcb31f0

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

core/upgrade-guide.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Upgrade guide
2+
3+
## What changed between 2.6 and 2.7?
4+
5+
- New Resource metadata allowing to declare multiple Resources on a class: `ApiPlatform\Metadata\ApiResource`
6+
- Clarification of some properties within the ApiResource declaration
7+
- Removal of the item and collection difference on operation declaration
8+
- `ApiPlatform\Core\DataProvider\...DataProviderInterface` has a new
9+
interface `ApiPlatform\State\ProviderInterface`
10+
- `ApiPlatform\Core\DataPersister\...DataPersisterInterface` has a new
11+
interface `ApiPlatform\State\ProcessorInterface`
12+
- New ApiProperty metadata `ApiPlatform\Metadata\ApiProperty`
13+
- Configuration flag `metadata_backward_compatibility_layer` that allows
14+
the use of legacy metadata layers
15+
16+
The detailed changes are present in the [CHANGELOG](https://github.com/api-platform/core/blob/main/CHANGELOG.md).
17+
18+
### ApiResource Metadata
19+
20+
The `ApiResource` annotation has a new namespace:
21+
`ApiPlatform\Metadata\ApiResource` instead of `ApiPlatform\Core\Annotation\ApiResource`.
22+
23+
For example, the Book resource in 2.6:
24+
25+
```php
26+
<?php
27+
// api/src/Entity/Book.php
28+
namespace App\Entity;
29+
30+
use ApiPlatform\Core\Annotation\ApiResource;
31+
32+
#[ApiResource(
33+
iri: 'http://schema.org/Book',
34+
itemOperations: [
35+
'get',
36+
'post_publication' => [
37+
'method' => 'POST',
38+
'path' => '/books/{id}/publication',
39+
],
40+
])
41+
]
42+
class Book
43+
{
44+
// ...
45+
}
46+
```
47+
48+
Becomes in 2.7:
49+
50+
```php
51+
<?php
52+
// api/src/Entity/Book.php
53+
namespace App\Entity;
54+
55+
use ApiPlatform\Metadata\ApiResource;
56+
use ApiPlatform\Metadata\Get;
57+
use ApiPlatform\Metadata\Post;
58+
use App\Controller\CreateBookPublication;
59+
60+
#[ApiResource(types: ['http://schema.org/Book'], operations: [
61+
new Get(),
62+
new Post(name: 'publication', uriTemplate: '/books/{id}/publication')
63+
])]
64+
class Book
65+
{
66+
// ...
67+
}
68+
```
69+
70+
You can use the `api:upgrade-resource` command to upgrade
71+
your resources automatically, [see instructions here](#the-upgrade-command).
72+
73+
### Removal of item/collection operations
74+
75+
We removed the notion of item and collection. Instead, use
76+
http verbs matching the operation you want to declare.
77+
There is also a `collection` flag instructing wether the
78+
operation returns an array or an object.
79+
The default ApiResource attribute still declares a CRUD:
80+
81+
```php
82+
#[ApiResource]
83+
```
84+
85+
is the same as
86+
87+
```php
88+
#[ApiResource(operations=[
89+
new Get(),
90+
new Put(),
91+
new Patch(),
92+
new Delete(),
93+
new GetCollection(),
94+
new Post()
95+
])]
96+
```
97+
98+
### Detailed metadata changes
99+
100+
#### #[ApiResource]
101+
102+
`ApiPlatform\Metadata\ApiResource` instead of `ApiPlatform\Core\Annotation\ApiResource`
103+
104+
|Before|After|
105+
|---|---|
106+
|`iri: 'http://schema.org/Book'`|`types: ['http://schema.org/Book']`|
107+
|`path: '/books/{id}/publication'`|`uriTemplate: '/books/{id}/publication'`|
108+
|`identifiers: []`|`uriVariables: []`|
109+
|`attributes: []`|`extraProperties: []`|
110+
|`attributes: ['validation_groups' => ['a', 'b']]`|`validationContext: ['groups' => ['a', 'b']]`|
111+
112+
#### #[ApiProperty]
113+
114+
`ApiPlatform\Metadata\ApiProperty` instead of `ApiPlatform\Core\Annotation\ApiProperty`
115+
116+
|Before|After|
117+
|---|---|
118+
|`iri: 'http://schema.org/Book'`|`types: ['http://schema.org/Book']`|
119+
|`type: 'string'`|`builtinTypes: ['string']`|
120+
121+
Note that builtinTypes are computed automatically from php types.
122+
123+
For example:
124+
125+
```php
126+
class Book
127+
{
128+
public string|Isbn $isbn;
129+
}
130+
```
131+
132+
Will compute: `builtinTypes: ['string', Isbn::class]`
133+
134+
## Versions 2.7 and 3.0
135+
136+
In 2.7 the `metadata_backward_compatibility_layer` flag is set to `true`.
137+
This means that all the legacy services will still work just as they used
138+
to work in 2.6 (for example `PropertyMetadataFactoryInterface` or
139+
`ResourceMetadataFactoryInterface`). When updating we advise to first
140+
resolve the deprecations then to set this flag to `false` to use the
141+
new metadata system.
142+
143+
In 3.0 this flag will default to `false` and the legacy code will be removed.
144+
145+
## The upgrade command
146+
147+
The upgrade command will automatically upgrade the old `ApiPlatform\Core\Annotation\ApiResource` to `ApiPlatform\Metadata\ApiResource`.
148+
By default this does a dry run and shows a diff:
149+
150+
```bash
151+
php bin/console api:upgrade-resource
152+
```
153+
154+
To write in-place use the `force` option:
155+
156+
```bash
157+
php bin/console api:upgrade-resource -f
158+
```

0 commit comments

Comments
 (0)