Skip to content

Commit 216d9cc

Browse files
authoredOct 30, 2024··
fix(serializer): fetch type on normalization error when possible (#6761)
1 parent 9ba1ae1 commit 216d9cc

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed
 

‎features/main/validation.feature

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ Feature: Using validations groups
114114
"@context": "/contexts/ConstraintViolationList",
115115
"@type": "ConstraintViolationList",
116116
"hydra:title": "An error occurred",
117-
"hydra:description": "This value should be of type unknown.\nqux: This value should be of type string.\nfoo: This value should be of type bool.\nbar: This value should be of type int.\nuuid: This value should be of type uuid.\nrelatedDummy: This value should be of type array|string.\nrelatedDummies: This value should be of type array.",
117+
"hydra:description": "baz: This value should be of type string.\nqux: This value should be of type string.\nfoo: This value should be of type bool.\nbar: This value should be of type int.\nuuid: This value should be of type uuid.\nrelatedDummy: This value should be of type array|string.\nrelatedDummies: This value should be of type array.",
118118
"violations": [
119119
{
120-
"propertyPath": "",
121-
"message": "This value should be of type unknown.",
120+
"propertyPath": "baz",
121+
"message": "This value should be of type string.",
122122
"code": "ba785a8c-82cb-4283-967c-3cf342181b40",
123123
"hint": "Failed to create object because the class misses the \"baz\" property."
124124
},

‎src/Serializer/AbstractItemNormalizer.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,20 @@ protected function instantiateObject(array &$data, string $class, array &$contex
351351
$missingConstructorArguments[] = $constructorParameter->name;
352352
}
353353

354-
$exception = NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Failed to create object because the class misses the "%s" property.', $constructorParameter->name), $data, ['unknown'], $context['deserialization_path'] ?? null, true);
354+
$attributeContext = $this->getAttributeDenormalizationContext($class, $paramName, $context);
355+
$constructorParameterType = 'unknown';
356+
$reflectionType = $constructorParameter->getType();
357+
if ($reflectionType instanceof \ReflectionNamedType) {
358+
$constructorParameterType = $reflectionType->getName();
359+
}
360+
361+
$exception = NotNormalizableValueException::createForUnexpectedDataType(
362+
\sprintf('Failed to create object because the class misses the "%s" property.', $constructorParameter->name),
363+
null,
364+
[$constructorParameterType],
365+
$attributeContext['deserialization_path'] ?? null,
366+
true
367+
);
355368
$context['not_normalizable_value_exceptions'][] = $exception;
356369
}
357370
}

0 commit comments

Comments
 (0)
Please sign in to comment.