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

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
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