-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Handle invalid mapping type property type #60296
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
base: 7.2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -12,6 +12,7 @@ | |||||||||||
namespace Symfony\Component\Serializer\Tests\Normalizer; | ||||||||||||
|
||||||||||||
use PHPUnit\Framework\TestCase; | ||||||||||||
use stdClass; | ||||||||||||
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; | ||||||||||||
use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor; | ||||||||||||
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; | ||||||||||||
|
@@ -525,6 +526,74 @@ private function getDenormalizerForStringCollection() | |||||||||||
return $denormalizer; | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* @dataProvider provideInvalidDiscriminatorTypes | ||||||||||||
*/ | ||||||||||||
public function testDenormalizeWithDiscriminatorMapHandlesInvalidTypeValue(mixed $typeValue, bool $shouldFail) | ||||||||||||
{ | ||||||||||||
if ($shouldFail) { | ||||||||||||
$this->expectException(NotNormalizableValueException::class); | ||||||||||||
$this->expectExceptionMessage( | ||||||||||||
'The type property "type" for the abstract object "Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummy" must be a string.' | ||||||||||||
); | ||||||||||||
} | ||||||||||||
|
||||||||||||
$factory = new ClassMetadataFactory(new AttributeLoader()); | ||||||||||||
|
||||||||||||
$loaderMock = new class implements ClassMetadataFactoryInterface { | ||||||||||||
public function getMetadataFor($value): ClassMetadataInterface | ||||||||||||
{ | ||||||||||||
if (AbstractDummy::class === $value) { | ||||||||||||
return new ClassMetadata( | ||||||||||||
AbstractDummy::class, | ||||||||||||
new ClassDiscriminatorMapping('type', [ | ||||||||||||
'first' => AbstractDummyFirstChild::class, | ||||||||||||
'second' => AbstractDummySecondChild::class, | ||||||||||||
]) | ||||||||||||
); | ||||||||||||
} | ||||||||||||
|
||||||||||||
throw new InvalidArgumentException(); | ||||||||||||
} | ||||||||||||
|
||||||||||||
public function hasMetadataFor($value): bool | ||||||||||||
{ | ||||||||||||
return AbstractDummy::class === $value; | ||||||||||||
} | ||||||||||||
}; | ||||||||||||
|
||||||||||||
$discriminatorResolver = new ClassDiscriminatorFromClassMetadata($loaderMock); | ||||||||||||
$normalizer = new AbstractObjectNormalizerDummy($factory, null, new PhpDocExtractor(), $discriminatorResolver); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure you need the |
||||||||||||
$serializer = new Serializer([$normalizer]); | ||||||||||||
$normalizer->setSerializer($serializer); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed? |
||||||||||||
$normalizedData = $normalizer->denormalize(['foo' => 'foo', 'baz' => 'baz', 'quux' => ['value' => 'quux'], 'type' => $typeValue], AbstractDummy::class); | ||||||||||||
|
||||||||||||
if ($shouldFail) { | ||||||||||||
$this->fail('Expected exception not thrown.'); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not needed because of the |
||||||||||||
} | ||||||||||||
|
||||||||||||
$this->assertInstanceOf(DummyFirstChildQuux::class, $normalizedData->quux); | ||||||||||||
} | ||||||||||||
|
||||||||||||
public function provideInvalidDiscriminatorTypes() | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
{ | ||||||||||||
$toStringObject = new class() { | ||||||||||||
public function __toString() | ||||||||||||
{ | ||||||||||||
return 'first'; | ||||||||||||
} | ||||||||||||
}; | ||||||||||||
|
||||||||||||
return [ | ||||||||||||
[[], true], | ||||||||||||
[new StdClass(), true], | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
[123, true], | ||||||||||||
[false, true], | ||||||||||||
['first', false], | ||||||||||||
[$toStringObject, false], | ||||||||||||
]; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public function testDenormalizeWithDiscriminatorMapUsesCorrectClassname() | ||||||||||||
{ | ||||||||||||
$factory = new ClassMetadataFactory(new AttributeLoader()); | ||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe?