Skip to content

Commit 57fe136

Browse files
authored
feat(serializer): update MissingConstructorArgumentsException message (#5902)
1 parent fce42e0 commit 57fe136

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

features/serializer/vo_relations.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ Feature: Value object as ApiResource
155155
"pattern": "^An error occurred$"
156156
},
157157
"hydra:description": {
158-
"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.$"
158+
"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\".$"
159159
}
160160
},
161161
"required": [

src/Serializer/AbstractItemNormalizer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
316316
$constructorParameters = $constructor->getParameters();
317317

318318
$params = [];
319+
$missingConstructorArguments = [];
319320
foreach ($constructorParameters as $constructorParameter) {
320321
$paramName = $constructorParameter->name;
321322
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;
@@ -350,14 +351,18 @@ protected function instantiateObject(array &$data, string $class, array &$contex
350351
$params[] = $constructorParameter->getDefaultValue();
351352
} else {
352353
if (!isset($context['not_normalizable_value_exceptions'])) {
353-
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]);
354+
$missingConstructorArguments[] = $constructorParameter->name;
354355
}
355356

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

362+
if ($missingConstructorArguments) {
363+
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);
364+
}
365+
361366
if (\count($context['not_normalizable_value_exceptions'] ?? []) > 0) {
362367
return $reflectionClass->newInstanceWithoutConstructor();
363368
}

0 commit comments

Comments
 (0)