Skip to content

[Serializer] Fix TraceableSerializer when called from a callable inside array_map #60809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2025
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
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.
  • Loading branch information
OrestisZag committed Jun 17, 2025
commit 490a110ed83bd90d9cfe5130abb25dd7545c8d2c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Copy link
Contributor

@ruudk ruudk Jun 17, 2025

Choose a reason for hiding this comment

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

When using array_map with a callable like above or a native first class callable like $traceableSerializer->normalize(...) the problem occurs. Somehow PHP does not provide a file and line for that location when using debug_backtrace.

This is an example of the trace (that is called somewhere else), but to show what is happening:
Screenshot 2025-06-17 at 12 15 54@2x

$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
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy