Skip to content

Commit

Permalink
feat(serializer): update MissingConstructorArgumentsException message (
Browse files Browse the repository at this point in the history
  • Loading branch information
ERuban committed Apr 21, 2024
1 parent fce42e0 commit 57fe136
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion features/serializer/vo_relations.feature
Expand Up @@ -155,7 +155,7 @@ Feature: Value object as ApiResource
"pattern": "^An error occurred$"
},
"hydra:description": {
"pattern": "^Cannot create an instance of \"ApiPlatform\\\\Tests\\\\Fixtures\\\\TestBundle\\\\(Document|Entity)\\\\VoDummyCar\" from serialized data because its constructor requires parameter \"drivers\" to be present.$"
"pattern": "^Cannot create an instance of \"ApiPlatform\\\\Tests\\\\Fixtures\\\\TestBundle\\\\(Document|Entity)\\\\VoDummyCar\" from serialized data because its constructor requires the following parameters to be present : \"\\$drivers\".$"
}
},
"required": [
Expand Down
7 changes: 6 additions & 1 deletion src/Serializer/AbstractItemNormalizer.php
Expand Up @@ -316,6 +316,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
$constructorParameters = $constructor->getParameters();

$params = [];
$missingConstructorArguments = [];
foreach ($constructorParameters as $constructorParameter) {
$paramName = $constructorParameter->name;
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;
Expand Down Expand Up @@ -350,14 +351,18 @@ protected function instantiateObject(array &$data, string $class, array &$contex
$params[] = $constructorParameter->getDefaultValue();
} else {
if (!isset($context['not_normalizable_value_exceptions'])) {
throw new MissingConstructorArgumentsException(sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires parameter "%s" to be present.', $class, $constructorParameter->name), 0, null, [$constructorParameter->name]);
$missingConstructorArguments[] = $constructorParameter->name;
}

$exception = NotNormalizableValueException::createForUnexpectedDataType(sprintf('Failed to create object because the class misses the "%s" property.', $constructorParameter->name), $data, ['unknown'], $context['deserialization_path'] ?? null, true);
$context['not_normalizable_value_exceptions'][] = $exception;
}
}

if ($missingConstructorArguments) {
throw new MissingConstructorArgumentsException(sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires the following parameters to be present : "$%s".', $class, implode('", "$', $missingConstructorArguments)), 0, null, $missingConstructorArguments, $class);
}

if (\count($context['not_normalizable_value_exceptions'] ?? []) > 0) {
return $reflectionClass->newInstanceWithoutConstructor();
}
Expand Down

0 comments on commit 57fe136

Please sign in to comment.