[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\Workflow\Registry;
class AccompanyingPeriodWorkEvaluationNormalizer implements NormalizerInterface, NormalizerAwareInterface
class AccompanyingPeriodWorkEvaluationNormalizer implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
public function __construct(
private readonly Registry $registry,
private readonly EntityWorkflowRepository $entityWorkflowRepository,
private readonly MetadataExtractor $metadataExtractor,
) {}
private const IGNORE_EVALUATION = 'evaluation:ignore';
public function __construct(private readonly Registry $registry, private readonly EntityWorkflowRepository $entityWorkflowRepository, private readonly MetadataExtractor $metadataExtractor) {}
/**
* @param AccompanyingPeriodWorkEvaluation $object
*/
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(
$object->getDocuments()->getValues(),
$format,
$context
);
$initial['accompanyingPeriodWork'] = $this->normalizer->normalize(
$object->getAccompanyingPeriodWork(),
$format,
$context
);
// then, we add normalization for things which are not into the entity
// Add additional workflows
$initial['workflows_availables'] = $this->metadataExtractor->availableWorkflowFor(
AccompanyingPeriodWorkEvaluation::class
AccompanyingPeriodWorkEvaluation::class,
);
$workflows = $this->entityWorkflowRepository->findBy([
@@ -64,11 +62,13 @@ class AccompanyingPeriodWorkEvaluationNormalizer implements NormalizerInterface,
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{
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
{
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';
public function __construct(
private readonly Registry $registry,
private readonly EntityWorkflowRepository $entityWorkflowRepository,
private readonly MetadataExtractor $metadataExtractor,
) {}
public function __construct(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
{
@@ -45,9 +41,7 @@ class AccompanyingPeriodWorkNormalizer implements \Symfony\Component\Serializer\
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)));
}
// Only remove docgen:expects, keep IGNORE_WORK in context
$cleanContext = array_filter($context, fn (string|int $key) => 'docgen:expects' !== $key, ARRAY_FILTER_USE_KEY);
$cleanContext = array_filter($context, fn (string|int $key) => !in_array($key, ['docgen:expects', self::IGNORE_WORK], true), ARRAY_FILTER_USE_KEY);
if (null === $object && 'docgen' === $format) {
$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
// and also: https://github.com/symfony/symfony/issues/36965
@@ -85,7 +82,7 @@ class AccompanyingPeriodWorkNormalizer implements \Symfony\Component\Serializer\
$initial['accompanyingPeriodWorkEvaluations'] = $this->normalizer->normalize(
$object->getAccompanyingPeriodWorkEvaluations()->getValues(),
$format,
$cleanContext
[...$cleanContext]
);
// add the referrers
@@ -121,7 +118,7 @@ class AccompanyingPeriodWorkNormalizer implements \Symfony\Component\Serializer\
'relatedEntityClass' => AccompanyingPeriodWork::class,
]);
$initial['workflows'] = $this->normalizer->normalize($workflows, 'json', $contextWithIgnore);
$initial['workflows'] = $this->normalizer->normalize($workflows, 'json', $context);
}
return $initial;
@@ -130,15 +127,19 @@ class AccompanyingPeriodWorkNormalizer implements \Symfony\Component\Serializer\
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{
return match ($format) {
'json' => $data instanceof AccompanyingPeriodWork,
'docgen' => ($data instanceof AccompanyingPeriodWork
|| (null === $data && ($context['docgen:expects'] ?? null) === AccompanyingPeriodWork::class)),
'json' => $data instanceof AccompanyingPeriodWork && ($context[self::IGNORE_WORK] ?? null) !== spl_object_hash($data),
'docgen' => ($data instanceof AccompanyingPeriodWork || (null === $data && ($context['docgen:expects'] ?? null) === AccompanyingPeriodWork::class))
&& !array_key_exists(self::IGNORE_WORK, $context),
default => false,
};
}
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 [];
}
}

View File

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