Skip to content

Commit

Permalink
Merge d182b74 into c715918
Browse files Browse the repository at this point in the history
  • Loading branch information
hhamon committed Dec 1, 2020
2 parents c715918 + d182b74 commit e4d9e5e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## 2.7.0

* Elasticsearch: **BC** Paginator now receives the denormalization context to support denormalizing documents using serialization groups.

## 2.6.0

* Display the API Platform's version in the debug-bar
Expand Down
Expand Up @@ -116,7 +116,8 @@ public function getCollection(string $resourceClass, ?string $operationName = nu
$documents,
$resourceClass,
$limit,
$offset
$offset,
$context
);
}
}
8 changes: 6 additions & 2 deletions src/Bridge/Elasticsearch/DataProvider/Paginator.php
Expand Up @@ -33,14 +33,16 @@ final class Paginator implements \IteratorAggregate, PaginatorInterface
private $limit;
private $offset;
private $cachedDenormalizedDocuments = [];
private $denormalizationContext = [];

public function __construct(DenormalizerInterface $denormalizer, array $documents, string $resourceClass, int $limit, int $offset)
public function __construct(DenormalizerInterface $denormalizer, array $documents, string $resourceClass, int $limit, int $offset, array $denormalizationContext = [])
{
$this->denormalizer = $denormalizer;
$this->documents = $documents;
$this->resourceClass = $resourceClass;
$this->limit = $limit;
$this->offset = $offset;
$this->denormalizationContext = $denormalizationContext;
}

/**
Expand Down Expand Up @@ -102,6 +104,8 @@ public function getItemsPerPage(): float
*/
public function getIterator(): \Traversable
{
$denormalizationContext = array_merge([AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true], $this->denormalizationContext);

foreach ($this->documents['hits']['hits'] ?? [] as $document) {
$cacheKey = isset($document['_index'], $document['_type'], $document['_id']) ? md5("${document['_index']}_${document['_type']}_${document['_id']}") : null;

Expand All @@ -112,7 +116,7 @@ public function getIterator(): \Traversable
$document,
$this->resourceClass,
ItemNormalizer::FORMAT,
[AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true]
$denormalizationContext
);

if ($cacheKey) {
Expand Down
Expand Up @@ -92,6 +92,12 @@ public function testSupports()

public function testGetCollection()
{
$context = [
'groups' => ['custom'],
];

$operationName = 'get';

$documentMetadataFactoryProphecy = $this->prophesize(DocumentMetadataFactoryInterface::class);
$documentMetadataFactoryProphecy->create(Foo::class)->willReturn(new DocumentMetadata('foo'))->shouldBeCalled();

Expand Down Expand Up @@ -159,7 +165,7 @@ public function testGetCollection()
->shouldBeCalled();

$requestBodySearchCollectionExtensionProphecy = $this->prophesize(RequestBodySearchCollectionExtensionInterface::class);
$requestBodySearchCollectionExtensionProphecy->applyToCollection([], Foo::class, null, [])->wilLReturn([])->shouldBeCalled();
$requestBodySearchCollectionExtensionProphecy->applyToCollection([], Foo::class, $operationName, $context)->willReturn([])->shouldBeCalled();

$collectionDataProvider = new CollectionDataProvider(
$clientProphecy->reveal(),
Expand All @@ -172,8 +178,8 @@ public function testGetCollection()
);

self::assertEquals(
new Paginator($denormalizer, $documents, Foo::class, 2, 0),
$collectionDataProvider->getCollection(Foo::class)
new Paginator($denormalizer, $documents, Foo::class, 2, 0, $context),
$collectionDataProvider->getCollection(Foo::class, $operationName, $context)
);
}
}
4 changes: 2 additions & 2 deletions tests/Bridge/Elasticsearch/DataProvider/PaginatorTest.php
Expand Up @@ -178,11 +178,11 @@ private function getPaginator(int $limit = self::OFFSET, int $offset = self::OFF

foreach ($documents['hits']['hits'] as $document) {
$denormalizerProphecy
->denormalize($document, Foo::class, ItemNormalizer::FORMAT, [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true])
->denormalize($document, Foo::class, ItemNormalizer::FORMAT, [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true, 'groups' => ['custom']])
->willReturn($this->denormalizeFoo($document['_source']));
}

return new Paginator($denormalizerProphecy->reveal(), $documents, Foo::class, $limit, $offset);
return new Paginator($denormalizerProphecy->reveal(), $documents, Foo::class, $limit, $offset, ['groups' => ['custom']]);
}

private function denormalizeFoo(array $fooDocument): Foo
Expand Down

0 comments on commit e4d9e5e

Please sign in to comment.