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

[Serializer] Unknown expected type return when invalid values allowed #62313

Open
@leoleva

Description

@leoleva

Symfony version(s) affected

6.4.27

Description

Expected types are not returned in error in case 'allow_invalid_values' => true is passed as context. Would expect that no matter what flag is passed, the expected types do not change (at least in this case)

How to reproduce

Composer:

    "require": {        "symfony/serializer": "^6.4",        "symfony/validator": "^6.4",        "symfony/property-info": "^6.4",        "symfony/property-access": "^6.4"    }

Simplified version:

<?phpuse Symfony\Component\PropertyInfo\Extractor\SerializerExtractor;use Symfony\Component\PropertyInfo\PropertyInfoExtractor;use Symfony\Component\Serializer\Context\SerializerContextBuilder;use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;use Symfony\Component\Serializer\Serializer;require_once "vendor/autoload.php";enum Status: string {    case PENDING = 'pending';    case ACTIVE = 'active';}class Request{    public function __construct(      public int $id,      public Status $status,    ) {    }}$factory = new ClassMetadataFactory(    new AttributeLoader());$serializer = new Serializer(    [        new ArrayDenormalizer(),        new BackedEnumNormalizer(),        new ObjectNormalizer(            new ClassMetadataFactory(                new AttributeLoader()            ),            new Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter(),            null,            new PropertyInfoExtractor(                [                    new SerializerExtractor(                        new ClassMetadataFactory(                            new AttributeLoader()                        )                    ),                ]            ),        ),    ],);$context = ['collect_denormalization_errors' => true, 'allow_invalid_values' => true];try {    $result = $serializer->denormalize(['id' => 123, 'status' => null], Request::class, null, $context);} catch (\Symfony\Component\Serializer\Exception\PartialDenormalizationException $exception) {    var_dump($exception->getErrors()[0]->getExpectedTypes());}

Result:

array(1) {  [0] =>  string(7) "unknown"}

In case'allow_invalid_values' => true is removed from context, we receive:

array(2) {  [0] =>  string(3) "int"  [1] =>  string(6) "string"}

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp