Skip to content

fix: Use normalisation context when none is provided in tests #6157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2024
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
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/Test/ApiTestAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public static function assertMatchesResourceCollectionJsonSchema(string $resourc
$operation = $operationName ? (new GetCollection())->withName($operationName) : new GetCollection();
}

$serializationContext = $serializationContext ?? $operation->getNormalizationContext();

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

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

$serializationContext = $serializationContext ?? $operation->getNormalizationContext();

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

static::assertMatchesJsonSchema($schema->getArrayCopy());
Expand Down
66 changes: 66 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Issue6146/Issue6146Child.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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\Issue6146;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;

#[ApiResource(
operations: [
new Get(uriTemplate: 'issue-6146-childs/{id}'),
new GetCollection(uriTemplate: 'issue-6146-childs'),
],
normalizationContext: ['groups' => ['testgroup']],
)]
#[ORM\Entity]
class Issue6146Child
{
#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private ?int $id = null;

#[ORM\ManyToOne(targetEntity: Issue6146Parent::class, inversedBy: 'childs')]
#[ORM\JoinColumn(nullable: false)]
private Issue6146Parent $parent;

#[ORM\Column(type: 'string')]
#[Groups(['testgroup'])]
private string $foo = 'testtest';

public function getFoo(): string
{
return $this->foo;
}

public function setParent(Issue6146Parent $parent): self
{
$this->parent = $parent;

return $this;
}

public function getParent(): Issue6146Parent
{
return $this->parent;
}

public function getId(): ?int
{
return $this->id;
}
}
57 changes: 57 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Issue6146/Issue6146Parent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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\Issue6146;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;

#[ApiResource(
operations: [
new Get(uriTemplate: 'issue-6146-parents/{id}'),
new GetCollection(uriTemplate: 'issue-6146-parents'),
],
normalizationContext: ['groups' => ['testgroup']],
)]
#[ORM\Entity]
class Issue6146Parent
{
#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private ?int $id = null;

#[ORM\OneToMany(mappedBy: 'parent', targetEntity: Issue6146Child::class)]
#[Groups(['testgroup'])]
private Collection $childs;

public function __construct()
{
$this->childs = new ArrayCollection();
}

public function getId(): ?int
{
return $this->id;
}

public function addChild(Issue6146Child $child): void
{
$this->childs->add($child);
}
}
27 changes: 27 additions & 0 deletions tests/Symfony/Bundle/Test/ApiTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyDtoInputOutput;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6041\NumericValidated;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6146\Issue6146Child;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6146\Issue6146Parent;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\JsonSchemaContextDummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\User;
use ApiPlatform\Tests\Fixtures\TestBundle\Model\ResourceInterface;
Expand Down Expand Up @@ -126,6 +128,31 @@ public function testAssertMatchesResourceCollectionJsonSchema(): void
$this->assertMatchesResourceCollectionJsonSchema(ResourceInterface::class);
}

public function testAssertMatchesResourceCollectionJsonSchemaKeepSerializationContext(): void
{
$this->recreateSchema();

/** @var EntityManagerInterface $manager */
$manager = static::getContainer()->get('doctrine')->getManager();

$parent = new Issue6146Parent();
$manager->persist($parent);

$child = new Issue6146Child();
$child->setParent($parent);
$parent->addChild($child);
$manager->persist($child);

$manager->persist($child);
$manager->flush();

self::createClient()->request('GET', "issue-6146-parents/{$parent->getId()}");
$this->assertMatchesResourceItemJsonSchema(Issue6146Parent::class);

self::createClient()->request('GET', '/issue-6146-parents');
$this->assertMatchesResourceCollectionJsonSchema(Issue6146Parent::class);
}

public function testAssertMatchesResourceItemJsonSchema(): void
{
self::createClient()->request('GET', '/resource_interfaces/some-id');
Expand Down