Skip to content

Commit

Permalink
[Serializer] Revert #54488 to fix BC Break
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilPeyssard committed Apr 17, 2024
1 parent a1eed5a commit 5bfb9be
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 238 deletions.
2 changes: 1 addition & 1 deletion Mapping/Loader/AnnotationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)

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

if (isset($attributesMetadata[$attributeName])) {
$attributeMetadata = $attributesMetadata[$attributeName];
Expand Down
16 changes: 4 additions & 12 deletions Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,17 @@ protected function extractAttributes(object $object, ?string $format = null, arr

if (str_starts_with($name, 'get') || str_starts_with($name, 'has')) {
// getters and hassers
$attributeName = $name;
$attributeName = substr($name, 3);

if (!$reflClass->hasProperty($attributeName)) {
$attributeName = substr($attributeName, 3);

if (!$reflClass->hasProperty($attributeName)) {
$attributeName = lcfirst($attributeName);
}
$attributeName = lcfirst($attributeName);
}
} elseif (str_starts_with($name, 'is')) {
// issers
$attributeName = $name;
$attributeName = substr($name, 2);

if (!$reflClass->hasProperty($attributeName)) {
$attributeName = substr($attributeName, 2);

if (!$reflClass->hasProperty($attributeName)) {
$attributeName = lcfirst($attributeName);
}
$attributeName = lcfirst($attributeName);
}
}

Expand Down
48 changes: 0 additions & 48 deletions Tests/Fixtures/SamePropertyAsMethodDummy.php

This file was deleted.

This file was deleted.

This file was deleted.

50 changes: 0 additions & 50 deletions Tests/Normalizer/ObjectNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
use Symfony\Component\Serializer\Tests\Fixtures\Php74Dummy;
use Symfony\Component\Serializer\Tests\Fixtures\Php74DummyPrivate;
use Symfony\Component\Serializer\Tests\Fixtures\Php80Dummy;
use Symfony\Component\Serializer\Tests\Fixtures\SamePropertyAsMethodDummy;
use Symfony\Component\Serializer\Tests\Fixtures\SamePropertyAsMethodWithMethodSerializedNameDummy;
use Symfony\Component\Serializer\Tests\Fixtures\SamePropertyAsMethodWithPropertySerializedNameDummy;
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
use Symfony\Component\Serializer\Tests\Normalizer\Features\AttributesTestTrait;
use Symfony\Component\Serializer\Tests\Normalizer\Features\CacheableObjectAttributesTestTrait;
Expand Down Expand Up @@ -874,53 +871,6 @@ public function testNormalizeStdClass()
$this->assertSame(['baz' => 'baz'], $this->normalizer->normalize($o2));
}

public function testSamePropertyAsMethod()
{
$object = new SamePropertyAsMethodDummy('free_trial', 'has_subscribe', 'get_ready', 'is_active');
$expected = [
'freeTrial' => 'free_trial',
'hasSubscribe' => 'has_subscribe',
'getReady' => 'get_ready',
'isActive' => 'is_active',
];

$this->assertSame($expected, $this->normalizer->normalize($object));
}

public function testSamePropertyAsMethodWithPropertySerializedName()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
$this->normalizer->setSerializer($this->serializer);

$object = new SamePropertyAsMethodWithPropertySerializedNameDummy('free_trial', 'has_subscribe', 'get_ready', 'is_active');
$expected = [
'free_trial_property' => 'free_trial',
'has_subscribe_property' => 'has_subscribe',
'get_ready_property' => 'get_ready',
'is_active_property' => 'is_active',
];

$this->assertSame($expected, $this->normalizer->normalize($object));
}

public function testSamePropertyAsMethodWithMethodSerializedName()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
$this->normalizer->setSerializer($this->serializer);

$object = new SamePropertyAsMethodWithMethodSerializedNameDummy('free_trial', 'has_subscribe', 'get_ready', 'is_active');
$expected = [
'free_trial_method' => 'free_trial',
'has_subscribe_method' => 'has_subscribe',
'get_ready_method' => 'get_ready',
'is_active_method' => 'is_active',
];

$this->assertSame($expected, $this->normalizer->normalize($object));
}

public function testNormalizeWithIgnoreAnnotationAndPrivateProperties()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
Expand Down

0 comments on commit 5bfb9be

Please sign in to comment.