From e4fa5a234b05652630c07ab375c5d4b9e46e17f8 Mon Sep 17 00:00:00 2001 From: Manuel Rossard <95523073+mrossard@users.noreply.github.com> Date: Fri, 14 Apr 2023 13:08:56 +0200 Subject: [PATCH] fix(serializer): no forced resource class relation (#5542) Co-authored-by: Manuel Rossard --- features/jsonld/non_resource.feature | 10 ++++ src/Serializer/AbstractItemNormalizer.php | 1 + tests/Behat/DoctrineContext.php | 12 +++++ .../ApiResource/EntityClassWithDateTime.php | 37 +++++++++++++++ .../Entity/EntityClassWithDateTime.php | 46 +++++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 tests/Fixtures/TestBundle/ApiResource/EntityClassWithDateTime.php create mode 100644 tests/Fixtures/TestBundle/Entity/EntityClassWithDateTime.php diff --git a/features/jsonld/non_resource.feature b/features/jsonld/non_resource.feature index 0336b2cdfc9..c00016cbe36 100644 --- a/features/jsonld/non_resource.feature +++ b/features/jsonld/non_resource.feature @@ -132,3 +132,13 @@ Feature: JSON-LD non-resource handling And the response should be in JSON And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" And the JSON node "totalPrice.@id" should not exist + + @!mongodb + @createSchema + Scenario: Get a resource using entityClass with a DateTime attribute + Given there is a resource using entityClass with a DateTime attribute + When I send a "GET" request to "/EntityClassWithDateTime/1" + Then the response status code should be 200 + And the response should be in JSON + And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" + And the JSON node "start" should exist diff --git a/src/Serializer/AbstractItemNormalizer.php b/src/Serializer/AbstractItemNormalizer.php index 0f16eaaf734..6f87a81a522 100644 --- a/src/Serializer/AbstractItemNormalizer.php +++ b/src/Serializer/AbstractItemNormalizer.php @@ -640,6 +640,7 @@ protected function getAttributeValue(object $object, string $attribute, string $ } unset($context['resource_class']); + unset($context['force_resource_class']); if ($type && $type->getClassName()) { $childContext = $this->createChildContext($context, $attribute, $format); diff --git a/tests/Behat/DoctrineContext.php b/tests/Behat/DoctrineContext.php index e1356dfc0d9..f1b3d14b0a6 100644 --- a/tests/Behat/DoctrineContext.php +++ b/tests/Behat/DoctrineContext.php @@ -132,6 +132,7 @@ use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyTravel; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\EmbeddableDummy; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\EmbeddedDummy; +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\EntityClassWithDateTime; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\ExternalUser; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\FileConfigDummy; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Foo; @@ -2127,6 +2128,17 @@ public function thereIsADummyObjectWithManyMultipleRelation(): void $this->manager->flush(); } + /** + * @Given there is a resource using entityClass with a DateTime attribute + */ + public function thereIsAResourceUsingEntityClassAndDateTime(): void + { + $entity = new EntityClassWithDateTime(); + $entity->setStart(new \DateTime()); + $this->manager->persist($entity); + $this->manager->flush(); + } + private function isOrm(): bool { return null !== $this->schemaTool; diff --git a/tests/Fixtures/TestBundle/ApiResource/EntityClassWithDateTime.php b/tests/Fixtures/TestBundle/ApiResource/EntityClassWithDateTime.php new file mode 100644 index 00000000000..63bbcddf1ee --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/EntityClassWithDateTime.php @@ -0,0 +1,37 @@ + + * + * 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\ApiResource; + +use ApiPlatform\Doctrine\Orm\State\Options; +use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Get; +use ApiPlatform\Metadata\GetCollection; + +#[ApiResource( + operations : [ + new Get( + uriTemplate: '/EntityClassWithDateTime/{id}', + ), + new GetCollection( + uriTemplate: '/EntityClassWithDateTime', + uriVariables: ['id'] + ), + ], + stateOptions: new Options(entityClass: \ApiPlatform\Tests\Fixtures\TestBundle\Entity\EntityClassWithDateTime::class) +)] +class EntityClassWithDateTime +{ + public ?int $id; + public ?\DateTimeInterface $start; +} diff --git a/tests/Fixtures/TestBundle/Entity/EntityClassWithDateTime.php b/tests/Fixtures/TestBundle/Entity/EntityClassWithDateTime.php new file mode 100644 index 00000000000..d9d7aabaf98 --- /dev/null +++ b/tests/Fixtures/TestBundle/Entity/EntityClassWithDateTime.php @@ -0,0 +1,46 @@ + + * + * 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; + +use Doctrine\DBAL\Types\Types; +use Doctrine\ORM\Mapping as ORM; + +#[ORM\Entity] +class EntityClassWithDateTime +{ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column] + private ?int $id = null; + + #[ORM\Column(type: Types::DATETIME_MUTABLE)] + private ?\DateTimeInterface $start = null; + + public function getId(): ?int + { + return $this->id; + } + + public function getStart(): ?\DateTimeInterface + { + return $this->start; + } + + public function setStart(\DateTimeInterface $start): self + { + $this->start = $start; + + return $this; + } +}