Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Hydra/Serializer/PartialCollectionViewNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,13 @@ private function cursorPaginationFields(array $fields, int $direction, $object):

$operator = $direction > 0 ? $forwardRangeOperator : $backwardRangeOperator;

$value = $this->propertyAccessor->getValue($object, $field['field']);
if ($value instanceof \DateTimeInterface) {
$value = $value->format(\DateTimeInterface::ATOM);
}

$paginationFilters[$field['field']] = [
$operator => (string) $this->propertyAccessor->getValue($object, $field['field']),
$operator => (string) $value,
];
}

Expand Down
44 changes: 44 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Issue8085/DatedCursorDummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue8085;

use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use Doctrine\ORM\Mapping as ORM;

#[ApiResource(
operations: [
new GetCollection(
paginationItemsPerPage: 3,
paginationPartial: true,
paginationViaCursor: [['field' => 'createdAt', 'direction' => 'DESC']],
),
],
graphQlOperations: [],
)]
#[ApiFilter(DateFilter::class, properties: ['createdAt'])]
#[ORM\Entity]
#[ORM\Table(name: 'issue_8085_dated_cursor_dummy')]
class DatedCursorDummy
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue]
public ?int $id = null;

#[ORM\Column(type: 'datetime_immutable')]
public ?\DateTimeImmutable $createdAt = null;
}
71 changes: 71 additions & 0 deletions tests/Functional/JsonLd/CursorPaginationDateTimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Functional\JsonLd;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue8085\DatedCursorDummy;
use ApiPlatform\Tests\RecreateSchemaTrait;
use ApiPlatform\Tests\SetupClassResourcesTrait;

/**
* Regression test for https://github.com/api-platform/core/issues/8085.
*
* Cursor pagination must support DateTimeInterface fields without throwing
* "Object of class DateTime could not be converted to string".
*/
final class CursorPaginationDateTimeTest extends ApiTestCase
{
use RecreateSchemaTrait;
use SetupClassResourcesTrait;

protected static ?bool $alwaysBootKernel = false;

public static function getResources(): array
{
return [DatedCursorDummy::class];
}

public function testCursorPaginationWithDateTimeField(): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped();
}

$this->recreateSchema([DatedCursorDummy::class]);
$manager = $this->getManager();
for ($i = 1; $i <= 5; ++$i) {
$d = new DatedCursorDummy();
$d->createdAt = new \DateTimeImmutable("2024-01-0$i 12:00:00", new \DateTimeZone('UTC'));
$manager->persist($d);
}
$manager->flush();

$response = self::createClient()->request('GET', '/dated_cursor_dummies', [
'headers' => ['Accept' => 'application/ld+json'],
]);

$this->assertResponseIsSuccessful();
$body = $response->toArray();

$this->assertArrayHasKey('hydra:view', $body);
$this->assertArrayHasKey('hydra:next', $body['hydra:view']);

// 5 rows (2024-01-01..05) sorted DESC + 3 per page → visible page is 05,04,03;
// hydra:next must use lt with the last visible item (2024-01-03) as ISO 8601.
$this->assertMatchesRegularExpression(
'#createdAt%5Blt%5D=2024-01-03T12%3A00%3A00(?:%2B00%3A00|Z)#',
$body['hydra:view']['hydra:next'],
);
}
}
Loading