Deprecating Resources and Properties (Alternative to Versioning)

A best practice regarding web API development is to apply the evolution strategy to indicate to client applications which resource types, operations and fields are deprecated and shouldn't be used anymore.

Web API 開発に関するベスト プラクティスは、進化戦略を適用して、どのリソース タイプ、操作、およびフィールドが非推奨であり、今後使用すべきでないかをクライアント アプリケーションに示すことです。

While versioning an API requires modifying all clients to upgrade, even the ones not impacted by the changes. It's a tedious task that should be avoided as much as possible.

API のバージョンを管理するには、すべてのクライアントを変更してアップグレードする必要がありますが、変更の影響を受けないクライアントも含めて、可能な限り回避する必要があります。

On the other hand, the evolution strategy (also known as versionless APIs) consists of deprecating the fields, resources types or operations that will be removed at some point.

一方、進化戦略 (バージョンレス API とも呼ばれます) は、ある時点で削除されるフィールド、リソースタイプ、または操作を非推奨にすることで構成されます。

Most modern API formats including JSON-LD / Hydra, GraphQL and OpenAPI allow you to mark resources types, operations or fields as deprecated.

JSON-LD / Hydra、GraphQL、OpenAPI などの最新の API 形式のほとんどでは、リソースのタイプ、操作、またはフィールドを非推奨としてマークすることができます。

Deprecating Resource Classes, Operations and Properties

When using API Platform, it's easy to mark a whole resource, a specific operation or a specific property as deprecated. All documentation formats mentioned in the introduction will then automatically take the deprecation into account.

API プラットフォームを使用すると、リソース全体、特定の操作、または特定のプロパティを非推奨としてマークするのは簡単です。

To deprecate a resource class, use the deprecationReason attribute:

リソース クラスを非推奨にするには、deprecationReason 属性を使用します。

<?php
// api/src/Entity/Parchment.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;

#[ApiResource(deprecationReason: "Create a Book instead")]
class Parchment
{
    // ...
}

As you can see, to deprecate a resource, we just have to explain what the client should do to upgrade in the dedicated attribute.

ご覧のとおり、リソースを非推奨にするには、専用属性でクライアントがアップグレードするために何をすべきかを説明するだけです。

The deprecation will automatically be taken into account by clients supporting the previously mentioned format, including Admin, clients created with Create Client and the lower level api-doc-parser library.

非推奨は、Admin、Create Client で作成されたクライアント、および下位レベルの api-doc-parser ライブラリを含む、前述の形式をサポートするクライアントによって自動的に考慮されます。

Here is how it renders for OpenAPI in the built-in Swagger UI shipped with the framework:

フレームワークに同梱されている組み込みの Swagger UI で OpenAPI をレンダリングする方法を次に示します。

Deprecation shown in Swagger UI

And now in the built-in version of GraphiQL (for GraphQL APIs):

そして今、組み込みバージョンの GraphiQL (GraphQL API 用) で:

Deprecation shown in GraphiQL

You can also use this new deprecationReason attribute to deprecate specific operations:

この新しい deprecationReason 属性を使用して、特定の操作を廃止することもできます。

<?php
// api/src/Entity/Parchment.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;

#[ApiResource]
#[Get(deprecationReason: 'Retrieve a Book instead')]
class Parchment
{
    // ...
}

It's also possible to deprecate a single property:

単一のプロパティを廃止することも可能です:

[codeSelector]

[コードセレクター]

<?php
// api/src/Entity/Review.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\ApiProperty;

#[ApiResource]
class Review
{
    // ...

    #[ApiProperty(deprecationReason: "Use the rating property instead")]
    public $letter;

    // ...
}
# api/config/api_platform/resources/Review.yaml
properties:
    # ...
    App\Entity\Review:
        # ...
        letter:
            deprecationReason: 'Use the rating property instead'

[/codeSelector]

[/コードセレクター]

  • With JSON-lD / Hydra, an owl:deprecated annotation property will be added to the appropriate data structure
    JSON-ID / Hydra では、owl:deprecated アノテーション プロパティが適切なデータ構造に追加されます。
  • With Swagger / OpenAPI, a deprecated property will be added
    Swagger / OpenAPI では、非推奨のプロパティが追加されます
  • With GraphQL, the isDeprecated and deprecationReason properties will be added to the schema
    GraphQL では、isDeprecated および deprecationReason プロパティがスキーマに追加されます

Setting the Sunset HTTP Header to Indicate When a Resource or an Operation Will Be Removed

The Sunset HTTP response header indicates that a URI is likely to become unresponsive at a specified point in the future. It is especially useful to indicate when a deprecated URL will not be available anymore.

Sunset HTTP 応答ヘッダーは、URI が将来特定の時点で応答しなくなる可能性があることを示します。これは、非推奨の URL がいつ利用できなくなるかを示すのに特に役立ちます。

Thanks to the sunset attribute, API Platform makes it easy to set this header for all URLs related to a resource class:

Sunset 属性のおかげで、API プラットフォームでは、リソース クラスに関連するすべての URL にこのヘッダーを簡単に設定できます。

<?php
// api/src/Entity/Parchment.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;

#[ApiResource(
    deprecationReason: 'Create a Book instead',
    sunset: '01/01/2020'
)]
class Parchment
{
    // ...
}

The value of the sunset attribute can be any string compatible with the \DateTime constructor. It will be automatically converted to a valid HTTP date.

Sunset 属性の値は、\DateTime コンストラクターと互換性のある任意の文字列にすることができます。有効な HTTP 日付に自動的に変換されます。

It's also possible to set the Sunset header only for a specific operation:

特定の操作に対してのみ Sunset ヘッダーを設定することもできます。

<?php
// api/src/Entity/Parchment.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;

#[ApiResource]
#[Get(
    deprecationReason: 'Retrieve a Book instead',
    sunset: '01/01/2020'
)]
class Parchment
{
    // ...
}