8000 [Serializer] Add a MaxDepth option by dunglas · Pull Request #17113 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Add a MaxDepth option #17113

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

Closed
wants to merge 8 commits into from
Closed
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
48 changes: 48 additions & 0 deletions src/Symfony/Component/Serializer/Annotation/MaxDepth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Annotation;

use Symfony\Component\Serializer\Exception\InvalidArgumentException;

/**
* Annotation class for @MaxDepth().
*
* @Annotation
* @Target({"PROPERTY", "METHOD"})
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class MaxDepth
{
/**
* @var int
*/
private $maxDepth;

public function __construct(array $data)
{
if (!isset($data['value']) || !$data['value']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's about if (empty($data['value'])) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, don't use empty.

throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', get_class($this)));
}

if (!is_int($data['value']) || $data['value'] <= 0) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a positive integer.', get_class($this)));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about also checking that the value is greater than zero?


$this->maxDepth = $data['value'];
}

public function getMaxDepth()
{
return $this->maxDepth;
}
}
32 changes: 31 additions & 1 deletion src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class AttributeMetadata implements AttributeMetadataInterface
*/
public $groups = array();

/**
* @var int|null
*
* @internal This property is public in order to reduce the size of the
* class' serialized representation. Do not access it. Use
* {@link getMaxDepth()} instead.
*/
public $maxDepth;

/**
* Constructs a metadata for the given attribute.
*
Expand Down Expand Up @@ -72,6 +81,22 @@ public function getGroups()
return $this->groups;
}

/**
* {@inheritdoc}
*/
public function setMaxDepth($maxDepth)
{
$this->maxDepth = $maxDepth;
}

/**
* {@inheritdoc}
*/
public function getMaxDepth()
{
return $this->maxDepth;
}

/**
* {@inheritdoc}
*/
Expand All @@ -80,6 +105,11 @@ public function merge(AttributeMetadataInterface $attributeMetadata)
foreach ($attributeMetadata->getGroups() as $group) {
$this->addGroup($group);
}

// Overwrite only if not defined
if (null === $this->maxDepth) {
$this->maxDepth = $attributeMetadata->getMaxDepth();
}
}

/**
Expand All @@ -89,6 +119,6 @@ public function merge(AttributeMetadataInterface $attributeMetadata)
*/
public function __sleep()
{
return array('name', 'groups');
return array('name', 'groups', 'maxDepth');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ public function addGroup($group);
*/
public function getGroups();

/**
* Sets the serialization max depth for this attribute.
*
* @param int|null $maxDepth
*/
public function setMaxDepth($maxDepth);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that a BC break?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, needs a new interface...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, I think than introducing such interfaces was a (my) mistake. Serializer Metadata are value objects. Nobody will (nor should) implement these interfaces. Composition is better for "extending" metadata.

What do you think about marking them as @internal (and even deprecated) @symfony/deciders?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#17114 merged


/**
* Gets the serialization max depth for this attribute.
*
* @return int|null
*/
public function getMaxDepth();

/**
* Merges an {@see AttributeMetadataInterface} with in the current one.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\Annotations\Reader;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use Symfony\Component\Serializer\Exception\MappingException;
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
Expand Down Expand Up @@ -55,11 +56,13 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
}

if ($property->getDeclaringClass()->name === $className) {
foreach ($this->reader->getPropertyAnnotations($property) as $groups) {
if ($groups instanceof Groups) {
foreach ($groups->getGroups() as $group) {
foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
if ($annotation instanceof Groups) {
foreach ($annotation->getGroups() as $group) {
$attributesMetadata[$property->name]->addGroup($group);
}
} elseif ($annotation instanceof MaxDepth) {
$attributesMetadata[$property->name]->setMaxDepth($annotation->getMaxDepth());
}

$loaded = true;
Expand All @@ -68,29 +71,40 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
}

foreach ($reflectionClass->getMethods() as $method) {
if ($method->getDeclaringClass()->name === $className) {
foreach ($this->reader->getMethodAnnotations($method) as $groups) {
if ($groups instanceof Groups) {
if (preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches)) {
$attributeName = lcfirst($matches[2]);

if (isset($attributesMetadata[$attributeName])) {
$attributeMetadata = $attributesMetadata[$attributeName];
} else {
$attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
$classMetadata->addAttributeMetadata($attributeMetadata);
}

foreach ($groups->getGroups() as $group) {
$attributeMetadata->addGroup($group);
}
} else {
throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
}
if ($method->getDeclaringClass()->name !== $className) {
continue;
}

$accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would name the variable $isAccessorOrMutator to reflect the boolean value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do that for methods but does it make sense for variables? We doesn't name them $intFoo or $stringBar.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think prefixing a variable with is is a bit different than prefixing it with an actual type. But I wouldn't insist on this if you don't like it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've no strong opinion about that. What do we use usually?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$isXXX sound better to me.

if ($accessorOrMutator) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we are not that strict with the return value of preg_match() in other place, but I would prefer false !== $accessorOrMutator.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work. preg_match returns 0 if the pattern is not found and false in case of error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, you're right.

$attributeName = lcfirst($matches[2]);

if (isset($attributesMetadata[$attributeName])) {
$attributeMetadata = $attributesMetadata[$attributeName];
} else {
$attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
$classMetadata->addAttributeMetadata($attributeMetadata);
}
}

foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
if ($annotation instanceof Groups) {
if (!$accessorOrMutator) {
throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
}

$loaded = true;
foreach ($annotation->getGroups() as $group) {
$attributeMetadata->addGroup($group);
}
} elseif ($annotation instanceof MaxDepth) {
if (!$accessorOrMutator) {
throw new MappingException(sprintf('MaxDepth on "%s::%s" cannot be added. MaxDepth can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
}

$attributeMetadata->setMaxDepth($annotation->getMaxDepth());
}

$loaded = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
foreach ($attribute->group as $group) {
$attributeMetadata->addGroup((string) $group);
}

if (isset($attribute['max-depth'])) {
$attributeMetadata->setMaxDepth((int) $attribute['max-depth']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for a value to be greater than zero?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the constraint in the XSD instead.

}
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
$attributeMetadata->addGroup($group);
}
}

if (isset($data['max_depth'])) {
if (!is_int($data['max_depth'])) {
throw new MappingException('The "max_depth" value must an integer in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName());
}

$attributeMetadata->setMaxDepth($data['max_depth']);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,20 @@
<xsd:complexType name="attribute">
<xsd:annotation>
<xsd:documentation><![CDATA[
Contains serialization groups for a attributes. The name of the attribute should be given in the "name" option.
Contains serialization groups and max depth for attributes. The name of the attribute should be given in the "name" option.
]]></xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:sequence minOccurs="0">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this should also be done on older branches

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's not necessary: previously metadata was only used for groups. It makes sense to require it.
But with this new annotation (only in 3.1+) you can use metadata for groups or max depth. It makes sense to make groups optional only for 3.1+.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, but then we should update the inline comments which both still refer to groups only.

<xsd:element name="group" type="xsd:string" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="max-depth">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>

</xsd:schema>
Loading
0