Skip to content
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
15 changes: 12 additions & 3 deletions src/Symfony/SymfonyMessageSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public function decode(string $serializedMessage): object
{
try
{
/** @psalm-var array{message:array<string, string|int|float|null>, namespace:class-string} $data */
$data = $this->serializer->unserialize($serializedMessage);

self::validateUnserializedData($data);
Expand Down Expand Up @@ -162,7 +161,7 @@ public function normalize(object $message): array
}

/**
* @psalm-param array{message:array<string, string|int|float|null>, namespace:class-string} $data
* @psalm-assert array{message:array<string, mixed>, namespace:class-string} $data
*
* @throws \UnexpectedValueException
*/
Expand All @@ -178,12 +177,22 @@ private static function validateUnserializedData(array $data): void
);
}

if (false === \is_array($data['message']))
{
throw new \UnexpectedValueException('"message" field from serialized data should be an array');
}

if (false === \is_string($data['namespace']))
{
throw new \UnexpectedValueException('"namespace" field from serialized data should be a string');
}

/**
* Let's check if the specified class exists.
*
* @psalm-suppress DocblockTypeContradiction
*/
if ($data['namespace'] === '' || \class_exists((string) $data['namespace']) === false)
if ($data['namespace'] === '' || \class_exists($data['namespace']) === false)
{
throw new \UnexpectedValueException(
\sprintf('Class "%s" not found', $data['namespace'])
Expand Down
28 changes: 27 additions & 1 deletion tests/Symfony/SymfonyMessageSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,32 @@ public static function classWithClosedConstructor(): void
);
}

/**
* @test
*
* @throws \Throwable
*/
public function messageNotArray(): void
{
$this->expectException(DecodeMessageFailed::class);
$this->expectExceptionMessage('"message" field from serialized data should be an array');

(new SymfonyMessageSerializer())->decode(\json_encode(['message' => 'someValue', 'namespace' => \SomeClass::class]));
}

/**
* @test
*
* @throws \Throwable
*/
public function namespaceNotString(): void
{
$this->expectException(DecodeMessageFailed::class);
$this->expectExceptionMessage('"namespace" field from serialized data should be a string');

(new SymfonyMessageSerializer())->decode(\json_encode(['message' => [], 'namespace' => new \stdClass]));
}

/**
* @test
*
Expand All @@ -82,7 +108,7 @@ public function classNotFound(): void
$this->expectException(DecodeMessageFailed::class);
$this->expectExceptionMessage('Class "SomeClass" not found');

(new SymfonyMessageSerializer())->decode(\json_encode(['message' => 'someValue', 'namespace' => \SomeClass::class]));
(new SymfonyMessageSerializer())->decode(\json_encode(['message' => [], 'namespace' => \SomeClass::class]));
}

/**
Expand Down