From f43e216e13cc56c6501991115117373d53cb2e88 Mon Sep 17 00:00:00 2001 From: nobohan Date: Fri, 11 Feb 2022 10:53:05 +0100 Subject: [PATCH] fix normalisation of AccompanyingCourse requestor api --- .../Normalizer/DiscriminatedObjectDenormalizer.php | 3 ++- .../Controller/AccompanyingCourseApiController.php | 14 +++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Serializer/Normalizer/DiscriminatedObjectDenormalizer.php b/src/Bundle/ChillMainBundle/Serializer/Normalizer/DiscriminatedObjectDenormalizer.php index 77f14c8da..76ba52a3d 100644 --- a/src/Bundle/ChillMainBundle/Serializer/Normalizer/DiscriminatedObjectDenormalizer.php +++ b/src/Bundle/ChillMainBundle/Serializer/Normalizer/DiscriminatedObjectDenormalizer.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\MainBundle\Serializer\Normalizer; use LogicException; +use Symfony\Component\Serializer\Exception\NotNormalizableValueException; use Symfony\Component\Serializer\Exception\RuntimeException; use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; @@ -44,7 +45,7 @@ class DiscriminatedObjectDenormalizer implements ContextAwareDenormalizerInterfa if ($this->denormalizer->supportsDenormalization($data, $localType, $format)) { try { return $this->denormalizer->denormalize($data, $localType, $format, $context); - } catch (RuntimeException $e) { + } catch (RuntimeException|NotNormalizableValueException $e) { $lastException = $e; } } diff --git a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseApiController.php b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseApiController.php index 10f733307..3da40db4c 100644 --- a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseApiController.php @@ -16,6 +16,7 @@ use Chill\MainBundle\Entity\Scope; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Serializer\Model\Collection; use Chill\MainBundle\Serializer\Model\Counter; +use Chill\MainBundle\Serializer\Normalizer\DiscriminatedObjectDenormalizer; use Chill\PersonBundle\AccompanyingPeriod\Suggestion\ReferralsSuggestionInterface; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork; @@ -235,13 +236,12 @@ final class AccompanyingCourseApiController extends ApiController $exceptions = []; try { - $contentAsArray = json_decode($request->getContent(), true); - $entityType = $contentAsArray['type']; - if ($entityType === 'person') { - $requestor = $this->getSerializer()->deserialize($request->getContent(), Person::class, $_format, []); - } elseif ($entityType === 'thirdparty') { - $requestor = $this->getSerializer()->deserialize($request->getContent(), ThirdParty::class, $_format, []); - } + $requestor = $this->getSerializer()->deserialize( + $request->getContent(), + '@multi', + $_format, + [DiscriminatedObjectDenormalizer::ALLOWED_TYPES => [Person::class, ThirdParty::class]] + ); } catch (RuntimeException $e) { $exceptions[] = $e; }