From a3cd1f6b74f866575b0813b61ee673d45ef6f998 Mon Sep 17 00:00:00 2001 From: tristan Date: Mon, 6 Apr 2026 16:49:44 +0200 Subject: [PATCH] fix : use custom EnvironmentCreateProcessor to properly add environments API Platform's default sub-resource POST was replacing instead of adding. Custom processor with read:false + Link + manual persist fixes this. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/Entity/Environment.php | 3 ++ src/State/EnvironmentCreateProcessor.php | 44 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/State/EnvironmentCreateProcessor.php diff --git a/src/Entity/Environment.php b/src/Entity/Environment.php index 1fc8e55..ecb3d7e 100644 --- a/src/Entity/Environment.php +++ b/src/Entity/Environment.php @@ -10,6 +10,7 @@ use ApiPlatform\Metadata\Link; use ApiPlatform\Metadata\Patch; use ApiPlatform\Metadata\Post; use App\Repository\EnvironmentRepository; +use App\State\EnvironmentCreateProcessor; use App\State\MaintenanceToggleProcessor; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; @@ -23,7 +24,9 @@ use Symfony\Component\Serializer\Attribute\Groups; uriVariables: [ 'slug' => new Link(toProperty: 'application', fromClass: Application::class, identifiers: ['slug']), ], + read: false, security: "is_granted('ROLE_ADMIN')", + processor: EnvironmentCreateProcessor::class, ), new Patch( security: "is_granted('ROLE_ADMIN')", diff --git a/src/State/EnvironmentCreateProcessor.php b/src/State/EnvironmentCreateProcessor.php new file mode 100644 index 0000000..fe68f87 --- /dev/null +++ b/src/State/EnvironmentCreateProcessor.php @@ -0,0 +1,44 @@ +attributes->get('slug') + ?? $context['request']?->attributes->get('_route_params')['slug'] + ?? ''; + + $application = $this->applicationRepository->findOneBy(['slug' => $slug]); + + if (null === $application) { + throw new NotFoundHttpException(sprintf('Application "%s" not found.', $slug)); + } + + $data->setApplication($application); + + $this->entityManager->persist($data); + $this->entityManager->flush(); + + return $data; + } +}