Skip to content

Commit d4a097f

Browse files
committed
Merge branch '5.4' into 6.4
* 5.4: fix tests Drop test case Catch TableNotFoundException in MySQL delete [DoctrineBridge] Fix deprecation warning with ORM 3 when guessing field lengths Throw TransformationFailedException when there is a null bytes injection [Cache][Lock] Identify missing table in pgsql correctly and address failing integration tests [Serializer] Fix object normalizer when properties has the same name as their accessor
2 parents 9818cce + 1a23213 commit d4a097f

File tree

6 files changed

+238
-5
lines changed

6 files changed

+238
-5
lines changed

Mapping/Loader/AttributeLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
130130

131131
$accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);
132132
if ($accessorOrMutator) {
133-
$attributeName = lcfirst($matches[2]);
133+
$attributeName = $reflectionClass->hasProperty($method->name) ? $method->name : lcfirst($matches[2]);
134134

135135
if (isset($attributesMetadata[$attributeName])) {
136136
$attributeMetadata = $attributesMetadata[$attributeName];

Normalizer/ObjectNormalizer.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,25 @@ protected function extractAttributes(object $object, ?string $format = null, arr
9090

9191
if (str_starts_with($name, 'get') || str_starts_with($name, 'has') || str_starts_with($name, 'can')) {
9292
// getters, hassers and canners
93-
$attributeName = substr($name, 3);
93+
$attributeName = $name;
9494

9595
if (!$reflClass->hasProperty($attributeName)) {
96-
$attributeName = lcfirst($attributeName);
96+
$attributeName = substr($attributeName, 3);
97+
98+
if (!$reflClass->hasProperty($attributeName)) {
99+
$attributeName = lcfirst($attributeName);
100+
}
97101
}
98102
} elseif (str_starts_with($name, 'is')) {
99103
// issers
100-
$attributeName = substr($name, 2);
104+
$attributeName = $name;
101105

102106
if (!$reflClass->hasProperty($attributeName)) {
103-
$attributeName = lcfirst($attributeName);
107+
$attributeName = substr($attributeName, 2);
108+
109+
if (!$reflClass->hasProperty($attributeName)) {
110+
$attributeName = lcfirst($attributeName);
111+
}
104112
}
105113
}
106114

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
class SamePropertyAsMethodDummy
15+
{
16+
private $freeTrial;
17+
private $hasSubscribe;
18+
private $getReady;
19+
private $isActive;
20+
21+
public function __construct($freeTrial, $hasSubscribe, $getReady, $isActive)
22+
{
23+
$this->freeTrial = $freeTrial;
24+
$this->hasSubscribe = $hasSubscribe;
25+
$this->getReady = $getReady;
26+
$this->isActive = $isActive;
27+
}
28+
29+
public function getFreeTrial()
30+
{
31+
return $this->freeTrial;
32+
}
33+
34+
public function hasSubscribe()
35+
{
36+
return $this->hasSubscribe;
37+
}
38+
39+
public function getReady()
40+
{
41+
return $this->getReady;
42+
}
43+
44+
public function isActive()
45+
{
46+
return $this->isActive;
47+
}
48+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
use Symfony\Component\Serializer\Annotation\SerializedName;
15+
16+
class SamePropertyAsMethodWithMethodSerializedNameDummy
17+
{
18+
private $freeTrial;
19+
private $hasSubscribe;
20+
private $getReady;
21+
private $isActive;
22+
23+
public function __construct($freeTrial, $hasSubscribe, $getReady, $isActive)
24+
{
25+
$this->freeTrial = $freeTrial;
26+
$this->hasSubscribe = $hasSubscribe;
27+
$this->getReady = $getReady;
28+
$this->isActive = $isActive;
29+
}
30+
31+
/**
32+
* @SerializedName("free_trial_method")
33+
*/
34+
public function getFreeTrial()
35+
{
36+
return $this->freeTrial;
37+
}
38+
39+
/**
40+
* @SerializedName("has_subscribe_method")
41+
*/
42+
public function hasSubscribe()
43+
{
44+
return $this->hasSubscribe;
45+
}
46+
47+
/**
48+
* @SerializedName("get_ready_method")
49+
*/
50+
public function getReady()
51+
{
52+
return $this->getReady;
53+
}
54+
55+
/**
56+
* @SerializedName("is_active_method")
57+
*/
58+
public function isActive()
59+
{
60+
return $this->isActive;
61+
}
62+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
use Symfony\Component\Serializer\Annotation\SerializedName;
15+
16+
class SamePropertyAsMethodWithPropertySerializedNameDummy
17+
{
18+
/**
19+
* @SerializedName("free_trial_property")
20+
*/
21+
private $freeTrial;
22+
23+
/**
24+
* @SerializedName("has_subscribe_property")
25+
*/
26+
private $hasSubscribe;
27+
28+
/**
29+
* @SerializedName("get_ready_property")
30+
*/
31+
private $getReady;
32+
33+
/**
34+
* @SerializedName("is_active_property")
35+
*/
36+
private $isActive;
37+
38+
public function __construct($freeTrial, $hasSubscribe, $getReady, $isActive)
39+
{
40+
$this->freeTrial = $freeTrial;
41+
$this->hasSubscribe = $hasSubscribe;
42+
$this->getReady = $getReady;
43+
$this->isActive = $isActive;
44+
}
45+
46+
public function getFreeTrial()
47+
{
48+
return $this->freeTrial;
49+
}
50+
51+
public function hasSubscribe()
52+
{
53+
return $this->hasSubscribe;
54+
}
55+
56+
public function getReady()
57+
{
58+
return $this->getReady;
59+
}
60+
61+
public function isActive()
62+
{
63+
return $this->isActive;
64+
}
65+
}

Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
4343
use Symfony\Component\Serializer\Tests\Fixtures\Php74DummyPrivate;
4444
use Symfony\Component\Serializer\Tests\Fixtures\Php80Dummy;
45+
use Symfony\Component\Serializer\Tests\Fixtures\SamePropertyAsMethodDummy;
46+
use Symfony\Component\Serializer\Tests\Fixtures\SamePropertyAsMethodWithMethodSerializedNameDummy;
47+
use Symfony\Component\Serializer\Tests\Fixtures\SamePropertyAsMethodWithPropertySerializedNameDummy;
4548
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
4649
use Symfony\Component\Serializer\Tests\Normalizer\Features\AttributesTestTrait;
4750
use Symfony\Component\Serializer\Tests\Normalizer\Features\CacheableObjectAttributesTestTrait;
@@ -850,6 +853,53 @@ public function testNormalizeStdClass()
850853

851854
$this->assertSame(['baz' => 'baz'], $this->normalizer->normalize($o2));
852855
}
856+
857+
public function testSamePropertyAsMethod()
858+
{
859+
$object = new SamePropertyAsMethodDummy('free_trial', 'has_subscribe', 'get_ready', 'is_active');
860+
$expected = [
861+
'freeTrial' => 'free_trial',
862+
'hasSubscribe' => 'has_subscribe',
863+
'getReady' => 'get_ready',
864+
'isActive' => 'is_active',
865+
];
866+
867+
$this->assertSame($expected, $this->normalizer->normalize($object));
868+
}
869+
870+
public function testSamePropertyAsMethodWithPropertySerializedName()
871+
{
872+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
873+
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
874+
$this->normalizer->setSerializer($this->serializer);
875+
876+
$object = new SamePropertyAsMethodWithPropertySerializedNameDummy('free_trial', 'has_subscribe', 'get_ready', 'is_active');
877+
$expected = [
878+
'free_trial_property' => 'free_trial',
879+
'has_subscribe_property' => 'has_subscribe',
880+
'get_ready_property' => 'get_ready',
881+
'is_active_property' => 'is_active',
882+
];
883+
884+
$this->assertSame($expected, $this->normalizer->normalize($object));
885+
}
886+
887+
public function testSamePropertyAsMethodWithMethodSerializedName()
888+
{
889+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
890+
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
891+
$this->normalizer->setSerializer($this->serializer);
892+
893+
$object = new SamePropertyAsMethodWithMethodSerializedNameDummy('free_trial', 'has_subscribe', 'get_ready', 'is_active');
894+
$expected = [
895+
'free_trial_method' => 'free_trial',
896+
'has_subscribe_method' => 'has_subscribe',
897+
'get_ready_method' => 'get_ready',
898+
'is_active_method' => 'is_active',
899+
];
900+
901+
$this->assertSame($expected, $this->normalizer->normalize($object));
902+
}
853903
}
854904

855905
class ProxyObjectDummy extends ObjectDummy

0 commit comments

Comments
 (0)