Skip to content

Commit

Permalink
Merge pull request #93 from jolicode/fix/override
Browse files Browse the repository at this point in the history
fix(serializer): fix map to overriding ignore / groups attribute from serializer
  • Loading branch information
joelwurtz committed Mar 22, 2024
2 parents 2b05ba1 + 8c2bb50 commit 1e8bf2b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/EventListener/Symfony/SerializerIgnoreListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public function __construct(

public function __invoke(PropertyMetadataEvent $event): void
{
if ($event->ignored !== null) {
return;
}

if (($event->mapperMetadata->source !== 'array' && $event->mapperMetadata->source !== \stdClass::class) && $this->isIgnoreProperty($event->mapperMetadata->source, $event->source->name)) {
$event->ignored = true;

Expand Down
8 changes: 6 additions & 2 deletions src/Generator/PropertyConditionsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ public function generate(GeneratorMetadata $metadata, PropertyMetadata $property

if (!$propertyMetadata->disableGroupsCheck) {
$conditions[] = $this->groupsCheck($metadata->variableRegistry, $propertyMetadata->groups); // Property groups
$conditions[] = $this->groupsCheck($metadata->variableRegistry, $propertyMetadata->source->groups); // Source groups
$conditions[] = $this->groupsCheck($metadata->variableRegistry, $propertyMetadata->target->groups); // Target groups

if ($propertyMetadata->groups === null) {
$conditions[] = $this->groupsCheck($metadata->variableRegistry, $propertyMetadata->source->groups); // Source groups
$conditions[] = $this->groupsCheck($metadata->variableRegistry, $propertyMetadata->target->groups); // Target groups
}

$conditions[] = $this->noGroupsCheck($metadata, $propertyMetadata);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use AutoMapper\Tests\Fixtures\Dog;
use AutoMapper\Tests\Fixtures\Fish;
use AutoMapper\Tests\Fixtures\FooGenerator;
use AutoMapper\Tests\Fixtures\GroupOverride;
use AutoMapper\Tests\Fixtures\HasDateTime;
use AutoMapper\Tests\Fixtures\HasDateTimeImmutable;
use AutoMapper\Tests\Fixtures\HasDateTimeImmutableWithNullValue;
Expand Down Expand Up @@ -1448,4 +1449,14 @@ public function testDiscriminantToArray(): void
self::assertSame('dog', $petOwnerData['pets'][0]['type']);
self::assertSame('Wouf', $petOwnerData['pets'][0]['bark']);
}

public function testGroupOverride(): void
{
$this->buildAutoMapper(mapPrivatePropertiesAndMethod: true);

$group = new GroupOverride();
$data = $this->autoMapper->map($group, 'array', ['groups' => ['group2']]);

self::assertSame(['id' => 'id', 'name' => 'name'], $data);
}
}
20 changes: 20 additions & 0 deletions tests/Fixtures/GroupOverride.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace AutoMapper\Tests\Fixtures;

use AutoMapper\Attribute\MapTo;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\Ignore;

class GroupOverride
{
#[Groups(['group1'])]
#[MapTo(groups: ['group2'])]
public string $id = 'id';

#[Ignore]
#[MapTo(ignore: false, groups: ['group2'])]
public string $name = 'name';
}

0 comments on commit 1e8bf2b

Please sign in to comment.