|
21 | 21 | use Symfony\Component\PropertyInfo\PropertyInfoExtractor; |
22 | 22 | use Symfony\Component\Serializer\Attribute\Groups; |
23 | 23 | use Symfony\Component\Serializer\Attribute\Ignore; |
| 24 | +use Symfony\Component\Serializer\Exception\ExtraAttributesException; |
24 | 25 | use Symfony\Component\Serializer\Exception\LogicException; |
25 | 26 | use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
26 | 27 | use Symfony\Component\Serializer\Exception\RuntimeException; |
@@ -1232,6 +1233,51 @@ public function testDiscriminatorWithAllowExtraAttributesFalse() |
1232 | 1233 | $this->assertInstanceOf(DiscriminatorDummyTypeA::class, $obj); |
1233 | 1234 | } |
1234 | 1235 |
|
| 1236 | + public function testNameConverterWithWrongCaseAndAllowExtraAttributesFalse() |
| 1237 | + { |
| 1238 | + $classMetadataFactory = new ClassMetadataFactory(new AttributeLoader()); |
| 1239 | + $normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter()); |
| 1240 | + |
| 1241 | + $result = $normalizer->denormalize( |
| 1242 | + ['some_camel_case_property' => 1], |
| 1243 | + NameConverterTestDummy::class, |
| 1244 | + null, |
| 1245 | + [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false] |
| 1246 | + ); |
| 1247 | + $this->assertSame(1, $result->someCamelCaseProperty); |
| 1248 | + |
| 1249 | + $this->expectException(ExtraAttributesException::class); |
| 1250 | + $this->expectExceptionMessage('someCamelCaseProperty'); |
| 1251 | + $normalizer->denormalize( |
| 1252 | + ['someCamelCaseProperty' => 1], |
| 1253 | + NameConverterTestDummy::class, |
| 1254 | + null, |
| 1255 | + [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false] |
| 1256 | + ); |
| 1257 | + } |
| 1258 | + |
| 1259 | + public function testNameConverterWithWrongCaseAndAllowExtraAttributesTrue() |
| 1260 | + { |
| 1261 | + $classMetadataFactory = new ClassMetadataFactory(new AttributeLoader()); |
| 1262 | + $normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter()); |
| 1263 | + |
| 1264 | + $result = $normalizer->denormalize( |
| 1265 | + ['someCamelCaseProperty' => 999], |
| 1266 | + NameConverterTestDummy::class, |
| 1267 | + null, |
| 1268 | + [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true] |
| 1269 | + ); |
| 1270 | + $this->assertSame(0, $result->someCamelCaseProperty); |
| 1271 | + |
| 1272 | + $result = $normalizer->denormalize( |
| 1273 | + ['some_camel_case_property' => 42], |
| 1274 | + NameConverterTestDummy::class, |
| 1275 | + null, |
| 1276 | + [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true] |
| 1277 | + ); |
| 1278 | + $this->assertSame(42, $result->someCamelCaseProperty); |
| 1279 | + } |
| 1280 | + |
1235 | 1281 | public function testNormalizeObjectWithGroupsAndIsPrefixedProperty() |
1236 | 1282 | { |
1237 | 1283 | $classMetadataFactory = new ClassMetadataFactory(new AttributeLoader()); |
@@ -1786,3 +1832,20 @@ public function visibleGroup() |
1786 | 1832 | return $this->visibleGroup; |
1787 | 1833 | } |
1788 | 1834 | } |
| 1835 | + |
| 1836 | +class NameConverterTestDummy |
| 1837 | +{ |
| 1838 | + public function __construct( |
| 1839 | + public readonly int $someCamelCaseProperty = 0, |
| 1840 | + ) { |
| 1841 | + } |
| 1842 | +} |
| 1843 | + |
| 1844 | +class NameConverterTestDummyMultiple |
| 1845 | +{ |
| 1846 | + public function __construct( |
| 1847 | + public readonly int $someCamelCaseProperty = 0, |
| 1848 | + public readonly int $anotherProperty = 0, |
| 1849 | + ) { |
| 1850 | + } |
| 1851 | +} |
0 commit comments