8000 bug #58226 [Serializer] Fix for method named `get()` (mihai-stancu) · symfony/symfony@c49d6db · GitHub
[go: up one dir, main page]

Skip to content

Commit c49d6db

Browse files
bug #58226 [Serializer] Fix for method named get() (mihai-stancu)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Serializer] Fix for method named `get()` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #58012 | License | MIT During normalization, if a class has a method called `get` that method will get confused for an accessor and it will go through logic relating to property-access for the property named `substr('get', 3)`. Eventually a new PropertyPath instance will be created with an empty path which will trigger an exception. ```php $serializer->serialize(new class { public function get() {} }, 'json'); ``` Commits ------- a0d6e26 [Serializer] Fix for method named `get()`
2 parents 28b1709 + a0d6e26 commit c49d6db

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,19 @@ protected function extractAttributes(object $object, ?string $format = null, arr
100100
$name = $reflMethod->name;
101101
$attributeName = null;
102102

103-
if (str_starts_with($name, 'get') || str_starts_with($name, 'has') || str_starts_with($name, 'can')) {
103+
if (3 < \strlen($name) && match ($name[0]) {
104+
'g' => str_starts_with($name, 'get'),
105+
'h' => str_starts_with($name, 'has'),
106+
'c' => str_starts_with($name, 'can'),
107+
default => false,
108+
}) {
104109
// getters, hassers and canners
105110
$attributeName = substr($name, 3);
106111

107112
if (!$reflClass->hasProperty($attributeName)) {
108113
$attributeName = lcfirst($attributeName);
109114
}
110-
} elseif (str_starts_with($name, 'is')) {
115+
} elseif ('is' !== $name && str_starts_with($name, 'is')) {
111116
// issers
112117
$attributeName = substr($name, 2);
113118

0 commit comments

Comments
 (0)
0