Skip to content

Commit 003dbee

Browse files
committed
[Serializer] [PropertyAccess] Fix "type unknown" on denormalize
1 parent 0b0484f commit 003dbee

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/RequestPayloadValueResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public function testValidationNotPassed()
252252
} catch (HttpException $e) {
253253
$validationFailedException = $e->getPrevious();
254254
$this->assertInstanceOf(ValidationFailedException::class, $validationFailedException);
255-
$this->assertSame('This value should be of type unknown.', $validationFailedException->getViolations()[0]->getMessage());
255+
$this->assertSame('This value should be of type string.', $validationFailedException->getViolations()[0]->getMessage());
256256
$this->assertSame('Test', $validationFailedException->getViolations()[1]->getMessage());
257257
}
258258
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\PropertyAccess\Exception;
13+
14+
/**
15+
* Thrown when a type of given value does not match an expected type.
16+
*
17+
* @author Farhad Safarov <farhad.safarov@gmail.com>
18+
*/
19+
class InvalidTypeException extends InvalidArgumentException
20+
{
21+
public function __construct(
22+
public readonly string $expectedType,
23+
public readonly string $actualType,
24+
public readonly string $propertyPath,
25+
\Throwable $previous = null,
26+
) {
27+
parent::__construct(
28+
sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath),
29+
previous: $previous,
30+
);
31+
}
32+
}

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Symfony\Component\Cache\Adapter\ApcuAdapter;
1919
use Symfony\Component\Cache\Adapter\NullAdapter;
2020
use Symfony\Component\PropertyAccess\Exception\AccessException;
21-
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
21+
use Symfony\Component\PropertyAccess\Exception\InvalidTypeException;
2222
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
2323
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
2424
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
@@ -208,12 +208,12 @@ private static function throwInvalidArgumentException(string $message, array $tr
208208
if (preg_match('/^\S+::\S+\(\): Argument #\d+ \(\$\S+\) must be of type (\S+), (\S+) given/', $message, $matches)) {
209209
[, $expectedType, $actualType] = $matches;
210210

211-
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
211+
throw new InvalidTypeException($expectedType, $actualType, $propertyPath, $previous);
212212
}
213213
if (preg_match('/^Cannot assign (\S+) to property \S+::\$\S+ of type (\S+)$/', $message, $matches)) {
214214
[, $actualType, $expectedType] = $matches;
215215

216-
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
216+
throw new InvalidTypeException($expectedType, $actualType, $propertyPath, $previous);
217217
}
218218
}
219219

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Normalizer;
1313

1414
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException as PropertyAccessInvalidArgumentException;
15+
use Symfony\Component\PropertyAccess\Exception\InvalidTypeException as PropertyAccessInvalidTypeException;
1516
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
1617
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
1718
use Symfony\Component\PropertyAccess\PropertyAccess;
@@ -398,7 +399,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
398399
$exception = NotNormalizableValueException::createForUnexpectedDataType(
399400
sprintf('Failed to denormalize attribute "%s" value for class "%s": '.$e->getMessage(), $attribute, $type),
400401
$data,
401-
['unknown'],
402+
$e instanceof PropertyAccessInvalidTypeException ? [$e->expectedType] : ['unknown'],
402403
$context['deserialization_path'] ?? null,
403404
false,
404405
$e->getCode(),

0 commit comments

Comments
 (0)