|
62 | 62 | use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor; |
63 | 63 | use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumProperty; |
64 | 64 | use Symfony\Component\Serializer\Tests\Fixtures\DummyWithObjectOrNull; |
| 65 | +use Symfony\Component\Serializer\Tests\Fixtures\DummyWithUnion; |
65 | 66 | use Symfony\Component\Serializer\Tests\Fixtures\DummyWithVariadicParameter; |
66 | 67 | use Symfony\Component\Serializer\Tests\Fixtures\FalseBuiltInDummy; |
67 | 68 | use Symfony\Component\Serializer\Tests\Fixtures\FooImplementationDummy; |
@@ -1392,6 +1393,60 @@ public function testCollectDenormalizationErrorsWithInvalidConstructorTypes() |
1392 | 1393 | $this->assertSame($expected, $exceptionsAsArray); |
1393 | 1394 | } |
1394 | 1395 |
|
| 1396 | + public function testCollectDenormalizationErrorsWithUnionConstructorTypes() |
| 1397 | + { |
| 1398 | + $json = '{}'; |
| 1399 | + |
| 1400 | + $serializer = new Serializer( |
| 1401 | + [new ObjectNormalizer()], |
| 1402 | + ['json' => new JsonEncoder()] |
| 1403 | + ); |
| 1404 | + |
| 1405 | + try { |
| 1406 | + $serializer->deserialize( |
| 1407 | + $json, |
| 1408 | + DummyWithUnion::class, |
| 1409 | + 'json', |
| 1410 | + [DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true] |
| 1411 | + ); |
| 1412 | + |
| 1413 | + $this->fail(); |
| 1414 | + } catch (\Throwable $th) { |
| 1415 | + $this->assertInstanceOf(PartialDenormalizationException::class, $th); |
| 1416 | + } |
| 1417 | + |
| 1418 | + $exceptionsAsArray = array_map(fn (NotNormalizableValueException $e): array => [ |
| 1419 | + 'currentType' => $e->getCurrentType(), |
| 1420 | + 'expectedTypes' => $e->getExpectedTypes(), |
| 1421 | + 'path' => $e->getPath(), |
| 1422 | + 'useMessageForUser' => $e->canUseMessageForUser(), |
| 1423 | + 'message' => $e->getMessage(), |
| 1424 | + ], $th->getErrors()); |
| 1425 | + |
| 1426 | + $expected = [ |
| 1427 | + [ |
| 1428 | + 'currentType' => 'null', |
| 1429 | + 'expectedTypes' => [ |
| 1430 | + 'int', 'float', |
| 1431 | + ], |
| 1432 | + 'path' => 'value', |
| 1433 | + 'useMessageForUser' => true, |
| 1434 | + 'message' => 'Failed to create object because the class misses the "value" property.', |
| 1435 | + ], |
| 1436 | + [ |
| 1437 | + 'currentType' => 'null', |
| 1438 | + 'expectedTypes' => [ |
| 1439 | + 'string', 'int', |
| 1440 | + ], |
| 1441 | + 'path' => 'value2', |
| 1442 | + 'useMessageForUser' => true, |
| 1443 | + 'message' => 'Failed to create object because the class misses the "value2" property.', |
| 1444 | + ], |
| 1445 | + ]; |
| 1446 | + |
| 1447 | + $this->assertSame($expected, $exceptionsAsArray); |
| 1448 | + } |
| 1449 | + |
1395 | 1450 | public function testCollectDenormalizationErrorsWithEnumConstructor() |
1396 | 1451 | { |
1397 | 1452 | $serializer = new Serializer( |
|
0 commit comments