[breaking] Reinstate normalizer to previous state. Earlier fix still encountering problems so undone

This commit is contained in:
2025-10-01 18:04:07 +02:00
parent c2294e08a5
commit 067a36d90e
4 changed files with 56 additions and 41 deletions

View File

@@ -19,38 +19,36 @@ use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Workflow\Registry; use Symfony\Component\Workflow\Registry;
class AccompanyingPeriodWorkEvaluationNormalizer implements NormalizerInterface, NormalizerAwareInterface class AccompanyingPeriodWorkEvaluationNormalizer implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface, NormalizerAwareInterface
{ {
use NormalizerAwareTrait; use NormalizerAwareTrait;
public function __construct( private const IGNORE_EVALUATION = 'evaluation:ignore';
private readonly Registry $registry,
private readonly EntityWorkflowRepository $entityWorkflowRepository, public function __construct(private readonly Registry $registry, private readonly EntityWorkflowRepository $entityWorkflowRepository, private readonly MetadataExtractor $metadataExtractor) {}
private readonly MetadataExtractor $metadataExtractor,
) {}
/** /**
* @param AccompanyingPeriodWorkEvaluation $object * @param AccompanyingPeriodWorkEvaluation $object
*/ */
public function normalize($object, ?string $format = null, array $context = []): array public function normalize($object, ?string $format = null, array $context = []): array
{ {
$initial = $this->normalizer->normalize($object, $format, $context); $initial = $this->normalizer->normalize($object, $format, array_merge(
$context,
));
// due to bug: https://api-platform.com/docs/core/serialization/#collection-relation
// and also: https://github.com/symfony/symfony/issues/36965
// we have to rewrite the documents as a collection
$initial['documents'] = $this->normalizer->normalize( $initial['documents'] = $this->normalizer->normalize(
$object->getDocuments()->getValues(), $object->getDocuments()->getValues(),
$format, $format,
$context $context
); );
$initial['accompanyingPeriodWork'] = $this->normalizer->normalize( // then, we add normalization for things which are not into the entity
$object->getAccompanyingPeriodWork(),
$format,
$context
);
// Add additional workflows
$initial['workflows_availables'] = $this->metadataExtractor->availableWorkflowFor( $initial['workflows_availables'] = $this->metadataExtractor->availableWorkflowFor(
AccompanyingPeriodWorkEvaluation::class AccompanyingPeriodWorkEvaluation::class,
); );
$workflows = $this->entityWorkflowRepository->findBy([ $workflows = $this->entityWorkflowRepository->findBy([
@@ -64,11 +62,13 @@ class AccompanyingPeriodWorkEvaluationNormalizer implements NormalizerInterface,
public function supportsNormalization($data, ?string $format = null, array $context = []): bool public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{ {
return 'json' === $format return 'json' === $format
&& ($data instanceof AccompanyingPeriodWorkEvaluation || ($data instanceof \Doctrine\Persistence\Proxy && $data->__isInitialized())); && $data instanceof AccompanyingPeriodWorkEvaluation
&& !\array_key_exists(self::IGNORE_EVALUATION, $context);
} }
public function getSupportedTypes(?string $format): array public function getSupportedTypes(?string $format): array
{ {
return 'json' === $format ? [AccompanyingPeriodWorkEvaluation::class => null] : []; return 'json' === $format ? [AccompanyingPeriodWorkEvaluation::class => true] : [];
} }
} }

View File

@@ -31,11 +31,7 @@ class AccompanyingPeriodWorkNormalizer implements \Symfony\Component\Serializer\
private const IGNORE_WORK = 'ignore:work'; private const IGNORE_WORK = 'ignore:work';
public function __construct( public function __construct(private readonly Registry $registry, private readonly EntityWorkflowRepository $entityWorkflowRepository, private readonly MetadataExtractor $metadataExtractor) {}
private readonly Registry $registry,
private readonly EntityWorkflowRepository $entityWorkflowRepository,
private readonly MetadataExtractor $metadataExtractor,
) {}
public function normalize($object, ?string $format = null, array $context = []): array|\ArrayObject|bool|float|int|string|null public function normalize($object, ?string $format = null, array $context = []): array|\ArrayObject|bool|float|int|string|null
{ {
@@ -45,9 +41,7 @@ class AccompanyingPeriodWorkNormalizer implements \Symfony\Component\Serializer\
if ('docgen' === $format && !($object instanceof AccompanyingPeriodWork || null === $object)) { if ('docgen' === $format && !($object instanceof AccompanyingPeriodWork || null === $object)) {
throw new UnexpectedValueException(sprintf('Object must be an instanceof AccompanyingPeriodWork or null when format is docgen, %s given', get_debug_type($object))); throw new UnexpectedValueException(sprintf('Object must be an instanceof AccompanyingPeriodWork or null when format is docgen, %s given', get_debug_type($object)));
} }
$cleanContext = array_filter($context, fn (string|int $key) => !in_array($key, ['docgen:expects', self::IGNORE_WORK], true), ARRAY_FILTER_USE_KEY);
// Only remove docgen:expects, keep IGNORE_WORK in context
$cleanContext = array_filter($context, fn (string|int $key) => 'docgen:expects' !== $key, ARRAY_FILTER_USE_KEY);
if (null === $object && 'docgen' === $format) { if (null === $object && 'docgen' === $format) {
$dateNull = $this->normalizer->normalize(null, $format, [...$context, 'docgen:expects' => \DateTimeImmutable::class]); $dateNull = $this->normalizer->normalize(null, $format, [...$context, 'docgen:expects' => \DateTimeImmutable::class]);
@@ -77,7 +71,10 @@ class AccompanyingPeriodWorkNormalizer implements \Symfony\Component\Serializer\
]; ];
} }
$initial = $this->normalizer->normalize($object, $format, $cleanContext); $initial = $this->normalizer->normalize($object, $format, array_merge(
$cleanContext,
[self::IGNORE_WORK => null === $object ? null : spl_object_hash($object)]
));
// due to bug: https://api-platform.com/docs/core/serialization/#collection-relation // due to bug: https://api-platform.com/docs/core/serialization/#collection-relation
// and also: https://github.com/symfony/symfony/issues/36965 // and also: https://github.com/symfony/symfony/issues/36965
@@ -85,7 +82,7 @@ class AccompanyingPeriodWorkNormalizer implements \Symfony\Component\Serializer\
$initial['accompanyingPeriodWorkEvaluations'] = $this->normalizer->normalize( $initial['accompanyingPeriodWorkEvaluations'] = $this->normalizer->normalize(
$object->getAccompanyingPeriodWorkEvaluations()->getValues(), $object->getAccompanyingPeriodWorkEvaluations()->getValues(),
$format, $format,
$cleanContext [...$cleanContext]
); );
// add the referrers // add the referrers
@@ -121,7 +118,7 @@ class AccompanyingPeriodWorkNormalizer implements \Symfony\Component\Serializer\
'relatedEntityClass' => AccompanyingPeriodWork::class, 'relatedEntityClass' => AccompanyingPeriodWork::class,
]); ]);
$initial['workflows'] = $this->normalizer->normalize($workflows, 'json', $contextWithIgnore); $initial['workflows'] = $this->normalizer->normalize($workflows, 'json', $context);
} }
return $initial; return $initial;
@@ -130,15 +127,19 @@ class AccompanyingPeriodWorkNormalizer implements \Symfony\Component\Serializer\
public function supportsNormalization($data, ?string $format = null, array $context = []): bool public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{ {
return match ($format) { return match ($format) {
'json' => $data instanceof AccompanyingPeriodWork, 'json' => $data instanceof AccompanyingPeriodWork && ($context[self::IGNORE_WORK] ?? null) !== spl_object_hash($data),
'docgen' => ($data instanceof AccompanyingPeriodWork 'docgen' => ($data instanceof AccompanyingPeriodWork || (null === $data && ($context['docgen:expects'] ?? null) === AccompanyingPeriodWork::class))
|| (null === $data && ($context['docgen:expects'] ?? null) === AccompanyingPeriodWork::class)), && !array_key_exists(self::IGNORE_WORK, $context),
default => false, default => false,
}; };
} }
public function getSupportedTypes(?string $format): array public function getSupportedTypes(?string $format): array
{ {
return 'json' === $format ? [AccompanyingPeriodWork::class => null] : []; return match ($format) {
'json', 'docgen' => [AccompanyingPeriodWork::class => true],
default => [],
};
} }
} }

View File

@@ -103,3 +103,4 @@ class SocialActionNormalizer implements NormalizerAwareInterface, NormalizerInte
return []; return [];
} }
} }

View File

@@ -56,19 +56,32 @@ class SocialIssueNormalizer implements \Symfony\Component\Serializer\Normalizer\
public function supportsNormalization($data, $format = null, array $context = []): bool public function supportsNormalization($data, $format = null, array $context = []): bool
{ {
return match ($format) { if ($data instanceof SocialIssue && 'json' === $format) {
'json' => $data instanceof SocialIssue, return true;
'docgen' => $data instanceof SocialIssue || }
(null === $data && ($context['docgen:expects'] ?? null) === SocialIssue::class),
default => false, if ('docgen' === $format) {
}; if ($data instanceof SocialIssue) {
return true;
}
if (null === $data && SocialIssue::class === ($context['docgen:expects'] ?? null)) {
return true;
}
}
return false;
} }
public function getSupportedTypes(?string $format): array public function getSupportedTypes(?string $format): array
{ {
return match ($format) { if ('json' === $format || 'docgen' === $format) {
'json', 'docgen' => [SocialIssue::class => true], return [
default => [], SocialIssue::class => true,
}; ];
}
return [];
} }
} }