8000 [Serializer] Prevent that bad Ignore method annotations lead to incorrect results by astepin · Pull Request #46947 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Prevent that bad Ignore method annotations lead to incorrect results #46947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
8000
Failed to load files.
Loading
Diff view
Diff view
Prevent that bad Ignore method annotations lead to incorrect results
fix #45016
  • Loading branch information
astepin committed Jul 15, 2022
commit edb10386dd505679cd1cfb298429874b49a7b98b
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)

$attributeMetadata->setSerializedName($annotation->getSerializedName());
} elseif ($annotation instanceof Ignore) {
if (!$accessorOrMutator) {
throw new MappingException(sprintf('Ignore on "%s::%s()" cannot be added. Ignore can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
}

$attributeMetadata->setIgnore(true);
} elseif ($annotation instanceof Context) {
if (!$accessorOrMutator) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

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

use Symfony\Component\Serializer\Annotation\Ignore;

class Entity45016
{
/**
* @var int
*/
private $id = 1234;

public function getId(): int
{
return $this->id;
}

/**
* @Ignore()
*/
public function badIgnore(): bool
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

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

use Symfony\Component\Serializer\Annotation\Ignore;

class Entity45016
{
/**
* @var int
*/
private $id = 1234;

public function getId(): int
{
return $this->id;
}

#[Ignore]
public function badIgnore(): bool
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ public function testThrowsOnContextOnInvalidMethod()
$loader->loadClassMetadata($classMetadata);
}

public function testCanHandleUnrelatedIgnoredMethods()
{
$class = $this->getNamespace().'\Entity45016';

$this->expectException(MappingException::class);
$this->expectExceptionMessage(sprintf('Ignore on "%s::badIgnore()" cannot be added', $class));

$metadata = new ClassMetadata($class);
$loader = $this->getLoaderForContextMapping();

$loader->loadClassMetadata($metadata);
}

abstract protected function createLoader(): AnnotationLoader;

abstract protected function getNamespace(): string;
Expand Down
0