Skip to content

Commit 2629539

Browse files
fix: use normalisation context when none is provided in ApiTestAssertionsTrait (#6157)
1 parent c08f1e1 commit 2629539

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

src/Symfony/Bundle/Test/ApiTestAssertionsTrait.php

+4
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());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6146;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\GetCollection;
19+
use Doctrine\ORM\Mapping as ORM;
20+
use Symfony\Component\Serializer\Attribute\Groups;
21+
22+
#[ApiResource(
23+
operations: [
24+
new Get(uriTemplate: 'issue-6146-childs/{id}'),
25+
new GetCollection(uriTemplate: 'issue-6146-childs'),
26+
],
27+
normalizationContext: ['groups' => ['testgroup']],
28+
)]
29+
#[ORM\Entity]
30+
class Issue6146Child
31+
{
32+
#[ORM\Column(type: 'integer')]
33+
#[ORM\Id]
34+
#[ORM\GeneratedValue(strategy: 'AUTO')]
35+
private ?int $id = null;
36+
37+
#[ORM\ManyToOne(targetEntity: Issue6146Parent::class, inversedBy: 'childs')]
38+
#[ORM\JoinColumn(nullable: false)]
39+
private Issue6146Parent $parent;
40+
41+
#[ORM\Column(type: 'string')]
42+
#[Groups(['testgroup'])]
43+
private string $foo = 'testtest';
44+
45+
public function getFoo(): string
46+
{
47+
return $this->foo;
48+
}
49+
50+
public function setParent(Issue6146Parent $parent): self
51+
{
52+
$this->parent = $parent;
53+
54+
return $this;
55+
}
56+
57+
public function getParent(): Issue6146Parent
58+
{
59+
return $this->parent;
60+
}
61+
62+
public function getId(): ?int
63+
{
64+
return $this->id;
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6146;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\GetCollection;
19+
use Doctrine\Common\Collections\ArrayCollection;
20+
use Doctrine\Common\Collections\Collection;
21+
use Doctrine\ORM\Mapping as ORM;
22+
use Symfony\Component\Serializer\Attribute\Groups;
23+
24+
#[ApiResource(
25+
operations: [
26+
new Get(uriTemplate: 'issue-6146-parents/{id}'),
27+
new GetCollection(uriTemplate: 'issue-6146-parents'),
28+
],
29+
normalizationContext: ['groups' => ['testgroup']],
30+
)]
31+
#[ORM\Entity]
32+
class Issue6146Parent
33+
{
34+
#[ORM\Column(type: 'integer')]
35+
#[ORM\Id]
36+
#[ORM\GeneratedValue(strategy: 'AUTO')]
37+
private ?int $id = null;
38+
39+
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: Issue6146Child::class)]
40+
#[Groups(['testgroup'])]
41+
private Collection $childs;
42+
43+
public function __construct()
44+
{
45+
$this->childs = new ArrayCollection();
46+
}
47+
48+
public function getId(): ?int
49+
{
50+
return $this->id;
51+
}
52+
53+
public function addChild(Issue6146Child $child): void
54+
{
55+
$this->childs->add($child);
56+
}
57+
}

tests/Symfony/Bundle/Test/ApiTestCaseTest.php

+27
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,31 @@ public function testAssertMatchesResourceCollectionJsonSchema(): void
126128
$this->assertMatchesResourceCollectionJsonSchema(ResourceInterface::class);
127129
}
128130

131+
public function testAssertMatchesResourceCollectionJsonSchemaKeepSerializationContext(): void
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+
$manager->persist($child);
147+
$manager->flush();
148+
149+
self::createClient()->request('GET', "issue-6146-parents/{$parent->getId()}");
150+
$this->assertMatchesResourceItemJsonSchema(Issue6146Parent::class);
151+
152+
self::createClient()->request('GET', '/issue-6146-parents');
153+
$this->assertMatchesResourceCollectionJsonSchema(Issue6146Parent::class);
154+
}
155+
129156
public function testAssertMatchesResourceItemJsonSchema(): void
130157
{
131158
self::createClient()->request('GET', '/resource_interfaces/some-id');

0 commit comments

Comments
 (0)