-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
baa35a3
38f562a
23f3ac6
9445fb6
581abf1
b6529d4
b4cd155
f9a3f88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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']) { | ||
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))); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't that a BC break? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, needs a new interface... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would name the variable There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think prefixing a variable with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've no strong opinion about that. What do we use usually? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $isXXX sound better to me. |
||
if ($accessorOrMutator) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know we are not that strict with the return value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't work. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check for a value to be greater than zero? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added the constraint in the XSD instead. |
||
} | ||
} | ||
|
||
return true; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like this should also be done on older branches There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> |
There was a problem hiding this comment.
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'])) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
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.