Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Serializer] Fixing PHP warning in the ObjectNormalizer with MaxDepth…
… enabled
  • Loading branch information
jaydiablo authored and nicolas-grekas committed Apr 12, 2024
commit 31e3bde868b01a1ef50bbfb52170b3d275e60519
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ private function updateData(array $data, string $attribute, mixed $attributeValu
private function isMaxDepthReached(array $attributesMetadata, string $class, string $attribute, array &$context): bool
{
if (!($enableMaxDepth = $context[self::ENABLE_MAX_DEPTH] ?? $this->defaultContext[self::ENABLE_MAX_DEPTH] ?? false)
|| null === $maxDepth = $attributesMetadata[$attribute]?->getMaxDepth()
|| !isset($attributesMetadata[$attribute]) || null === $maxDepth = $attributesMetadata[$attribute]?->getMaxDepth()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be:

Suggested change
|| !isset($attributesMetadata[$attribute]) || null === $maxDepth = $attributesMetadata[$attribute]?->getMaxDepth()
|| null === $maxDepth = ($attributesMetadata[$attribute]?->getMaxDepth() ?? null)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning is still thrown in that case as $attributesMetadata[$attribute] is still trying to be accessed.

) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,20 @@ protected function setAttributeValue(object $object, string $attribute, $value,

$this->assertSame('scalar', $normalizer->denormalize('scalar', XmlScalarDummy::class, 'xml')->value);
}

public function testNormalizationWithMaxDepthOnStdclassObjectDoesNotThrowWarning()
{
$object = new \stdClass();
$object->string = 'yes';

$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$normalizer = new ObjectNormalizer($classMetadataFactory);
$normalized = $normalizer->normalize($object, context: [
AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true,
]);

$this->assertSame(['string' => 'yes'], $normalized);
}
}

class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
Expand Down