Skip to content

Commit

Permalink
Make collection code check for PaginatedResultSet
Browse files Browse the repository at this point in the history
  • Loading branch information
cnizzardini committed Feb 18, 2024
1 parent af946bb commit 4023c6b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
5 changes: 3 additions & 2 deletions plugins/json-ld-view/src/JsonSerializer.php
Expand Up @@ -5,6 +5,7 @@

use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\Paging\PaginatedResultSet;
use Cake\Datasource\ResultSetInterface;
use Cake\Http\ServerRequest;
use Cake\ORM\Entity;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function __construct(mixed $serialize, ?ServerRequest $request = null, ?P
$this->hydra = 'hydra:';
}

if ($jsonLd instanceof ResultSetInterface) {
if ($jsonLd instanceof ResultSetInterface || $jsonLd instanceof PaginatedResultSet) {
$this->data = $this->collection($jsonLd);
} elseif (is_subclass_of($jsonLd, Entity::class)) {
$this->data = $this->item($jsonLd);
Expand Down Expand Up @@ -109,7 +110,7 @@ public function getData(): ?array
*/
private function recursion(mixed &$mixed): mixed
{
if ($mixed instanceof ResultSetInterface || is_array($mixed)) {
if ($mixed instanceof ResultSetInterface || $mixed instanceof PaginatedResultSet || is_array($mixed)) {
foreach ($mixed as $item) {
$this->recursion($item);
}
Expand Down
18 changes: 15 additions & 3 deletions plugins/json-ld-view/tests/TestCase/JsonSerializerTest.php
Expand Up @@ -62,9 +62,9 @@ public function setUp(): void
}

/**
* Test collection serialization and deserialization
* @dataProvider dataProviderForCollectionTypes
*/
public function test_collection(): void
public function test_collection(string $resultType): void
{
/** @var Table $actor */
$actor = FactoryLocator::get('Table')->get('Actors');
Expand All @@ -86,14 +86,26 @@ public function test_collection(): void
new View($request, null, null, ['viewVars' => ['actors' => $resultSet]])
);

$jsonSerializer = new JsonSerializer($result, $this->request, $paginator);
if ($resultType === 'PaginatedResultSet') {
$jsonSerializer = new JsonSerializer($resultSet, $this->request, $paginator);
} else {
$jsonSerializer = new JsonSerializer($result, $this->request, $paginator);
}

$json = $jsonSerializer->asJson(JSON_PRETTY_PRINT);

$this->assertIsString($json);
$this->assertIsObject(json_decode($json));
}

public static function dataProviderForCollectionTypes(): array
{
return [
['PaginatedResultSet'],
['ResultSet'],
];
}

/**
* Test item serialization and deserialization
*/
Expand Down

0 comments on commit 4023c6b

Please sign in to comment.