Skip to content

Commit

Permalink
Merge branch '6.4' into 7.1
Browse files Browse the repository at this point in the history
* 6.4:
  [AssetMapper] Fix `JavaScriptImportPathCompiler` regex for non-latin characters
  Definition::$class may not be class-string
  require Cache component versions compatible with Redis 6.1
  [Twitter][Notifier] Fix post INIT upload
  [Messenger][RateLimiter] fix additional message handled when using a rate limiter
  [Serializer] fixed object normalizer for a class with `cancel` method
  Fix bucket size reduce when previously created with bigger size
  • Loading branch information
xabbuh committed Nov 9, 2024
2 parents 763e313 + 9d862d6 commit 6066de1
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Mapping/Loader/AttributeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
}

$accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);
if ($accessorOrMutator) {
if ($accessorOrMutator && !ctype_lower($matches[2][0])) {
$attributeName = lcfirst($matches[2]);

if (isset($attributesMetadata[$attributeName])) {
Expand Down
8 changes: 5 additions & 3 deletions Normalizer/GetSetMethodNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ private function isGetMethod(\ReflectionMethod $method): bool
return !$method->isStatic()
&& !($method->getAttributes(Ignore::class) || $method->getAttributes(LegacyIgnore::class))
&& !$method->getNumberOfRequiredParameters()
&& ((2 < ($methodLength = \strlen($method->name)) && str_starts_with($method->name, 'is'))
|| (3 < $methodLength && (str_starts_with($method->name, 'has') || str_starts_with($method->name, 'get')))
&& ((2 < ($methodLength = \strlen($method->name)) && str_starts_with($method->name, 'is') && !ctype_lower($method->name[2]))
|| (3 < $methodLength && (str_starts_with($method->name, 'has') || str_starts_with($method->name, 'get')) && !ctype_lower($method->name[3]))
);
}

Expand All @@ -100,7 +100,9 @@ private function isSetMethod(\ReflectionMethod $method): bool
return !$method->isStatic()
&& !$method->getAttributes(Ignore::class)
&& 0 < $method->getNumberOfParameters()
&& str_starts_with($method->name, 'set');
&& str_starts_with($method->name, 'set')
&& !ctype_lower($method->name[3])
;
}

protected function extractAttributes(object $object, ?string $format = null, array $context = []): array
Expand Down
5 changes: 3 additions & 2 deletions Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ protected function extractAttributes(object $object, ?string $format = null, arr
$name = $reflMethod->name;
$attributeName = null;

if (3 < \strlen($name) && match ($name[0]) {
// ctype_lower check to find out if method looks like accessor but actually is not, e.g. hash, cancel
if (3 < \strlen($name) && !ctype_lower($name[3]) && match ($name[0]) {
'g' => str_starts_with($name, 'get'),
'h' => str_starts_with($name, 'has'),
'c' => str_starts_with($name, 'can'),
Expand All @@ -100,7 +101,7 @@ protected function extractAttributes(object $object, ?string $format = null, arr
if (!$reflClass->hasProperty($attributeName)) {
$attributeName = lcfirst($attributeName);
}
} elseif ('is' !== $name && str_starts_with($name, 'is')) {
} elseif ('is' !== $name && str_starts_with($name, 'is') && !ctype_lower($name[2])) {
// issers
$attributeName = substr($name, 2);

Expand Down
39 changes: 39 additions & 0 deletions Tests/Fixtures/Attributes/AccessorishGetters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes;

class AccessorishGetters
{
public function hash(): void
{
}

public function cancel()
{
}

public function getField1()
{
}

public function isField2()
{
}

public function hasField3()
{
}

public function setField4()
{
}
}
17 changes: 17 additions & 0 deletions Tests/Mapping/Loader/AttributeLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummyFirstChild;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummySecondChild;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummyThirdChild;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AccessorishGetters;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\BadAttributeDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\BadMethodContextDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\ContextDummyParent;
Expand Down Expand Up @@ -229,6 +230,22 @@ public function testLoadWithInvalidAttribute()
$this->loader->loadClassMetadata($classMetadata);
}

public function testIgnoresAccessorishGetters()
{
$classMetadata = new ClassMetadata(AccessorishGetters::class);
$this->loader->loadClassMetadata($classMetadata);

$attributesMetadata = $classMetadata->getAttributesMetadata();

self::assertCount(4, $classMetadata->getAttributesMetadata());

self::assertArrayHasKey('field1', $attributesMetadata);
self::assertArrayHasKey('field2', $attributesMetadata);
self::assertArrayHasKey('field3', $attributesMetadata);
self::assertArrayHasKey('field4', $attributesMetadata);
self::assertArrayNotHasKey('h', $attributesMetadata);
}

protected function getLoaderForContextMapping(): AttributeLoader
{
return $this->loader;
Expand Down
51 changes: 51 additions & 0 deletions Tests/Normalizer/GetSetMethodNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,14 @@ public function testNormalizeWithDiscriminator()
$this->assertSame(['type' => 'one', 'url' => 'URL_ONE'], $normalizer->normalize(new GetSetMethodDiscriminatedDummyOne()));
}

public function testNormalizeWithMethodNamesSimilarToAccessors()
{
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$normalizer = new GetSetMethodNormalizer($classMetadataFactory);

$this->assertSame(['class' => 'class', 123 => 123], $normalizer->normalize(new GetSetWithAccessorishMethod()));
}

public function testDenormalizeWithDiscriminator()
{
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
Expand Down Expand Up @@ -908,3 +916,46 @@ public function setBar($bar = null, $other = true)
$this->bar = $bar;
}
}

class GetSetWithAccessorishMethod
{
public function cancel()
{
return 'cancel';
}

public function hash()
{
return 'hash';
}

public function getClass()
{
return 'class';
}

public function setClass()
{
}

public function get123()
{
return 123;
}

public function set123()
{
}

public function gettings()
{
}

public function settings()
{
}

public function isolate()
{
}
}
78 changes: 78 additions & 0 deletions Tests/Normalizer/ObjectNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,24 @@ public function testObjectNormalizerWithAttributeLoaderAndObjectHasStaticPropert
$normalizer = new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader()));
$this->assertSame([], $normalizer->normalize($class));
}

public function testNormalizeWithMethodNamesSimilarToAccessors()
{
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$normalizer = new ObjectNormalizer($classMetadataFactory);

$object = new ObjectWithAccessorishMethods();
$normalized = $normalizer->normalize($object);

$this->assertFalse($object->isAccessorishCalled());
$this->assertSame([
'accessorishCalled' => false,
'tell' => true,
'class' => true,
'responsibility' => true,
123 => 321
], $normalized);
}
}

class ProxyObjectDummy extends ObjectDummy
Expand Down Expand Up @@ -1243,3 +1261,63 @@ class ObjectDummyWithIgnoreAttributeAndPrivateProperty

private $private = 'private';
}

class ObjectWithAccessorishMethods
{
private $accessorishCalled = false;

public function isAccessorishCalled()
{
return $this->accessorishCalled;
}

public function cancel()
{
$this->accessorishCalled = true;
}

public function hash()
{
$this->accessorishCalled = true;
}

public function canTell()
{
return true;
}

public function getClass()
{
return true;
}

public function hasResponsibility()
{
return true;
}

public function get_foo()
{
return 'bar';
}

public function get123()
{
return 321;
}

public function gettings()
{
$this->accessorishCalled = true;
}

public function settings()
{
$this->accessorishCalled = true;
}

public function isolate()
{
$this->accessorishCalled = true;
}
}

0 comments on commit 6066de1

Please sign in to comment.