Skip to content
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
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ public function getProperties(string $class, array $context = []): ?array
return null;
}

$groups = $context['serializer_groups'] ?? [];
$groupsHasBeenDefined = [] !== $groups;
$groups = array_merge($groups, ['Default', (false !== $nsSep = strrpos($class, '\\')) ? substr($class, $nsSep + 1) : $class]);

$properties = [];
$serializerClassMetadata = $this->classMetadataFactory->getMetadataFor($class);

foreach ($serializerClassMetadata->getAttributesMetadata() as $serializerAttributeMetadata) {
if (!$serializerAttributeMetadata->isIgnored() && (null === $context['serializer_groups'] || array_intersect($context['serializer_groups'], $serializerAttributeMetadata->getGroups()))) {
if (!$serializerAttributeMetadata->isIgnored() && (!$groupsHasBeenDefined || array_intersect(array_merge($serializerAttributeMetadata->getGroups(), ['*']), $groups))) {
$properties[] = $serializerAttributeMetadata->getName();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function testGetProperties()
public function testGetPropertiesWithIgnoredProperties()
{
$this->assertSame(['visibleProperty'], $this->extractor->getProperties(IgnorePropertyDummy::class, ['serializer_groups' => ['a']]));
$this->assertSame(['visibleProperty'], $this->extractor->getProperties(IgnorePropertyDummy::class, ['serializer_groups' => ['Default']]));
$this->assertSame(['visibleProperty'], $this->extractor->getProperties(IgnorePropertyDummy::class, ['serializer_groups' => ['IgnorePropertyDummy']]));
}

public function testGetPropertiesWithAnyGroup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
class IgnorePropertyDummy
{
#[Groups(['a'])]
#[Groups(['a', 'Default', 'IgnorePropertyDummy'])]
public $visibleProperty;

#[Groups(['a']), Ignore]
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `DateTimeNormalizer::CAST_KEY` context option
* Add `Default` and "class name" default groups

7.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,16 @@ private function getCacheValueForAttributesMetadata(string $class, array $contex
}

$metadataGroups = $metadata->getGroups();

$contextGroups = (array) ($context[AbstractNormalizer::GROUPS] ?? []);
$contextGroupsHasBeenDefined = [] !== $contextGroups;
$contextGroups = array_merge($contextGroups, ['Default', (false !== $nsSep = strrpos($class, '\\')) ? substr($class, $nsSep + 1) : $class]);

if ($contextGroups && !$metadataGroups) {
if ($contextGroupsHasBeenDefined && !$metadataGroups) {
continue;
}

if ($metadataGroups && !array_intersect($metadataGroups, $contextGroups) && !\in_array('*', $contextGroups, true)) {
if ($metadataGroups && !array_intersect(array_merge($metadataGroups, ['*']), $contextGroups)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,26 +213,32 @@ protected function getAllowedAttributes(string|object $classOrObject, array $con
return false;
}

$classMetadata = $this->classMetadataFactory->getMetadataFor($classOrObject);
$class = $classMetadata->getName();

$groups = $this->getGroups($context);
$groupsHasBeenDefined = [] !== $groups;
$groups = array_merge($groups, ['Default', (false !== $nsSep = strrpos($class, '\\')) ? substr($class, $nsSep + 1) : $class]);

$allowedAttributes = [];
$ignoreUsed = false;
foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {

foreach ($classMetadata->getAttributesMetadata() as $attributeMetadata) {
if ($ignore = $attributeMetadata->isIgnored()) {
$ignoreUsed = true;
}

// If you update this check, update accordingly the one in Symfony\Component\PropertyInfo\Extractor\SerializerExtractor::getProperties()
if (
!$ignore
&& ([] === $groups || array_intersect(array_merge($attributeMetadata->getGroups(), ['*']), $groups))
&& (!$groupsHasBeenDefined || array_intersect(array_merge($attributeMetadata->getGroups(), ['*']), $groups))
&& $this->isAllowedAttribute($classOrObject, $name = $attributeMetadata->getName(), null, $context)
) {
$allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata;
}
}

if (!$ignoreUsed && [] === $groups && $allowExtraAttributes) {
if (!$ignoreUsed && !$groupsHasBeenDefined && $allowExtraAttributes) {
// Backward Compatibility with the code using this method written before the introduction of @Ignore
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class GroupDummy extends GroupDummyParent implements GroupDummyInterface
protected $quux;
private $fooBar;
private $symfony;
#[Groups(['Default'])]
private $default;
#[Groups(['GroupDummy'])]
private $className;

#[Groups(['b'])]
public function setBar($bar)
Expand Down Expand Up @@ -80,4 +84,24 @@ public function setQuux($quux): void
{
$this->quux = $quux;
}

public function setDefault($default)
{
$this->default = $default;
}

public function getDefault()
{
return $this->default;
}

public function setClassName($className)
{
$this->className = $className;
}

public function getClassName()
{
return $this->className;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public static function createClassMetadata(string $namespace, bool $withParent =
$symfony->addGroup('name_converter');
}

$default = new AttributeMetadata('default');
$default->addGroup('Default');
$expected->addAttributeMetadata($default);

$className = new AttributeMetadata('className');
$className->addGroup('GroupDummy');
$expected->addAttributeMetadata($className);

// load reflection class so that the comparison passes
$expected->getReflectionClass();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ public function testGroupsNormalize()
$obj = new GroupDummy();
$obj->setFoo('foo');
$obj->setBar('bar');
$obj->setQuux('quux');
$obj->setFooBar('fooBar');
$obj->setSymfony('symfony');
$obj->setKevin('kevin');
$obj->setCoopTilleuls('coopTilleuls');
$obj->setDefault('default');
$obj->setClassName('className');

$this->assertEquals([
'bar' => 'bar',
'default' => 'default',
'className' => 'className',
], $normalizer->normalize($obj, null, ['groups' => ['c']]));

$this->assertEquals([
Expand All @@ -47,35 +52,76 @@ public function testGroupsNormalize()
'bar' => 'bar',
'kevin' => 'kevin',
'coopTilleuls' => 'coopTilleuls',
'default' => 'default',
'className' => 'className',
], $normalizer->normalize($obj, null, ['groups' => ['a', 'c']]));

$this->assertEquals([
'default' => 'default',
'className' => 'className',
], $normalizer->normalize($obj, null, ['groups' => ['unknown']]));

$this->assertEquals([
'quux' => 'quux',
'symfony' => 'symfony',
'foo' => 'foo',
'fooBar' => 'fooBar',
'bar' => 'bar',
'kevin' => 'kevin',
'coopTilleuls' => 'coopTilleuls',
'default' => 'default',
'className' => 'className',
], $normalizer->normalize($obj));
}

public function testGroupsDenormalize()
{
$normalizer = $this->getDenormalizerForGroups();

$obj = new GroupDummy();
$obj->setFoo('foo');
$obj->setDefault('default');
$obj->setClassName('className');

$data = ['foo' => 'foo', 'bar' => 'bar'];
$data = [
'foo' => 'foo',
'bar' => 'bar',
'quux' => 'quux',
'default' => 'default',
'className' => 'className',
];

$normalized = $normalizer->denormalize(
$denormalized = $normalizer->denormalize(
$data,
GroupDummy::class,
null,
['groups' => ['unknown']]
);
$this->assertEquals($obj, $denormalized);

$obj->setFoo('foo');

$denormalized = $normalizer->denormalize(
$data,
GroupDummy::class,
null,
['groups' => ['a']]
);
$this->assertEquals($obj, $normalized);
$this->assertEquals($obj, $denormalized);

$obj->setBar('bar');

$normalized = $normalizer->denormalize(
$denormalized = $normalizer->denormalize(
$data,
GroupDummy::class,
null,
['groups' => ['a', 'b']]
);
$this->assertEquals($obj, $normalized);
$this->assertEquals($obj, $denormalized);

$obj->setQuux('quux');

$denormalized = $normalizer->denormalize($data, GroupDummy::class);
$this->assertEquals($obj, $denormalized);
}

public function testNormalizeNoPropertyInGroup()
Expand All @@ -84,7 +130,12 @@ public function testNormalizeNoPropertyInGroup()

$obj = new GroupDummy();
$obj->setFoo('foo');
$obj->setDefault('default');
$obj->setClassName('className');

$this->assertEquals([], $normalizer->normalize($obj, null, ['groups' => ['notExist']]));
$this->assertEquals([
'default' => 'default',
'className' => 'className',
], $normalizer->normalize($obj, null, ['groups' => ['notExist']]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ public function testGroupsNormalizeWithNameConverter()
'bar' => null,
'foo_bar' => '@dunglas',
'symfony' => '@coopTilleuls',
'default' => null,
'class_name' => null,
],
$this->normalizer->normalize($obj, null, [GetSetMethodNormalizer::GROUPS => ['name_converter']])
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ public function testGroupsNormalizeWithNameConverter()
'bar' => null,
'foo_bar' => '@dunglas',
'symfony' => '@coopTilleuls',
'default' => null,
'class_name' => null,
],
$this->normalizer->normalize($obj, null, [ObjectNormalizer::GROUPS => ['name_converter']])
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,18 @@ public function testNormalizeWithParentClass()
$group->setKevin('Kevin');
$group->setCoopTilleuls('coop');
$this->assertEquals(
['foo' => 'foo', 'bar' => 'bar', 'quux' => 'quux', 'kevin' => 'Kevin', 'coopTilleuls' => 'coop', 'fooBar' => null, 'symfony' => null, 'baz' => 'baz'],
[
'foo' => 'foo',
'bar' => 'bar',
'quux' => 'quux',
'kevin' => 'Kevin',
'coopTilleuls' => 'coop',
'fooBar' => null,
'symfony' => null,
'baz' => 'baz',
'default' => null,
'className' => null,
],
$this->normalizer->normalize($group, 'any')
);
}
Expand Down Expand Up @@ -303,6 +314,8 @@ public function testGroupsNormalizeWithNameConverter()
'bar' => null,
'foo_bar' => '@dunglas',
'symfony' => '@coopTilleuls',
'default' => null,
'class_name' => null,
],
$this->normalizer->normalize($obj, null, [PropertyNormalizer::GROUPS => ['name_converter']])
);
Expand Down