-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[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 1 commit
baa35a3
38f562a
23f3ac6
9445fb6
581abf1
b6529d4
b4cd155
f9a3f88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
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 <[email protected]> | ||
| * | ||
| * 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 <[email protected]> | ||
| */ | ||
| class MaxDepth | ||
| { | ||
| /** | ||
| * @var int | ||
| */ | ||
| private $maxDepth; | ||
|
|
||
| public function __construct(array $data) | ||
| { | ||
| if (empty($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))); | ||
| } | ||
|
Member
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); | ||
|
Contributor
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?
Member
Author
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...
Member
Author
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
Member
Author
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.
Member
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 |
|---|---|---|
|
|
@@ -28,7 +28,7 @@ class ClassMetadata implements ClassMetadataInterface | |
| public $name; | ||
|
|
||
| /** | ||
| * @var AttributeMetadataInterface[] | ||
| * @var array | ||
|
Member
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. Why this change?
Member
Author
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. Because the key is a
Member
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, so technically this is a bugfix, isn't it? I would say let's improve the PHPdoc in all versions where this applies then.
Member
Author
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. Not really a bug (it just improves static analysis) but it makes sense to apply this on every versions.
Contributor
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. You could use |
||
| * | ||
| * @internal This property is public in order to reduce the size of the | ||
| * class' serialized representation. Do not access it. Use | ||
|
|
||
| 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); | ||
|
Member
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
Member
Author
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
Member
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
Member
Author
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?
Member
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) { | ||
|
Member
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
Member
Author
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.
Member
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']); | ||
|
Member
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?
Member
Author
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 |
|---|---|---|
|
|
@@ -65,6 +65,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata) | |
|
|
||
| if (isset($yaml['attributes']) && is_array($yaml['attributes'])) { | ||
| $attributesMetadata = $classMetadata->getAttributesMetadata(); | ||
|
|
||
|
Member
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. Doing this everywhere (on all versions)? |
||
| foreach ($yaml['attributes'] as $attribute => $data) { | ||
| if (isset($attributesMetadata[$attribute])) { | ||
| $attributeMetadata = $attributesMetadata[$attribute]; | ||
|
|
@@ -75,9 +76,13 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata) | |
|
|
||
| if (isset($data['groups'])) { | ||
| foreach ($data['groups'] as $group) { | ||
| $attributeMetadata->addGroup($group); | ||
| $attributeMetadata->addGroup((string) $group); | ||
|
Member
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. same here |
||
| } | ||
| } | ||
|
|
||
| if (isset($data['max_depth'])) { | ||
| $attributeMetadata->setMaxDepth((int) $data['max_depth']); | ||
|
Member
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 should check the value (we do that for the annotation and we have an implicit check in the XML loader due to the XSD file). |
||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,10 +47,11 @@ | |
| Contains serialization groups for a attributes. The name of the attribute should be given in the "name" option. | ||
| ]]></xsd:documentation> | ||
| </xsd:annotation> | ||
| <xsd:sequence> | ||
| <xsd:sequence minOccurs="0"> | ||
|
Member
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
Member
Author
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.
Member
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" type="xsd:int" /> | ||
|
Contributor
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. <xsd:attribute name="max-depth">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>We want the restriction to be part of the schema, too. |
||
| </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.
I prefer the previous explicit condition
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.
Me too but I've been asked to change such statements to
emptyon several PR.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.
Even on this one actually: #17113 (diff)
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.
That's a shame. We never use empty in Symfony.