Skip to content

Commit 2b62452

Browse files
fix: use normalisation context when none is provided in ApiTestAssertionsTrait
1 parent 8a35ee2 commit 2b62452

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed

src/Symfony/Bundle/Test/ApiTestAssertionsTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ public static function assertMatchesResourceCollectionJsonSchema(string $resourc
119119
$operation = $operationName ? (new GetCollection())->withName($operationName) : new GetCollection();
120120
}
121121

122+
$serializationContext = $serializationContext ?? $operation->getNormalizationContext();
123+
122124
$schema = $schemaFactory->buildSchema($resourceClass, $format, Schema::TYPE_OUTPUT, $operation, null, ($serializationContext ?? []) + [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]);
123125

124126
static::assertMatchesJsonSchema($schema->getArrayCopy());
@@ -134,6 +136,8 @@ public static function assertMatchesResourceItemJsonSchema(string $resourceClass
134136
$operation = $operationName ? (new Get())->withName($operationName) : new Get();
135137
}
136138

139+
$serializationContext = $serializationContext ?? $operation->getNormalizationContext();
140+
137141
$schema = $schemaFactory->buildSchema($resourceClass, $format, Schema::TYPE_OUTPUT, $operation, null, ($serializationContext ?? []) + [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]);
138142

139143
static::assertMatchesJsonSchema($schema->getArrayCopy());
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
declare(strict_types=1);
12+
13+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6146;
14+
15+
use ApiPlatform\Metadata\ApiResource;
16+
use ApiPlatform\Metadata\Get;
17+
use ApiPlatform\Metadata\GetCollection;
18+
use Doctrine\ORM\Mapping as ORM;
19+
use Symfony\Component\Serializer\Attribute\Groups;
20+
21+
#[ApiResource(
22+
operations: [
23+
new Get(uriTemplate: 'issue-6146-childs/{id}'),
24+
new GetCollection(uriTemplate: 'issue-6146-childs'),
25+
],
26+
normalizationContext: ['groups' => ['testgroup']],
27+
)]
28+
#[ORM\Entity]
29+
class Issue6146Child
30+
{
31+
#[ORM\Column(type: 'integer')]
32+
#[ORM\Id]
33+
#[ORM\GeneratedValue(strategy: 'AUTO')]
34+
private ?int $id = null;
35+
36+
#[ORM\ManyToOne(targetEntity: Issue6146Parent::class, inversedBy: 'childs')]
37+
private Issue6146Parent $parent;
38+
39+
#[ORM\Column(type: 'string')]
40+
#[Groups(['testgroup'])]
41+
private string $foo = 'testtest';
42+
43+
public function getFoo(): string
44+
{
45+
return $this->foo;
46+
}
47+
48+
public function setParent(Issue6146Parent $parent): Issue6146Child
49+
{
50+
$this->parent = $parent;
51+
return $this;
52+
}
53+
54+
public function getParent(): Issue6146Parent
55+
{
56+
return $this->parent;
57+
}
58+
59+
public function getId(): ?int
60+
{
61+
return $this->id;
62+
}
63+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
declare(strict_types=1);
12+
13+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6146;
14+
15+
use ApiPlatform\Metadata\ApiResource;
16+
use ApiPlatform\Metadata\Get;
17+
use ApiPlatform\Metadata\GetCollection;
18+
use Doctrine\Common\Collections\ArrayCollection;
19+
use Doctrine\Common\Collections\Collection;
20+
use Symfony\Component\Serializer\Attribute\Groups;
21+
use Doctrine\ORM\Mapping as ORM;
22+
23+
#[ApiResource(
24+
operations: [
25+
new Get(uriTemplate: 'issue-6146-parents/{id}'),
26+
new GetCollection(uriTemplate: 'issue-6146-parents'),
27+
],
28+
normalizationContext: ['groups' => ['testgroup']],
29+
)]
30+
#[ORM\Entity]
31+
class Issue6146Parent
32+
{
33+
#[ORM\Column(type: 'integer')]
34+
#[ORM\Id]
35+
#[ORM\GeneratedValue(strategy: 'AUTO')]
36+
private ?int $id = null;
37+
38+
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: Issue6146Child::class)]
39+
#[Groups(['testgroup'])]
40+
private Collection $childs;
41+
42+
public function __construct()
43+
{
44+
$this->childs = new ArrayCollection();
45+
}
46+
47+
public function getId(): ?int
48+
{
49+
return $this->id;
50+
}
51+
52+
public function addChild(Issue6146Child $child): void
53+
{
54+
$this->childs->add($child);
55+
}
56+
}

tests/Fixtures/TestBundle/Model/ResourceInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
namespace ApiPlatform\Tests\Fixtures\TestBundle\Model;
1515

16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\GetCollection;
18+
1619
interface ResourceInterface
1720
{
1821
public function getFoo(): string;

tests/Symfony/Bundle/Test/ApiTestCaseTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
1919
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyDtoInputOutput;
2020
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6041\NumericValidated;
21+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6146\Issue6146Child;
22+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6146\Issue6146Parent;
2123
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\JsonSchemaContextDummy;
2224
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\User;
2325
use ApiPlatform\Tests\Fixtures\TestBundle\Model\ResourceInterface;
@@ -126,6 +128,32 @@ public function testAssertMatchesResourceCollectionJsonSchema(): void
126128
$this->assertMatchesResourceCollectionJsonSchema(ResourceInterface::class);
127129
}
128130

131+
public function testAssertMatchesResourceCollectionJsonSchemaKeepSerializationContext()
132+
{
133+
$this->recreateSchema();
134+
135+
/** @var EntityManagerInterface $manager */
136+
$manager = static::getContainer()->get('doctrine')->getManager();
137+
138+
$parent = new Issue6146Parent();
139+
$manager->persist($parent);
140+
141+
$child = new Issue6146Child();
142+
$child->setParent($parent);
143+
$parent->addChild($child);
144+
$manager->persist($child);
145+
146+
147+
$manager->persist($child);
148+
$manager->flush();
149+
150+
self::createClient()->request('GET', "issue-6146-parents/{$parent->getId()}");
151+
$this->assertMatchesResourceItemJsonSchema(Issue6146Parent::class);
152+
153+
self::createClient()->request('GET', '/issue-6146-parents');
154+
$this->assertMatchesResourceCollectionJsonSchema(Issue6146Parent::class);
155+
}
156+
129157
public function testAssertMatchesResourceItemJsonSchema(): void
130158
{
131159
self::createClient()->request('GET', '/resource_interfaces/some-id');

0 commit comments

Comments
 (0)