mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-25 16:14:59 +00:00
fix: circular reference problems during fetches
- Add code snippet to avoid circular reference in SocialActionNormalizer.php, AccompanyingPeriodWorkEvaluationDocumentNormalizer.php, AccompanyingPeriodWorkEvaluationNormalizer.php, and AccompanyingPeriodWorkNormalizer.php
This commit is contained in:
@@ -29,18 +29,31 @@ class AccompanyingPeriodWorkEvaluationDocumentNormalizer implements \Symfony\Com
|
||||
|
||||
public function normalize($object, ?string $format = null, array $context = []): array
|
||||
{
|
||||
// Prevent circular references
|
||||
$objectHash = spl_object_hash($object);
|
||||
$visitedKey = 'evaluation_document_visited';
|
||||
|
||||
if (isset($context[$visitedKey][$objectHash])) {
|
||||
return [
|
||||
'id' => $object->getId(),
|
||||
'type' => 'accompanying_period_work_evaluation_document',
|
||||
];
|
||||
}
|
||||
|
||||
$context[$visitedKey][$objectHash] = true;
|
||||
|
||||
$initial = $this->normalizer->normalize($object, $format, array_merge($context, [
|
||||
self::SKIP => spl_object_hash($object),
|
||||
]));
|
||||
|
||||
$initial['workflows_availables'] = $this->metadataExtractor->availableWorkflowFor(
|
||||
AccompanyingPeriodWorkEvaluationDocument::class,
|
||||
$object->getId()
|
||||
$object->getId() ?? 0
|
||||
);
|
||||
|
||||
$workflows = $this->entityWorkflowRepository->findBy([
|
||||
'relatedEntityClass' => AccompanyingPeriodWorkEvaluationDocument::class,
|
||||
'relatedEntityId' => $object->getId(),
|
||||
'relatedEntityId' => $object->getId() ?? 0,
|
||||
]);
|
||||
$initial['workflows'] = $this->normalizer->normalize($workflows, 'json', $context);
|
||||
|
||||
|
@@ -33,10 +33,22 @@ class AccompanyingPeriodWorkEvaluationNormalizer implements \Symfony\Component\S
|
||||
*/
|
||||
public function normalize($object, ?string $format = null, array $context = []): array
|
||||
{
|
||||
// Prevent circular references
|
||||
$objectHash = spl_object_hash($object);
|
||||
$visitedKey = 'evaluation_visited';
|
||||
|
||||
if (isset($context[$visitedKey][$objectHash])) {
|
||||
return [
|
||||
'id' => $object->getId(),
|
||||
'type' => 'accompanying_period_work_evaluation',
|
||||
];
|
||||
}
|
||||
|
||||
$context[$visitedKey][$objectHash] = true;
|
||||
|
||||
$initial = $this->normalizer->normalize($object, $format, array_merge(
|
||||
$context,
|
||||
[self::IGNORE_EVALUATION => spl_object_hash($object)],
|
||||
[AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => static fn ($object, $format, $context) => $object->getId()]
|
||||
[self::IGNORE_EVALUATION => spl_object_hash($object)]
|
||||
));
|
||||
|
||||
// due to bug: https://api-platform.com/docs/core/serialization/#collection-relation
|
||||
@@ -52,12 +64,12 @@ class AccompanyingPeriodWorkEvaluationNormalizer implements \Symfony\Component\S
|
||||
|
||||
$initial['workflows_availables'] = $this->metadataExtractor->availableWorkflowFor(
|
||||
AccompanyingPeriodWorkEvaluation::class,
|
||||
$object->getId()
|
||||
$object->getId() ?? 0
|
||||
);
|
||||
|
||||
$workflows = $this->entityWorkflowRepository->findBy([
|
||||
'relatedEntityClass' => AccompanyingPeriodWorkEvaluation::class,
|
||||
'relatedEntityId' => $object->getId(),
|
||||
'relatedEntityId' => $object->getId() ?? 0,
|
||||
]);
|
||||
$initial['workflows'] = $this->normalizer->normalize($workflows, 'json', $context);
|
||||
|
||||
|
@@ -43,6 +43,24 @@ class AccompanyingPeriodWorkNormalizer implements \Symfony\Component\Serializer\
|
||||
throw new UnexpectedValueException(sprintf('Object must be an instanceof AccompanyingPeriodWork or null when format is docgen, %s given', get_debug_type($object)));
|
||||
}
|
||||
|
||||
// Prevent circular references
|
||||
if ($object instanceof AccompanyingPeriodWork && 'json' === $format) {
|
||||
$objectHash = spl_object_hash($object);
|
||||
$visitedKey = 'accompanying_period_work_visited';
|
||||
|
||||
if (isset($context[$visitedKey][$objectHash])) {
|
||||
return [
|
||||
'id' => $object->getId(),
|
||||
'type' => 'accompanying_period_work',
|
||||
'startDate' => $object->getStartDate()?->format('c'),
|
||||
'endDate' => $object->getEndDate()?->format('c'),
|
||||
'note' => $object->getNote(),
|
||||
];
|
||||
}
|
||||
|
||||
$context[$visitedKey][$objectHash] = true;
|
||||
}
|
||||
|
||||
$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) {
|
||||
@@ -106,7 +124,7 @@ class AccompanyingPeriodWorkNormalizer implements \Symfony\Component\Serializer\
|
||||
// then, we add normalization for things which are not into the entity
|
||||
$initial['workflows_availables'] = $this->metadataExtractor->availableWorkflowFor(
|
||||
AccompanyingPeriodWork::class,
|
||||
$object->getId()
|
||||
$object->getId() ?? 0
|
||||
);
|
||||
|
||||
$initial['workflows_availables_evaluation'] = $this->metadataExtractor->availableWorkflowFor(
|
||||
@@ -119,7 +137,7 @@ class AccompanyingPeriodWorkNormalizer implements \Symfony\Component\Serializer\
|
||||
|
||||
$workflows = $this->entityWorkflowRepository->findBy([
|
||||
'relatedEntityClass' => AccompanyingPeriodWork::class,
|
||||
'relatedEntityId' => $object->getId(),
|
||||
'relatedEntityId' => $object->getId() ?? 0,
|
||||
]);
|
||||
|
||||
$initial['workflows'] = $this->normalizer->normalize($workflows, 'json', $context);
|
||||
|
@@ -27,6 +27,22 @@ class SocialActionNormalizer implements NormalizerAwareInterface, NormalizerInte
|
||||
{
|
||||
switch ($format) {
|
||||
case 'json':
|
||||
// Prevent circular references
|
||||
$objectHash = spl_object_hash($socialAction);
|
||||
$visitedKey = 'social_action_visited';
|
||||
|
||||
if (isset($context[$visitedKey][$objectHash])) {
|
||||
return [
|
||||
'id' => $socialAction->getId(),
|
||||
'type' => 'social_work_social_action',
|
||||
'text' => $this->render->renderString($socialAction, []),
|
||||
'title' => $socialAction->getTitle(),
|
||||
'ordering' => $socialAction->getOrdering(),
|
||||
];
|
||||
}
|
||||
|
||||
$context[$visitedKey][$objectHash] = true;
|
||||
|
||||
return [
|
||||
'id' => $socialAction->getId(),
|
||||
'type' => 'social_work_social_action',
|
||||
|
Reference in New Issue
Block a user