Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit af7b0d1

Browse files
committed
Merge branch '6.4' into 7.3
* 6.4: do not use PHPUnit mock objects without configured expectations [Form] Fix ICU 72+ whitespace handling in `DateTimeToLocalizedStringTransformer` [Process] Adjust Process mustRun method phpdoc [Serializer] Fix NameConverter not detecting wrong input format with allow_extra_attributes=false
2 parents 6726b42 + 6556c8d commit af7b0d1

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

Normalizer/AbstractObjectNormalizer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,16 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
348348
if (isset($nestedData[$notConverted]) && !isset($originalNestedData[$attribute])) {
349349
throw new LogicException(\sprintf('Duplicate values for key "%s" found. One value is set via the SerializedPath attribute: "%s", the other one is set via the SerializedName attribute: "%s".', $notConverted, implode('->', $nestedAttributes[$notConverted]->getElements()), $attribute));
350350
}
351+
352+
if ($attribute === $notConverted
353+
&& !($context[self::ALLOW_EXTRA_ATTRIBUTES] ?? $this->defaultContext[self::ALLOW_EXTRA_ATTRIBUTES])
354+
&& (false === $allowedAttributes || \in_array($attribute, $allowedAttributes, true))
355+
&& $this->nameConverter->normalize($attribute, $resolvedClass, $format, $context) !== $attribute
356+
) {
357+
// Input was in wrong format (e.g., camelCase when snake_case expected)
358+
$extraAttributes[] = $notConverted;
359+
continue;
360+
}
351361
}
352362

353363
$attributeContext = $this->getAttributeDenormalizationContext($resolvedClass, $attribute, $context);

Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
2222
use Symfony\Component\Serializer\Attribute\Groups;
2323
use Symfony\Component\Serializer\Attribute\Ignore;
24+
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
2425
use Symfony\Component\Serializer\Exception\LogicException;
2526
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2627
use Symfony\Component\Serializer\Exception\RuntimeException;
@@ -1232,6 +1233,51 @@ public function testDiscriminatorWithAllowExtraAttributesFalse()
12321233
$this->assertInstanceOf(DiscriminatorDummyTypeA::class, $obj);
12331234
}
12341235

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+
12351281
public function testNormalizeObjectWithGroupsAndIsPrefixedProperty()
12361282
{
12371283
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
@@ -1786,3 +1832,20 @@ public function visibleGroup()
17861832
return $this->visibleGroup;
17871833
}
17881834
}
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

Comments
 (0)