|
67 | 67 | use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor; |
68 | 68 | use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumProperty; |
69 | 69 | use Symfony\Component\Serializer\Tests\Fixtures\DummyWithObjectOrNull; |
| 70 | +use Symfony\Component\Serializer\Tests\Fixtures\DummyWithUnion; |
70 | 71 | use Symfony\Component\Serializer\Tests\Fixtures\DummyWithVariadicParameter; |
71 | 72 | use Symfony\Component\Serializer\Tests\Fixtures\FalseBuiltInDummy; |
72 | 73 | use Symfony\Component\Serializer\Tests\Fixtures\FooImplementationDummy; |
@@ -1429,6 +1430,60 @@ public function testCollectDenormalizationErrorsWithInvalidConstructorTypes() |
1429 | 1430 | $this->assertSame($expected, $exceptionsAsArray); |
1430 | 1431 | } |
1431 | 1432 |
|
| 1433 | + public function testCollectDenormalizationErrorsWithUnionConstructorTypes() |
| 1434 | + { |
| 1435 | + $json = '{}'; |
| 1436 | + |
| 1437 | + $serializer = new Serializer( |
| 1438 | + [new ObjectNormalizer()], |
| 1439 | + ['json' => new JsonEncoder()] |
| 1440 | + ); |
| 1441 | + |
| 1442 | + try { |
| 1443 | + $serializer->deserialize( |
| 1444 | + $json, |
| 1445 | + DummyWithUnion::class, |
| 1446 | + 'json', |
| 1447 | + [DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true] |
| 1448 | + ); |
| 1449 | + |
| 1450 | + $this->fail(); |
| 1451 | + } catch (\Throwable $th) { |
| 1452 | + $this->assertInstanceOf(PartialDenormalizationException::class, $th); |
| 1453 | + } |
| 1454 | + |
| 1455 | + $exceptionsAsArray = array_map(fn (NotNormalizableValueException $e): array => [ |
| 1456 | + 'currentType' => $e->getCurrentType(), |
| 1457 | + 'expectedTypes' => $e->getExpectedTypes(), |
| 1458 | + 'path' => $e->getPath(), |
| 1459 | + 'useMessageForUser' => $e->canUseMessageForUser(), |
| 1460 | + 'message' => $e->getMessage(), |
| 1461 | + ], $th->getErrors()); |
| 1462 | + |
| 1463 | + $expected = [ |
| 1464 | + [ |
| 1465 | + 'currentType' => 'null', |
| 1466 | + 'expectedTypes' => [ |
| 1467 | + 'int', 'float', |
| 1468 | + ], |
| 1469 | + 'path' => 'value', |
| 1470 | + 'useMessageForUser' => true, |
| 1471 | + 'message' => 'Failed to create object because the class misses the "value" property.', |
| 1472 | + ], |
| 1473 | + [ |
| 1474 | + 'currentType' => 'null', |
| 1475 | + 'expectedTypes' => [ |
| 1476 | + 'string', 'int', |
| 1477 | + ], |
| 1478 | + 'path' => 'value2', |
| 1479 | + 'useMessageForUser' => true, |
| 1480 | + 'message' => 'Failed to create object because the class misses the "value2" property.', |
| 1481 | + ], |
| 1482 | + ]; |
| 1483 | + |
| 1484 | + $this->assertSame($expected, $exceptionsAsArray); |
| 1485 | + } |
| 1486 | + |
1432 | 1487 | public function testCollectDenormalizationErrorsWithEnumConstructor() |
1433 | 1488 | { |
1434 | 1489 | $serializer = new Serializer( |
|
0 commit comments