Skip to content

Commit ecd9d49

Browse files
authored
feat: PUT is now disabled by default (#1986)
1 parent 526fa56 commit ecd9d49

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

core/getting-started.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,14 @@ Method | URL | Description
156156
GET | /products | Retrieve the (paginated) collection
157157
POST | /products | Create a new product
158158
GET | /products/{id} | Retrieve a product
159-
PUT | /products/{id} | Update a product
160159
PATCH | /products/{id} | Apply a partial modification to a product
161160
DELETE | /products/{id} | Delete a product
162161

162+
> [!NOTE]
163+
>
164+
> `PUT` (replace or create) isn't registered automatically,
165+
> but is entirely supported by API Platform and can be added explicitly.
166+
163167
The same operations are available for the offer method (routes will start with the `/offers` pattern).
164168
Route prefixes are built by pluralizing the name of the mapped entity class.
165169
It is also possible to override the naming convention using [operation path namings](operation-path-naming.md).

core/operations.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,30 @@ is also possible.
1717

1818
There are two types of operations: collection operations and item operations.
1919

20-
Collection operations act on a collection of resources. By default two routes are implemented: `POST` and `GET`. Item
21-
operations act on an individual resource. Four default routes are defined: `GET`, `PUT`, `DELETE` and `PATCH`. `PATCH` is supported
20+
Collection operations act on a collection of resources. By default two operations are implemented: `POST` and `GET`. Item
21+
operations act on an individual resource. Three default operation are defined: `GET`, `DELETE` and `PATCH`. `PATCH` is supported
2222
with [JSON Merge Patch (RFC 7396)](https://www.rfc-editor.org/rfc/rfc7386), or [using the JSON:API format](https://jsonapi.org/format/#crud-updating), as required by the specification.
2323

24+
The `PUT` operation is also supported, but is not registered by default.
25+
2426
When the `ApiPlatform\Metadata\ApiResource` annotation is applied to an entity class, the following built-in CRUD
2527
operations are automatically enabled:
2628

2729
Collection operations:
2830

29-
Method | Mandatory | Description
30-
-------|-----------|------------------------------------------
31-
`GET` | yes | Retrieve the (paginated) list of elements
32-
`POST` | no | Create a new element
31+
Method | Mandatory | Description | Registered by default
32+
-------|-----------|-------------------------------------------|----------------------
33+
`GET` | yes | Retrieve the (paginated) list of elements | yes
34+
`POST` | no | Create a new element | yes
3335

3436
Item operations:
3537

36-
Method | Mandatory | Description
37-
---------|-----------|-------------------------------------------
38-
`GET` | yes | Retrieve an element
39-
`PUT` | no | Replace an element
40-
`PATCH` | no | Apply a partial modification to an element
41-
`DELETE` | no | Delete an element
38+
Method | Mandatory | Description | Registered by default
39+
---------|-----------|--------------------------------------------|----------------------
40+
`GET` | yes | Retrieve an element | yes
41+
`PUT` | no | Replace an element | no
42+
`PATCH` | no | Apply a partial modification to an element | yes
43+
`DELETE` | no | Delete an element | yes
4244

4345
Note: the `PATCH` method must be enabled explicitly in the configuration, refer to the [Content Negotiation](content-negotiation.md) section for more information.
4446

@@ -563,7 +565,7 @@ class Weather
563565
// ...
564566
```
565567

566-
This will expose the `Weather` model, but also all the default CRUD routes: `GET`, `PUT`, `PATCH`, `DELETE` and `POST`, which is nonsense in our context.
568+
This will expose the `Weather` model, but also all the default CRUD routes: `GET`, `PATCH`, `DELETE` and `POST`, which is nonsense in our context.
567569
Since we are required to expose at least one route, let's expose just one:
568570

569571
```php

core/serialization.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ basis. API Platform will always use the most specific definition. For instance,
189189
at the resource level and at the operation level, the configuration set at the operation level will be used and the resource
190190
level ignored.
191191

192-
In the following example we use different serialization groups for the `GET` and `PUT` operations:
192+
In the following example we use different serialization groups for the `GET` and `PATCH` operations:
193193

194194
<code-selector>
195195

@@ -200,15 +200,15 @@ namespace App\Entity;
200200
201201
use ApiPlatform\Metadata\ApiResource;
202202
use ApiPlatform\Metadata\Get;
203-
use ApiPlatform\Metadata\Put;
203+
use ApiPlatform\Metadata\Patch;
204204
use Symfony\Component\Serializer\Annotation\Groups;
205205
206206
#[ApiResource(normalizationContext: ['groups' => ['get']])]
207207
#[Get]
208-
#[Put(normalizationContext: ['groups' => ['put']])]
208+
#[Patch(normalizationContext: ['groups' => ['patch']])]
209209
class Book
210210
{
211-
#[Groups(['get', 'put'])]
211+
#[Groups(['get', 'patch'])]
212212
public $name;
213213
214214
#[Groups('get')]
@@ -225,15 +225,15 @@ App\Entity\Book:
225225
groups: ['get']
226226
operations:
227227
ApiPlatform\Metadata\Get: ~
228-
ApiPlatform\Metadata\Put:
228+
ApiPlatform\Metadata\Patch:
229229
normalizationContext:
230-
groups: ['put']
230+
groups: ['patch']
231231
232232
# api/config/serializer/Book.yaml
233233
App\Entity\Book:
234234
attributes:
235235
name:
236-
groups: ['get', 'put']
236+
groups: ['get', 'patch']
237237
author:
238238
groups: ['get']
239239
```
@@ -257,12 +257,12 @@ App\Entity\Book:
257257
</normalizationContext>
258258
<operations>
259259
<operation class="ApiPlatform\Metadata\Get" />
260-
<operation class="ApiPlatform\Metadata\Put">
260+
<operation class="ApiPlatform\Metadata\Patch">
261261
<normalizationContext>
262262
<values>
263263
<value name="groups">
264264
<values>
265-
<value>put</value>
265+
<value>patch</value>
266266
</values>
267267
</value>
268268
</values>
@@ -281,7 +281,7 @@ App\Entity\Book:
281281
<class name="App\Entity\Book">
282282
<attribute name="name">
283283
<group>get</group>
284-
<group>put</group>
284+
<group>patch</group>
285285
</attribute>
286286
<attribute name="author">
287287
<group>get</group>
@@ -293,7 +293,7 @@ App\Entity\Book:
293293
</code-selector>
294294

295295
The `name` and `author` properties will be included in the document generated during a `GET` operation because the configuration
296-
defined at the resource level is inherited. However the document generated when a `PUT` request will be received will only
296+
defined at the resource level is inherited. However the document generated when a `PATCH` request will be received will only
297297
include the `name` property because of the specific configuration for this operation.
298298

299299
Refer to the [operations](operations.md) documentation to learn more.

distribution/index.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -494,9 +494,11 @@ In Swagger UI, click on the `POST` operation of the `Book` resource type, click
494494
You just saved a new book resource through the bookshop API! API Platform automatically transforms the JSON document to
495495
an instance of the corresponding PHP entity class and uses Doctrine ORM to persist it in the database.
496496

497-
By default, the API supports `GET` (retrieve, on collections and items), `POST` (create), `PUT` (replace), `PATCH` (partial update) and `DELETE` (self-explanatory)
497+
By default, the API supports `GET` (retrieve, on collections and items), `POST` (create), `PATCH` (partial update) and `DELETE` (self-explanatory)
498498
HTTP methods. Don't forget to [disable the ones you don't want](../core/operations.md#enabling-and-disabling-operations)!
499499

500+
The `PUT` (replace or create) method is also supported, but is not enabled by default.
501+
500502
Try the `GET` operation on the collection. The book we added appears. When the collection contains more than 30 items,
501503
the pagination will automatically show up, [and this is entirely configurable](../core/pagination.md). You may be interested
502504
in [adding some filters and adding sorts to the collection](../core/filters.md) as well.

0 commit comments

Comments
 (0)