Skip to content

Commit 72405e8

Browse files
vincentchalamondunglas
authored andcommitted
Add documentation about SerializerAwareDataProviderInterface (#336)
1 parent 2c29225 commit 72405e8

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

core/data-providers.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,45 @@ services:
125125
tags: [ 'api_platform.item_data_provider' ]
126126
```
127127

128+
## Injecting the Serializer in an `ItemDataProvider`
129+
130+
In some cases, you may need to inject the `Serializer` in your `DataProvider`. There are no issues with the
131+
`CollectionDataProvider`, but when injecting it in the `ItemDataProvider` it will throw a `CircularReferenceException`.
132+
133+
For this reason, we implemented the `SerializerAwareDataProviderInterface`:
134+
135+
```php
136+
<?php
137+
// src/AppBundle/DataProvider/BlogPostItemDataProvider.php
138+
139+
namespace AppBundle\DataProvider;
140+
141+
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
142+
use ApiPlatform\Core\DataProvider\SerializerAwareDataProviderInterface;
143+
use ApiPlatform\Core\DataProvider\SerializerAwareDataProviderTrait;
144+
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
145+
use AppBundle\Entity\BlogPost;
146+
147+
final class BlogPostItemDataProvider implements ItemDataProviderInterface, SerializerAwareDataProviderInterface
148+
{
149+
use SerializerAwareDataProviderTrait;
150+
151+
public function supports(string $resourceClass, string $operationName = null): bool
152+
{
153+
return BlogPost::class === $resourceClass;
154+
}
155+
156+
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?BlogPost
157+
{
158+
// Retrieve data from anywhere you want, in a custom format
159+
$data = '...';
160+
161+
// Deserialize data using the Serializer
162+
return $this->getSerializer()->deserialize($data, BlogPost::class, 'custom');
163+
}
164+
}
165+
```
166+
128167
Previous chapter: [Extending JSON-LD context](extending-jsonld-context.md)
129168

130169
Next chapter: [Extensions](extensions.md)

index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@
6464
12. [Using External JSON-LD Vocabularies](core/external-vocabularies.md)
6565
13. [Extending JSON-LD context](core/extending-jsonld-context.md)
6666
14. [Data Providers](core/data-providers.md)
67-
1. [Custom Collection Data Provider](core/data-providers.md#creating-a-custom-data-provider#custom-collection-data-provider)
68-
2. [Custom Item Data Provider](core/data-providers.md#returning-a-paged-collection#custom-item-data-provider)
67+
1. [Custom Collection Data Provider](core/data-providers.md#custom-collection-data-provider)
68+
2. [Custom Item Data Provider](core/data-providers.md#custom-item-data-provider)
69+
3. [Injecting the Serializer in an `ItemDataProvider`](core/data-providers.md#injecting-the-serializer-in-an-itemdataprovider)
6970
15. [Extensions](core/extensions.md)
7071
1. [Custom Extension](core/extensions.md#custom-extension)
7172
2. [Filter upon the current user](core/extensions.md#example)

0 commit comments

Comments
 (0)