-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Closed
Description
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:
<?php
use 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