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

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
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public function testValidationNotPassed()
$validationFailedException = $e->getPrevious();
$this->assertSame(422, $e->getStatusCode());
$this->assertInstanceOf(ValidationFailedException::class, $validationFailedException);
$this->assertSame('This value should be of type unknown.', $validationFailedException->getViolations()[0]->getMessage());
$this->assertSame('This value should be of type string.', $validationFailedException->getViolations()[0]->getMessage());
$this->assertSame('Test', $validationFailedException->getViolations()[1]->getMessage());
}
}
Expand Down Expand Up @@ -665,7 +665,7 @@ public function testRequestPayloadValidationErrorCustomStatusCode()
$validationFailedException = $e->getPrevious();
$this->assertSame(400, $e->getStatusCode());
$this->assertInstanceOf(ValidationFailedException::class, $validationFailedException);
$this->assertSame('This value should be of type unknown.', $validationFailedException->getViolations()[0]->getMessage());
$this->assertSame('This value should be of type string.', $validationFailedException->getViolations()[0]->getMessage());
$this->assertSame('Test', $validationFailedException->getViolations()[1]->getMessage());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpKernel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"symfony/finder": "^6.4|^7.0",
"symfony/http-client-contracts": "^2.5|^3",
"symfony/process": "^6.4|^7.0",
"symfony/property-access": "^6.4|^7.0",
"symfony/property-access": "^7.1",
"symfony/routing": "^6.4|^7.0",
"symfony/serializer": "^6.4|^7.0",
"symfony/serializer": "^7.1",
"symfony/stopwatch": "^6.4|^7.0",
"symfony/translation": "^6.4|^7.0",
"symfony/translation-contracts": "^2.5|^3",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyAccess\Exception;

/**
* Thrown when a type of given value does not match an expected type.
*
* @author Farhad Safarov <[email protected]>
*/
class InvalidTypeException extends InvalidArgumentException
{
public function __construct(
public readonly string $expectedType,
public readonly string $actualType,
public readonly string $propertyPath,
\Throwable $previous = null,
) {
parent::__construct(
sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath),
previous: $previous,
);
}
}
6 changes: 3 additions & 3 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Component\PropertyAccess\Exception\AccessException;
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
use Symfony\Component\PropertyAccess\Exception\InvalidTypeException;
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
Expand Down Expand Up @@ -192,12 +192,12 @@ private static function throwInvalidArgumentException(string $message, array $tr
if (preg_match('/^\S+::\S+\(\): Argument #\d+ \(\$\S+\) must be of type (\S+), (\S+) given/', $message, $matches)) {
[, $expectedType, $actualType] = $matches;

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException as PropertyAccessInvalidArgumentException;
use Symfony\Component\PropertyAccess\Exception\InvalidTypeException;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
Expand Down Expand Up @@ -374,7 +375,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
$exception = NotNormalizableValueException::createForUnexpectedDataType(
sprintf('Failed to denormalize attribute "%s" value for class "%s": '.$e->getMessage(), $attribute, $type),
$data,
['unknown'],
$e instanceof InvalidTypeException ? [$e->expectedType] : ['unknown'],
$context['deserialization_path'] ?? null,
false,
$e->getCode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyAccess\Exception\InvalidTypeException;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
Expand Down Expand Up @@ -835,6 +837,24 @@ public function testNormalizeStdClass()

$this->assertSame(['baz' => 'baz'], $this->normalizer->normalize($o2));
}

public function testNotNormalizableValueInvalidType()
{
if (!class_exists(InvalidTypeException::class)) {
$this->markTestSkipped('Skipping test as the improvements on PropertyAccess are required.');
}

$this->expectException(NotNormalizableValueException::class);
$this->expectExceptionMessage('Expected argument of type "string", "array" given at property path "initialized"');

try {
$this->normalizer->denormalize(['initialized' => ['not a string']], TypedPropertiesObject::class, 'array');
} catch (NotNormalizableValueException $e) {
$this->assertSame(['string'], $e->getExpectedTypes());

throw $e;
}
}
}

class ProxyObjectDummy extends ObjectDummy
Expand Down