|
| 1 | +# Deprecating Resources and Properties (Alternative to Versioning) |
| 2 | + |
| 3 | +A best practice regarding web APIs development is to apply [the evolution strategy](https://philsturgeon.uk/api/2018/05/02/api-evolution-for-rest-http-apis/) |
| 4 | +to indicate to client applications which resource types, operations and fields are deprecated and shouldn't be used anymore. |
| 5 | + |
| 6 | +While versioning an API requires modifying all clients to upgrade, even the ones not impacted by the changes. |
| 7 | +It's a tedious task that should be avoided as much as possible. |
| 8 | + |
| 9 | +On the other hand, the evolution strategy (also known as versionless APIs) consists of deprecating the fields, resources |
| 10 | +types or operations that will be removed at some point. |
| 11 | + |
| 12 | +Most modern API formats including [JSON-LD / Hydra](content-negotiation.md), [GraphQL](graphql.md) and [OpenAPI](swagger.md) |
| 13 | +allow to mark resources types, operations or fields as deprecated. |
| 14 | + |
| 15 | +## Deprecating Resource Classes, Operations and Properties |
| 16 | + |
| 17 | +When using API Platform, it's easy to mark a whole resource, a specific operation or a specific property as deprecated. |
| 18 | +All documentation formats mentioned in the introduction will then automatically take the deprecation into account. |
| 19 | + |
| 20 | +To deprecate a resource class, use the `deprecationReason` attribute: |
| 21 | + |
| 22 | +```php |
| 23 | +<?php |
| 24 | +// api/src/Entity/Parchment.php |
| 25 | + |
| 26 | +namespace App\Entity; |
| 27 | + |
| 28 | +use ApiPlatform\Core\Annotation\ApiResource; |
| 29 | + |
| 30 | +/** |
| 31 | + * @ApiResource(deprecationReason="Create a Book instead") |
| 32 | + */ |
| 33 | +class Parchment |
| 34 | +{ |
| 35 | + // ... |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +As you can see, to deprecate a resource, we just have to explain what the client should do to upgrade in the dedicated attribute. |
| 40 | + |
| 41 | +The deprecation will automatically be taken into account by clients supporting the previously mentioned format, including |
| 42 | +[API Platform Admin](../admin/index.md), [API Platform Client Generator](../client-generator/index.md) and the lower level |
| 43 | +[api-doc-parser library](https://github.com/api-platform/api-doc-parser). |
| 44 | + |
| 45 | +Here is how it renders for OpenAPI in the built-in Swagger UI shipped with the framework: |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +And now in the built-in version of GraphiQL (for GraphQL APIs): |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +You can also use this new `deprecationReason` attribute to deprecate specific [operations](operations.md): |
| 54 | + |
| 55 | +```php |
| 56 | +<?php |
| 57 | +// api/src/Entity/Review.php |
| 58 | + |
| 59 | +/** |
| 60 | + * @ApiResource(itemOperations={ |
| 61 | + * "get"={"deprecation_reason"="Retrieve a Book instead"} |
| 62 | + * }) |
| 63 | + */ |
| 64 | +class Parchment |
| 65 | +{ |
| 66 | + // ... |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +It's also possible to deprecate a single property: |
| 71 | + |
| 72 | +```php |
| 73 | +<?php |
| 74 | +// api/src/Entity/Review.php |
| 75 | + |
| 76 | +namespace App\Entity; |
| 77 | + |
| 78 | +use ApiPlatform\Core\Annotation\ApiProperty; |
| 79 | +use ApiPlatform\Core\Annotation\ApiResource; |
| 80 | + |
| 81 | +/** |
| 82 | + * @ApiResource |
| 83 | + */ |
| 84 | +class Review |
| 85 | +{ |
| 86 | + // ... |
| 87 | + |
| 88 | + /** |
| 89 | + * @ApiProperty(deprecationReason="Use the rating property instead") |
| 90 | + */ |
| 91 | + public $letter; |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +* With JSON-lD / Hydra, [an `owl:deprecated` annotation property](https://www.w3.org/TR/owl2-syntax/#Annotation_Properties) will be added to the appropriate data structure |
| 96 | +* With Swagger / OpenAPI, [a `deprecated` property](https://swagger.io/docs/specification/2-0/paths-and-operations/) will be added |
| 97 | +* With GraphQL, the [`isDeprecated` and `deprecationReason` properties](https://facebook.github.io/graphql/June2018/#sec-Deprecation) will be added to the schema |
0 commit comments