8000 [Serializer] backed enum throw notNormalizableValueException outside construct method by alli83 · Pull Request #50192 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] backed enum throw notNormalizableValueException outside construct method #50192

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex

$constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
if ($constructor) {
$context['has_constructor'] = true;
if (true !== $constructor->isPublic()) {
return $reflectionClass->newInstanceWithoutConstructor();
}
Expand Down Expand Up @@ -431,6 +432,8 @@ protected function instantiateObject(array &$data, string $class, array &$contex
}
}

unset($context['has_constructor']);

return new $class();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public function denormalize($data, string $type, string $format = null, array $c
try {
return $type::from($data);
} catch (\ValueError $e) {
throw new InvalidArgumentException('The data must belong to a backed enumeration of type '.$type);
if (isset($context['has_constructor'])) {
throw new InvalidArgumentException('The data must belong to a backed enumeration of type '.$type);
}

throw NotNormalizableValueException::createForUnexpectedDataType('The data must belong to a backed enumeration of type '.$type, $data, [$type], $context['deserialization_path'] ?? null, true, 0, $e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\Serializer\Tests\Fixtures;

use Symfony\Component\Serializer\Tests\Fixtures\StringBackedEnumDummy;

class DummyObjectWithEnumProperty
{
public StringBackedEnumDummy $get;
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function testDenormalizeObjectThrowsException()
*/
public function testDenormalizeBadBackingValueThrowsException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(NotNormalizableValueException::class);
$this->expectExceptionMessage('The data must belong to a backed enumeration of type '.StringBackedEnumDummy::class);

$this->normalizer->denormalize('POST', StringBackedEnumDummy::class);
Expand Down
49 changes: 47 additions & 2 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne;
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo;
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor;
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumProperty;
use Symfony\Component\Serializer\Tests\Fixtures\FalseBuiltInDummy;
use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Php74Full;
Expand Down Expand Up @@ -1230,7 +1231,51 @@ public function testCollectDenormalizationErrorsWithEnumConstructor()
/**
* @requires PHP 8.1
*/
public function testNoCollectDenormalizationErrorsWithWrongEnum()
public function testCollectDenormalizationErrorsWithWrongPropertyWithoutConstruct()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader());
$reflectionExtractor = new ReflectionExtractor();
$propertyInfoExtractor = new PropertyInfoExtractor([], [$reflectionExtractor], [], [], []);

$serializer = new Serializer(
[
new BackedEnumNormalizer(),
new ObjectNormalizer($classMetadataFactory, null, null, $propertyInfoExtractor),
],
['json' => new JsonEncoder()]
);

try {
$serializer->deserialize('{"get": "POST"}', DummyObjectWithEnumProperty::class, 'json', [
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
]);
} catch (\Throwable $e) {
$this->assertInstanceOf(PartialDenormalizationException::class, $e);
}

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

$expected = [
[
'currentType' => 'string',
'useMessageForUser' => true,
'message' => 'The data must belong to a backed enumeration of type Symfony\Component\Serializer\Tests\Fixtures\StringBackedEnumDummy',
],
];

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

/**
* @requires PHP 8.1
*/
public function testNoCollectDenormalizationErrorsWithWrongEnumOnConstructor()
{
$serializer = new Serializer(
[
Expand All @@ -1241,7 +1286,7 @@ public function testNoCollectDenormalizationErrorsWithWrongEnum()
);

try {
$serializer->deserialize('{"get": "invalid"}', DummyObjectWithEnumConstructor::class, 'json', [
$serializer->deserialize('{"get": "POST"}', DummyObjectWithEnumConstructor::class, 'json', [
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
]);
} catch (\Throwable $th) {
Expand Down
0