Skip to content

Commit 4d5edae

Browse files
committed
bug #41378 [Messenger] Fix ErrorDetailsStamp denormalization (wucdbm)
This PR was submitted for the 5.4 branch but it was squashed and merged into the 5.3 branch instead. Discussion ---------- [Messenger] Fix `ErrorDetailsStamp` denormalization | Q | A | ------------- | --- | Branch? | 5.2.1+ <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #39689 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch 5.x. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry --> Fixes #39689 Trouble is, tests did not cover cases where a `PropertyTypeExtractorInterface` is used, in conjunction with a variable type such as `int|mixed` IMO this does not really fix the issue with the Serializer component, it being that if we have mixed, we should really be allowing anything. Another point is whether that type makes sense and shouldn't be just `mixed` instead. I've changed it to `int|string` as in that context, those are the possible types, string being rather rare. I suspect a PDO Exception as I've had those in the past, there was a similar bug with FlattenException. This was a nightmare to debug/reproduce because it happened once a fortnight in production. Commits ------- 09eec5e [Messenger] Fix `ErrorDetailsStamp` denormalization
2 parents 67d8a59 + 09eec5e commit 4d5edae

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/Symfony/Component/Messenger/Stamp/ErrorDetailsStamp.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class ErrorDetailsStamp implements StampInterface
2323
/** @var string */
2424
private $exceptionClass;
2525

26-
/** @var int|mixed */
26+
/** @var int|string */
2727
private $exceptionCode;
2828

2929
/** @var string */
@@ -33,7 +33,7 @@ final class ErrorDetailsStamp implements StampInterface
3333
private $flattenException;
3434

3535
/**
36-
* @param int|mixed $exceptionCode
36+
* @param int|string $exceptionCode
3737
*/
3838
public function __construct(string $exceptionClass, $exceptionCode, string $exceptionMessage, FlattenException $flattenException = null)
3939
{

src/Symfony/Component/Messenger/Tests/Stamp/ErrorDetailsStampTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Symfony\Component\Messenger\Stamp\ErrorDetailsStamp;
1919
use Symfony\Component\Messenger\Transport\Serialization\Normalizer\FlattenExceptionNormalizer;
2020
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
21+
use Symfony\Component\PropertyInfo\Extractor\ConstructorExtractor;
22+
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
2123
use Symfony\Component\Serializer\Encoder\JsonEncoder;
2224
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
2325
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
@@ -70,4 +72,26 @@ public function testDeserialization()
7072
$this->assertInstanceOf(ErrorDetailsStamp::class, $deserializedStamp);
7173
$this->assertEquals($stamp, $deserializedStamp);
7274
}
75+
76+
public function testDeserializationWithStringExceptionCode()
77+
{
78+
$exception = new StringErrorCodeException('exception message', 'some code');
79+
$stamp = ErrorDetailsStamp::create($exception);
80+
$extractor = new ConstructorExtractor([
81+
new ReflectionExtractor(),
82+
]);
83+
$serializer = new Serializer(
84+
new SymfonySerializer([
85+
new ArrayDenormalizer(),
86+
new FlattenExceptionNormalizer(),
87+
new ObjectNormalizer(null, null, null, $extractor, null, null, []),
88+
], [new JsonEncoder()])
89+
);
90+
91+
$deserializedEnvelope = $serializer->decode($serializer->encode(new Envelope(new \stdClass(), [$stamp])));
92+
93+
$deserializedStamp = $deserializedEnvelope->last(ErrorDetailsStamp::class);
94+
$this->assertInstanceOf(ErrorDetailsStamp::class, $deserializedStamp);
95+
$this->assertEquals($stamp, $deserializedStamp);
96+
}
7397
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\Messenger\Tests\Stamp;
4+
5+
class StringErrorCodeException extends \Exception
6+
{
7+
public function __construct(string $message, string $code)
8+
{
9+
parent::__construct($message);
10+
$this->code = $code;
11+
}
12+
}

0 commit comments

Comments
 (0)