Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[PropertyAccess][Serializer] Fix "type unknown" on denormalize#52879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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 numberDiff line numberDiff 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 typeunknown.', $validationFailedException->getViolations()[0]->getMessage());
$this->assertSame('This value should be of typestring.', $validationFailedException->getViolations()[0]->getMessage());
$this->assertSame('Test', $validationFailedException->getViolations()[1]->getMessage());
}
}
Expand DownExpand 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 typeunknown.', $validationFailedException->getViolations()[0]->getMessage());
$this->assertSame('This value should be of typestring.', $validationFailedException->getViolations()[0]->getMessage());
$this->assertSame('Test', $validationFailedException->getViolations()[1]->getMessage());
}
}
Expand Down
4 changes: 2 additions & 2 deletionssrc/Symfony/Component/HttpKernel/composer.json
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <farhad.safarov@gmail.com>
*/
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 deletionssrc/Symfony/Component/PropertyAccess/PropertyAccessor.php
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 newInvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".',$expectedType,'NULL' ===$actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
throw newInvalidTypeException($expectedType, $actualType, $propertyPath, $previous);
}
if (preg_match('/^Cannot assign (\S+) to property \S+::\$\S+ of type (\S+)$/', $message, $matches)) {
[, $actualType, $expectedType] = $matches;

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

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp