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

Appearance settings

[Serializer] Fix collect_denormalization_errors flag in defaultContext #60413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 6.4
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
throw new NotNormalizableValueException(sprintf('Could not denormalize object of type "%s", no supporting normalizer found.', $type));
}

if (isset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS]) || isset($this->defaultContext[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS])) {
if ((isset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS]) || isset($this->defaultContext[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS])) && !isset($context['not_normalizable_value_exceptions'])) {
unset($context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS]);
$context['not_normalizable_value_exceptions'] = [];
$errors = &$context['not_normalizable_value_exceptions'];
Expand Down
57 changes: 57 additions & 0 deletions 57 src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,54 @@ public function testCollectDenormalizationErrorsDefaultContext()

$serializer->denormalize($data, DummyWithVariadicParameter::class);
}

public function testDenormalizationFailsWithMultipleErrorsInDefaultContext()
{
$serializer = new Serializer(
[new DateTimeNormalizer(), new ObjectNormalizer()],
[],
[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true]
);

$data = ['date' => '', 'unknown' => null];

try {
$serializer->denormalize($data, DummyEntityWithStringAndDateTime::class);
$this->fail('Expected PartialDenormalizationException was not thrown');
} catch (PartialDenormalizationException $e) {
$this->assertIsArray($e->getErrors());
$this->assertCount(2, $e->getErrors(), 'Expected two denormalization errors');

$exceptionsAsArray = array_map(function (NotNormalizableValueException $ex): array {
return [
'currentType' => $ex->getCurrentType(),
'expectedTypes' => $ex->getExpectedTypes(),
'path' => $ex->getPath(),
'useMessageForUser' => $ex->canUseMessageForUser(),
'message' => $ex->getMessage(),
];
}, $e->getErrors());

$expected = [
[
'currentType' => 'null',
'expectedTypes' => ['string'],
'path' => 'bar',
'useMessageForUser' => true,
'message' => 'Failed to create object because the class misses the "bar" property.',
],
[
'currentType' => 'string',
'expectedTypes' => ['string'],
'path' => 'date',
'useMessageForUser' => true,
'message' => 'The data is either not an string, an empty string, or null; you should pass a string that can be parsed with the passed format or a valid DateTime string.',
],
];

$this->assertSame($expected, $exceptionsAsArray);
}
}
}

class Model
Expand Down Expand Up @@ -1743,6 +1791,15 @@ public function __construct($value)
}
}

class DummyEntityWithStringAndDateTime
{
public function __construct(
public string $bar,
public \DateTimeInterface $date,
) {
}
}

class DummyUnionType
{
/**
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.