From 490a110ed83bd90d9cfe5130abb25dd7545c8d2c Mon Sep 17 00:00:00 2001 From: Orestis Date: Tue, 17 Jun 2025 11:50:15 +0200 Subject: [PATCH] Fix TraceableSerializer when collected caller from array map If the TraceableSerializer runs from a callable in an array_map, then the caller looses the file and the line because it was invoked. This causes a hard fail since the required items are not defined. The error is the following: TraceableSerializer.php line 174: Warning: Undefined array key "file". The fix is to get the file and the line from the following array item which always is the caller class. --- .../Serializer/Debug/TraceableSerializer.php | 4 +-- .../Tests/Debug/TraceableSerializerTest.php | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Serializer/Debug/TraceableSerializer.php b/src/Symfony/Component/Serializer/Debug/TraceableSerializer.php index dd22e8678e782..964fd75031833 100644 --- a/src/Symfony/Component/Serializer/Debug/TraceableSerializer.php +++ b/src/Symfony/Component/Serializer/Debug/TraceableSerializer.php @@ -179,8 +179,8 @@ private function getCaller(string $method, string $interface): array && $method === $trace[$i]['function'] && is_a($trace[$i]['class'], $interface, true) ) { - $file = $trace[$i]['file']; - $line = $trace[$i]['line']; + $file = $trace[$i]['file'] ?? $trace[$i + 1]['file']; + $line = $trace[$i]['line'] ?? $trace[$i + 1]['line']; break; } diff --git a/src/Symfony/Component/Serializer/Tests/Debug/TraceableSerializerTest.php b/src/Symfony/Component/Serializer/Tests/Debug/TraceableSerializerTest.php index ea3c851c6040b..dc6e4a6b7a1b6 100644 --- a/src/Symfony/Component/Serializer/Tests/Debug/TraceableSerializerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Debug/TraceableSerializerTest.php @@ -126,6 +126,40 @@ public function testAddDebugTraceIdInContext() $traceableSerializer->encode('data', 'format'); $traceableSerializer->decode('data', 'format'); } + + public function testCollectedCaller() + { + $serializer = new \Symfony\Component\Serializer\Serializer(); + + $collector = new SerializerDataCollector(); + $traceableSerializer = new TraceableSerializer($serializer, $collector); + + $traceableSerializer->normalize('data'); + $collector->lateCollect(); + + $this->assertSame([ + 'name' => 'TraceableSerializerTest.php', + 'file' => __FILE__, + 'line' => __LINE__ - 6, + ], $collector->getData()['normalize'][0]['caller']); + } + + public function testCollectedCallerFromArrayMap() + { + $serializer = new \Symfony\Component\Serializer\Serializer(); + + $collector = new SerializerDataCollector(); + $traceableSerializer = new TraceableSerializer($serializer, $collector); + + array_map([$traceableSerializer, 'normalize'], ['data']); + $collector->lateCollect(); + + $this->assertSame([ + 'name' => 'TraceableSerializerTest.php', + 'file' => __FILE__, + 'line' => __LINE__ - 6, + ], $collector->getData()['normalize'][0]['caller']); + } } class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface