Add polymorphism support (based on discriminator)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 25/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 領域
- api, backend, backend-api-design
調査の方向性
まず PropertyNameCollectionFactory と PropertyMetadataFactory を読み、次に issue に記載されている Serializer の normalizer と schema generator の経路を追ってください。discriminator ベースのサブクラスが自身のプロパティをシリアライズおよびデシリアライズし、要求されている OpenAPI のポリモーフィズム対応が考慮されていれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Description
This was already closed in https://github.com/api-platform/core/issues/2931, but I would like you to reconsider.
Since it's quite limiting not being able to use Doctrine's Inheritance mapping and OpenAPI has nowadays quite good polymorphism support thanks to discriminator, oneOf, anyOf etc, I believe it makes sense to reconsider support of this. At beginning, I think most people would be fine if APIP at least allowed serialization/deserialization of properties in subclasses. Schema generator could be adjusted later to generate things like this
Main issue I would like to solve is that serializer does not see properties from subclasses. After further debugging I found that PropertyNameCollectionFactory and PropertyMetadataFactory are responsible. However, these factories only accept $resourceClassand not an actual class that Serializer is serializing/deserializing, hence it's not possible to simply add own implementation of these factories.
Example
#[ApiResource()]
#[Doctrine\ORM\Mapping\Entity()]
#[Doctrine\ORM\Mapping\InheritanceType('SINGLE_TABLE')]
#[Doctrine\ORM\Mapping\DiscriminatorColumn('type', 'string')]
#[Doctrine\ORM\Mapping\DiscriminatorMap(self::DISCRIMINATOR_MAPPING)]
#[Symfony\Component\Serializer\Attribute\DiscriminatorMap('type', self::DISCRIMINATOR_MAPPING)]
abstract class DatasyncConnector {
private const array DISCRIMINATOR_MAPPING = ["SCIM" => SCIMConnector::class, "JSON" => JSONConnector::class];
public string $type;
}
{}
#[ORM\Entity()]
class SCIMConnector extends DatasyncConnector {
public ?string $foo = null;
}
#[ORM\Entity()]
class JSONConnector extends DatasyncConnector {
public ?string $bar = null;
}
Here, I expect serialization will result in
{"type": "SCIM", "foo": "string"}
or
{"type": "JSON", "bar": "string"}
Similarly, deserialization should accept (and write to) these properties
Further context
Currently, I have added support for this in application I'm maintaining by enabling allow_extra_attributes and implementing custom normalizer:
#[AutoconfigureTag('serializer.normalizer', ['priority' => 1])]
class DatasyncConnectorNormalizer implements NormalizerInterface, DenormalizerInterface
{
public function __construct(
#[Autowire(service: 'serializer.normalizer.object')]
private ObjectNormalizer $normalizer,
#[Autowire(service: 'api_platform.jsonld.normalizer.item')]
private AbstractNormalizer $jsonldNormalizer,
) {}
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
{
return $this->normalizer->denormalize(
$data,
$type,
$format,
[
AbstractNormalizer::OBJECT_TO_POPULATE => $this->jsonldNormalizer
->denormalize($data, $type, $format, $context),
] + $context,
);
}
public function supportsDenormalization(
mixed $data,
string $type,
?string $format = null,
array $context = [],
): bool {
return true;
}
public function normalize(
mixed $data,
?string $format = null,
array $context = [],
): null|array|\ArrayObject|bool|float|int|string {
\assert($data instanceof DatasyncConnector);
$context['uri_variables'] = ['id' => $data->id];
return $this->jsonldNormalizer->normalize($data, $format, $context)
+ $this->normalizer->normalize($data, $format, $context);
}
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return true;
}
public function getSupportedTypes(?string $format): array
{
return [DatasyncConnector::class => true];
}
}
Even OpenAPI schema is adjusted thanks to implementing own OpenApiFactory which generates definitions for subclasses, then linking to them from resource like so
new GetCollection(openapi: new OpenApiOperation(responses: [
200 => new Response(content: new \ArrayObject(['application/ld+json' => [
'schema' => [
'type' => 'array',
'items' => [
'oneOf' => [
['$ref' => self::SCHEMA_PATH_PREFIX.'.jsonld-connector.output'],
['$ref' => self::SCHEMA_PATH_PREFIX.'.jsonld-connector.output_MS_GRAPH'],
],
'discriminator' => [
'propertyName' => 'type',
'mapping' => [
DatasyncConnectorType::SCIM->value
=> self::SCHEMA_PATH_PREFIX.'.jsonld-connector.output',
DatasyncConnectorType::JSON->value
=> self::SCHEMA_PATH_PREFIX.'.jsonld-connector.output',
DatasyncConnectorType::MS_GRAPH->value
=> self::SCHEMA_PATH_PREFIX.'.jsonld-connector.output_MS_GRAPH',
],
],
],
],
]])),
])),
- 主要言語
- PHP
- スター
- 2.6k
- フォーク
- 989
- 平均マージ
- 1日 23時間
- マージ済み PR(30日)
- 102
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
api-platform/core のほかの issue
-
JSON:API: resource schemas (`*.jsonapi`) have `properties` but no `type: object`対応中かも @ousamabenyounes が 1 日前に担当しました。 オープン
難易度 2/5 1時間未満 初心者へのやさしさ 80/100
api-platform/core#8701 ·
メンテナーはふだん 1 日以内に返信
-
JSON:API: `JsonApiCollectionBaseSchemaNoPagination` declares `data` as `type: array` without `items`オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
api-platform/core#8699 ·
メンテナーはふだん 1 日以内に返信
-
api:openapi:export --spec-version=3.0.0 outputs `webhooks`, which is invalid in OpenAPI 3.0対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
api-platform/core#8690 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
api-platform/core#8680 ·
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
api-platform/core#8725 ·
メンテナーはふだん 1 日以内に返信
api-platform/core の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 92/100
メンテナーはふだん 1 日以内に返信
-
Authentication / Authorization Feature: Volunteer
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
WordPress/mcp-adapter#399 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 80/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
freescout-help-desk/freescout#5750 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信