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

Skip to content

Commit 2f8681b

Browse files
Merge branch '7.3' into 7.4
* 7.3: Fix merge [FrameworkBundle] Clean `http_cache` dir in `KernelTestCase::ensureKernelShutdown()` fix tests on Windows [PropertyInfo] Conflict with phpdocumentor/reflection-docblock >= 6 [Finder] Fix appending empty iterators Fix attributeLoader Bump Symfony version to 7.3.11 Update VERSION for 7.3.10 Update CHANGELOG for 7.3.10 Bump Symfony version to 6.4.33 Update VERSION for 6.4.32 Update CONTRIBUTORS for 6.4.32 Update CHANGELOG for 6.4.32 [Process] Fix escaping for MSYS on Windows
2 parents 3b9a5d5 + 0d88884 commit 2f8681b

2 files changed

Lines changed: 51 additions & 6 deletions

File tree

Mapping/Loader/AttributeLoader.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ private function doLoadClassMetadata(\ReflectionClass $reflectionClass, ClassMet
149149
$attributeName = $this->getAttributeNameFromAccessor($reflectionClass, $method, true);
150150
$accessorOrMutator = null !== $attributeName;
151151
$hasProperty = $this->hasPropertyForAccessor($method->getDeclaringClass(), $name);
152+
$attributeMetadata = null;
152153

153154
if ($hasProperty || $accessorOrMutator) {
154155
if (null === $attributeName || 's' !== $name[0] && $hasProperty && $this->hasAttributeNameCollision($reflectionClass, $attributeName, $name)) {
@@ -165,35 +166,37 @@ private function doLoadClassMetadata(\ReflectionClass $reflectionClass, ClassMet
165166

166167
foreach ($this->loadAttributes($method) as $attribute) {
167168
if ($attribute instanceof Groups) {
168-
if (!$accessorOrMutator && !$hasProperty) {
169+
if (!$attributeMetadata) {
169170
throw new MappingException(\sprintf('Groups on "%s::%s()" cannot be added. Groups can only be added on methods beginning with "get", "is", "has", "can" or "set".', $className, $method->name));
170171
}
171172

172173
foreach ($attribute->groups as $group) {
173174
$attributeMetadata->addGroup($group);
174175
}
175176
} elseif ($attribute instanceof MaxDepth) {
176-
if (!$accessorOrMutator && !$hasProperty) {
177+
if (!$attributeMetadata) {
177178
throw new MappingException(\sprintf('MaxDepth on "%s::%s()" cannot be added. MaxDepth can only be added on methods beginning with "get", "is", "has", "can" or "set".', $className, $method->name));
178179
}
179180

180181
$attributeMetadata->setMaxDepth($attribute->maxDepth);
181182
} elseif ($attribute instanceof SerializedName) {
182-
if (!$accessorOrMutator && !$hasProperty) {
183+
if (!$attributeMetadata) {
183184
throw new MappingException(\sprintf('SerializedName on "%s::%s()" cannot be added. SerializedName can only be added on methods beginning with "get", "is", "has", "can" or "set".', $className, $method->name));
184185
}
185186

186187
$attributeMetadata->setSerializedName($attribute->serializedName);
187188
} elseif ($attribute instanceof SerializedPath) {
188-
if (!$accessorOrMutator && !$hasProperty) {
189+
if (!$attributeMetadata) {
189190
throw new MappingException(\sprintf('SerializedPath on "%s::%s()" cannot be added. SerializedPath can only be added on methods beginning with "get", "is", "has", "can" or "set".', $className, $method->name));
190191
}
191192

192193
$attributeMetadata->setSerializedPath($attribute->serializedPath);
193194
} elseif ($attribute instanceof Ignore) {
194-
$attributeMetadata->setIgnore(true);
195+
if ($attributeMetadata) {
196+
$attributeMetadata->setIgnore(true);
197+
}
195198
} elseif ($attribute instanceof Context) {
196-
if (!$accessorOrMutator && !$hasProperty) {
199+
if (!$attributeMetadata) {
197200
throw new MappingException(\sprintf('Context on "%s::%s()" cannot be added. Context can only be added on methods beginning with "get", "is", "has", "can" or "set".', $className, $method->name));
198201
}
199202

Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,15 @@ public function testSkipVoidNeverReturnTypeAccessors()
13731373
$this->assertArrayNotHasKey('neverProperty', $normalized);
13741374
$this->assertEquals('value', $normalized['normalProperty']);
13751375
}
1376+
1377+
public function testMetadataIsAppliedToTheRightValue()
1378+
{
1379+
$obj = new ObjectWithMetadata();
1380+
$normalizer = new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader()));
1381+
$normalized = $normalizer->normalize($obj);
1382+
1383+
$this->assertSame(['name' => 'John', 'foo' => 42, 'hello' => 'Hello i am John'], $normalized);
1384+
}
13761385
}
13771386

13781387
class ProxyObjectDummy extends ObjectDummy
@@ -2013,3 +2022,36 @@ public function __construct(
20132022
) {
20142023
}
20152024
}
2025+
2026+
class ObjectWithMetadata
2027+
{
2028+
private int $foo;
2029+
private string $name;
2030+
2031+
public function __construct()
2032+
{
2033+
$this->foo = 42;
2034+
$this->name = 'John';
2035+
}
2036+
2037+
public function getName(): string
2038+
{
2039+
return $this->name;
2040+
}
2041+
2042+
public function getFoo(): int
2043+
{
2044+
return $this->foo;
2045+
}
2046+
2047+
#[Ignore]
2048+
public function isEqualTo(self $test): bool
2049+
{
2050+
return $this->name === $test->getName();
2051+
}
2052+
2053+
public function getHello(): string
2054+
{
2055+
return 'Hello i am '.$this->getName();
2056+
}
2057+
}

0 commit comments

Comments
 (0)