Skip to content

Commit

Permalink
feat: adding symfonty ignore attribute support
Browse files Browse the repository at this point in the history
  • Loading branch information
vuryss committed Dec 3, 2024
1 parent 824afd2 commit 4a37cbf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/Metadata/MetadataExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ public function getSerializerContext(\ReflectionProperty $reflectionProperty): S
$symfonySerializedName = null;
$symfonySerializerGroups = [];
$serializerContexts = $reflectionProperty->getAttributes(SerializerContext::class);
$isSymfonyIgnored = false;

if (count($serializerContexts) > 1) {
throw new InvalidAttributeUsageException(
Expand Down Expand Up @@ -344,6 +345,16 @@ public function getSerializerContext(\ReflectionProperty $reflectionProperty): S
}
}

if (class_exists('\Symfony\Component\Serializer\Attribute\Ignore')) {
$symfonySerializerIgnoreAttribute = $reflectionProperty->getAttributes(
\Symfony\Component\Serializer\Attribute\Ignore::class
);

if (isset($symfonySerializerIgnoreAttribute[0])) {
$isSymfonyIgnored = true;
}
}

$serializerContext = isset($serializerContexts[0]) ? $serializerContexts[0]->newInstance() : new SerializerContext();

if (null === $serializerContext->name && null !== $symfonySerializedName) {
Expand All @@ -354,6 +365,10 @@ public function getSerializerContext(\ReflectionProperty $reflectionProperty): S
$serializerContext->groups = $symfonySerializerGroups;
}

if ($isSymfonyIgnored) {
$serializerContext->ignore = true;
}

return $serializerContext;
}
}
4 changes: 4 additions & 0 deletions tests/Datasets/Symfony/SymfonyAnnotatedObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Vuryss\Serializer\Tests\Datasets\Symfony;

use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Serializer\Attribute\Ignore;
use Symfony\Component\Serializer\Attribute\SerializedName;
use Vuryss\Serializer\Attribute\SerializerContext;

Expand Down Expand Up @@ -35,5 +36,8 @@ public function __construct(
#[SerializedName('and_another_field')]
#[SerializerContext(groups: ['group1'])]
public string $andAnotherField = 'blah',

#[Ignore]
public string $ignored = 'ignored',
) {}
}
11 changes: 7 additions & 4 deletions tests/Unit/SymfonyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
});

test('Can deserialized with Symfony serializer attributes', function () {
$json = '{"some_property":51,"another_property":71,"some_field":"foobar","another_field":"barfoo","this_has_priority":"nope","and_another_field":"111"}';
$json = '{"some_property":51,"another_property":71,"some_field":"foobar","another_field":"barfoo","this_has_priority":"nope","and_another_field":"111","ignored":"some-value"}';
$serializer = new \Vuryss\Serializer\Serializer();
$person = $serializer->deserialize($json, \Vuryss\Serializer\Tests\Datasets\Symfony\SymfonyAnnotatedObject::class);

Expand All @@ -28,7 +28,8 @@
->and($person->someField)->toBe('foobar')
->and($person->anotherField)->toBe('barfoo')
->and($person->yetAnotherField)->toBe('nope')
->and($person->andAnotherField)->toBe('111');
->and($person->andAnotherField)->toBe('111')
->and($person->ignored)->toBe('ignored');

$person = $serializer->deserialize($json, \Vuryss\Serializer\Tests\Datasets\Symfony\SymfonyAnnotatedObject::class, ['groups' => ['group1']]);

Expand All @@ -37,7 +38,8 @@
->and($person->someField)->toBe('foobar')
->and($person->anotherField)->toBe('bar')
->and($person->yetAnotherField)->toBe('nope')
->and($person->andAnotherField)->toBe('111');
->and($person->andAnotherField)->toBe('111')
->and($person->ignored)->toBe('ignored');

$person = $serializer->deserialize($json, \Vuryss\Serializer\Tests\Datasets\Symfony\SymfonyAnnotatedObject::class, ['groups' => ['group2']]);

Expand All @@ -46,5 +48,6 @@
->and($person->someField)->toBe('foo')
->and($person->anotherField)->toBe('barfoo')
->and($person->yetAnotherField)->toBe('really?')
->and($person->andAnotherField)->toBe('blah');
->and($person->andAnotherField)->toBe('blah')
->and($person->ignored)->toBe('ignored');
});

0 comments on commit 4a37cbf

Please sign in to comment.