Skip to content

Commit

Permalink
fix(serializer): no forced resource class relation (#5542)
Browse files Browse the repository at this point in the history
Co-authored-by: Manuel Rossard <manuel.rossard@u-bordeaux.fr>
  • Loading branch information
mrossard and mrossard committed Apr 14, 2023
1 parent 1f28efc commit e4fa5a2
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
10 changes: 10 additions & 0 deletions features/jsonld/non_resource.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions tests/Behat/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/EntityClassWithDateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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\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;
}
46 changes: 46 additions & 0 deletions tests/Fixtures/TestBundle/Entity/EntityClassWithDateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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;

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;
}
}

0 comments on commit e4fa5a2

Please sign in to comment.