fix(api) : denormalize interface-typed relation collections
Add ContractRelationDenormalizer to resolve IRIs for collections typed against an interface (Contract), fixing POST/PATCH 400 errors. Cover it with InterfaceCollectionDenormalizationTest.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Shared\Infrastructure\ApiPlatform\Serializer;
|
||||
|
||||
use ApiPlatform\Metadata\IriConverterInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
use Throwable;
|
||||
|
||||
use function array_key_exists;
|
||||
use function is_string;
|
||||
|
||||
/**
|
||||
* Modular monolith: cross-module relations are typed with a Shared\Domain\Contract
|
||||
* interface (e.g. UserInterface, TaskTagInterface) instead of the concrete entity,
|
||||
* to keep modules decoupled. Doctrine maps those back to the concrete entity through
|
||||
* resolve_target_entities.
|
||||
*
|
||||
* API Platform denormalizes *single* interface relations fine (the concrete class is
|
||||
* derived from the IRI), but blows up on *collections*: the collection value type stays
|
||||
* the interface, which is not a registered API resource, so no normalizer supports it
|
||||
* and the request fails with NotNormalizableValueException.
|
||||
*
|
||||
* This denormalizer bridges that gap for every contract interface, reusing Doctrine's
|
||||
* resolve_target_entities mapping (no per-entity config):
|
||||
* - a string value is an IRI -> resolved through the IriConverter
|
||||
* - an array value is an embedded object -> denormalized into the concrete entity
|
||||
*/
|
||||
final class ContractRelationDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface
|
||||
{
|
||||
use DenormalizerAwareTrait;
|
||||
|
||||
private const CONTRACT_NAMESPACE = 'App\Shared\Domain\Contract\\';
|
||||
|
||||
/** @var array<string, ?class-string> */
|
||||
private array $resolved = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly IriConverterInterface $iriConverter,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
) {}
|
||||
|
||||
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
|
||||
{
|
||||
return null !== $this->concreteClassFor($type);
|
||||
}
|
||||
|
||||
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): ?object
|
||||
{
|
||||
$concrete = $this->concreteClassFor($type);
|
||||
if (null === $concrete) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (is_string($data)) {
|
||||
return $this->iriConverter->getResourceFromIri($data, $context);
|
||||
}
|
||||
|
||||
// Embedded object payload: denormalize into the resolved concrete entity.
|
||||
return $this->denormalizer->denormalize($data, $concrete, $format, $context);
|
||||
}
|
||||
|
||||
public function getSupportedTypes(?string $format): array
|
||||
{
|
||||
// Support depends on the runtime-resolved Doctrine mapping, so it cannot be
|
||||
// statically cached by the serializer.
|
||||
return ['object' => false];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ?class-string the concrete entity a contract interface resolves to, or null
|
||||
*/
|
||||
private function concreteClassFor(string $type): ?string
|
||||
{
|
||||
if (array_key_exists($type, $this->resolved)) {
|
||||
return $this->resolved[$type];
|
||||
}
|
||||
|
||||
if (!str_starts_with($type, self::CONTRACT_NAMESPACE) || !interface_exists($type)) {
|
||||
return $this->resolved[$type] = null;
|
||||
}
|
||||
|
||||
try {
|
||||
$name = $this->entityManager->getClassMetadata($type)->getName();
|
||||
} catch (Throwable) {
|
||||
// Not a Doctrine-mapped (resolve_target_entities) interface.
|
||||
return $this->resolved[$type] = null;
|
||||
}
|
||||
|
||||
return $this->resolved[$type] = ($name !== $type ? $name : null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Functional\Module\Shared;
|
||||
|
||||
use App\Module\Core\Domain\Entity\User;
|
||||
use App\Module\ProjectManagement\Domain\Entity\Project;
|
||||
use App\Module\ProjectManagement\Domain\Entity\TaskTag;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
/**
|
||||
* Regression: cross-module to-many relations are typed with a Shared contract
|
||||
* interface (TaskTagInterface[], UserInterface[]). API Platform cannot
|
||||
* denormalize a collection whose value type is an interface (no resource
|
||||
* normalizer supports it), so every POST/PATCH carrying such a collection
|
||||
* blew up with NotNormalizableValueException.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class InterfaceCollectionDenormalizationTest extends WebTestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$conn = self::getContainer()->get(EntityManagerInterface::class)->getConnection();
|
||||
$conn->executeStatement("DELETE FROM time_entry_task_type WHERE time_entry_id IN (SELECT id FROM time_entry WHERE title = 'iface-denorm-te')");
|
||||
$conn->executeStatement("DELETE FROM time_entry WHERE title = 'iface-denorm-te'");
|
||||
$conn->executeStatement("DELETE FROM task_collaborator WHERE task_id IN (SELECT id FROM task WHERE title = 'iface-denorm-task')");
|
||||
$conn->executeStatement("DELETE FROM task WHERE title = 'iface-denorm-task'");
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testPostTimeEntryWithInterfaceTypedTags(): void
|
||||
{
|
||||
$client = self::createClient();
|
||||
$em = self::getContainer()->get(EntityManagerInterface::class);
|
||||
$this->loginAdmin($client);
|
||||
|
||||
$userId = $this->adminId($em);
|
||||
$tagId = $this->aTaskTagId($em);
|
||||
|
||||
$client->request('POST', '/api/time_entries', server: [
|
||||
'CONTENT_TYPE' => 'application/json',
|
||||
'HTTP_ACCEPT' => 'application/json',
|
||||
], content: json_encode([
|
||||
'title' => 'iface-denorm-te',
|
||||
'startedAt' => '2026-06-22T10:00:00+02:00',
|
||||
'stoppedAt' => '2026-06-22T11:00:00+02:00',
|
||||
'user' => '/api/users/'.$userId,
|
||||
'tags' => ['/api/task_tags/'.$tagId],
|
||||
]));
|
||||
|
||||
self::assertResponseStatusCodeSame(201, $client->getResponse()->getContent() ?: '');
|
||||
$data = json_decode($client->getResponse()->getContent(), true);
|
||||
self::assertCount(1, $data['tags'] ?? []);
|
||||
}
|
||||
|
||||
public function testPostTaskWithInterfaceTypedCollaborators(): void
|
||||
{
|
||||
$client = self::createClient();
|
||||
$em = self::getContainer()->get(EntityManagerInterface::class);
|
||||
$this->loginAdmin($client);
|
||||
|
||||
$userId = $this->adminId($em);
|
||||
$projectId = $this->aProjectId($em);
|
||||
|
||||
$client->request('POST', '/api/tasks', server: [
|
||||
'CONTENT_TYPE' => 'application/json',
|
||||
'HTTP_ACCEPT' => 'application/json',
|
||||
], content: json_encode([
|
||||
'title' => 'iface-denorm-task',
|
||||
'project' => '/api/projects/'.$projectId,
|
||||
'collaborators' => ['/api/users/'.$userId],
|
||||
]));
|
||||
|
||||
self::assertResponseStatusCodeSame(201, $client->getResponse()->getContent() ?: '');
|
||||
$data = json_decode($client->getResponse()->getContent(), true);
|
||||
self::assertCount(1, $data['collaborators'] ?? []);
|
||||
}
|
||||
|
||||
private function loginAdmin(KernelBrowser $client): void
|
||||
{
|
||||
$em = self::getContainer()->get(EntityManagerInterface::class);
|
||||
$user = $em->getRepository(User::class)->findOneBy(['username' => 'admin']);
|
||||
self::assertInstanceOf(User::class, $user);
|
||||
$client->loginUser($user);
|
||||
}
|
||||
|
||||
private function adminId(EntityManagerInterface $em): int
|
||||
{
|
||||
$user = $em->getRepository(User::class)->findOneBy(['username' => 'admin']);
|
||||
self::assertInstanceOf(User::class, $user);
|
||||
|
||||
return $user->getId();
|
||||
}
|
||||
|
||||
private function aTaskTagId(EntityManagerInterface $em): int
|
||||
{
|
||||
$tag = $em->getRepository(TaskTag::class)->findOneBy([]);
|
||||
if (null === $tag) {
|
||||
$tag = new TaskTag();
|
||||
$tag->setLabel('iface-denorm-tag');
|
||||
$em->persist($tag);
|
||||
$em->flush();
|
||||
}
|
||||
|
||||
return $tag->getId();
|
||||
}
|
||||
|
||||
private function aProjectId(EntityManagerInterface $em): int
|
||||
{
|
||||
$project = $em->getRepository(Project::class)->findOneBy([]);
|
||||
self::assertInstanceOf(Project::class, $project);
|
||||
|
||||
return $project->getId();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user