api-platform / api-platform/core

CR: Use ItemNormalizer/IriConverter without throwing Exceptions on wrong IRIs

Đang mở
#8,014 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
PHP
Star
2.6k
Fork
980
Merge trung bình
2 ngày 5 giờ
Pull request đã merge (30 ngày)
48

Mô tả

I use the ItemNormalizer to (de)normalize object structures from/to arrays where references are represented by their IRIs.
The serialized string is saved in the database and represents a published, fixed, unchangeable state of an API resource. Some references are not part of the publishing (means it isn't embedded) and reference an entity from the conventional database table. So the database cannot care about the referential integrity.
If I try to deserialize to such an object, an [UnexpectedValueException](https://github.com/api-platform/core/blob/4717bd5643d7761707229ed8705ae6824248f7e3/src/Serializer/AbstractItemNormalizer.php#L469) can appear if the reference doesn't exists anymore.

Currently the only one solution would be the following:
```
foreach ($normalizedObject as $key => $value) {
try {
$this->normalizer->denormalize([$key => $value], $className, null, [AbstractNormalizer::OBJECT_TO_POPULATE => $object]);
} catch (UnexpectedValueException $e) {}
}
```
There is some overhead because each field is denormalized for itself. This works great as long the field isn't an array field, where only one item isn't denormalizable anymore.
With some effort I can handle also this situation, but it will be ugly code.
The better solution would be to skip values which cannot be resolved. Currently there is no way to configure a behaviour like this. This would be my change request. :-)
Maybe there could be an context option for this.

Disclaimer: Usually I would use an other API design for this requirement. But it's not mine...

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.