Skip to content

Commit 94c2b34

Browse files
bug #60809 [Serializer] Fix TraceableSerializer when called from a callable inside array_map (OrestisZag)
This PR was merged into the 6.4 branch. Discussion ---------- [Serializer] Fix `TraceableSerializer` when called from a callable inside `array_map` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT 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. Commits ------- 490a110 Fix TraceableSerializer when collected caller from array map
2 parents f37df38 + 490a110 commit 94c2b34

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/Symfony/Component/Serializer/Debug/TraceableSerializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ private function getCaller(string $method, string $interface): array
179179
&& $method === $trace[$i]['function']
180180
&& is_a($trace[$i]['class'], $interface, true)
181181
) {
182-
$file = $trace[$i]['file'];
183-
$line = $trace[$i]['line'];
182+
$file = $trace[$i]['file'] ?? $trace[$i + 1]['file'];
183+
$line = $trace[$i]['line'] ?? $trace[$i + 1]['line'];
184184

185185
break;
186186
}

src/Symfony/Component/Serializer/Tests/Debug/TraceableSerializerTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,40 @@ public function testAddDebugTraceIdInContext()
126126
$traceableSerializer->encode('data', 'format');
127127
$traceableSerializer->decode('data', 'format');
128128
}
129+
130+
public function testCollectedCaller()
131+
{
132+
$serializer = new \Symfony\Component\Serializer\Serializer();
133+
134+
$collector = new SerializerDataCollector();
135+
$traceableSerializer = new TraceableSerializer($serializer, $collector);
136+
137+
$traceableSerializer->normalize('data');
138+
$collector->lateCollect();
139+
140+
$this->assertSame([
141+
'name' => 'TraceableSerializerTest.php',
142+
'file' => __FILE__,
143+
'line' => __LINE__ - 6,
144+
], $collector->getData()['normalize'][0]['caller']);
145+
}
146+
147+
public function testCollectedCallerFromArrayMap()
148+
{
149+
$serializer = new \Symfony\Component\Serializer\Serializer();
150+
151+
$collector = new SerializerDataCollector();
152+
$traceableSerializer = new TraceableSerializer($serializer, $collector);
153+
154+
array_map([$traceableSerializer, 'normalize'], ['data']);
155+
$collector->lateCollect();
156+
157+
$this->assertSame([
158+
'name' => 'TraceableSerializerTest.php',
159+
'file' => __FILE__,
160+
'line' => __LINE__ - 6,
161+
], $collector->getData()['normalize'][0]['caller']);
162+
}
129163
}
130164

131165
class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface

0 commit comments

Comments
 (0)
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