Thanks to visit codestin.com
Credit goes to github.com

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

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Dec 22, 2015

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #14924 (comment), api-platform/core#104 (comment)
License MIT
Doc PR todo

Add a max depth option during the normalization process. Especially useful when normalizing trees.

Usage:

use Symfony\Component\Serializer\Annotation\MaxDepth;

class MyObj
{
    /**
     * @MaxDepth(2)
     */
    public $foo;

    /**
     * @var self
     */
    public $child;
}

use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$normalizer = new ObjectNormalizer($classMetadataFactory);
$serializer = new Serializer(array($this->normalizer));

$level1 = new MyObj();
$level1->foo = 'level1';

$level2 = new MyObj();
$level2->foo = 'level2';
$level1->child = $level2;

$level3 = new MyObj();
$level3->foo = 'level3';
$level2->child = $level3;
$result = $serializer->normalize($level1, null, array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));
/*      $result = array(
            'foo' => 'level1',
            'child' => array(
                    'foo' => 'level2',
                    'child' => array(
                            'child' => null,
                        ),
                ),
        );
*/
  • Metadata support
  • @MaxDepth(2) annotation and loader
  • XML loader
  • YAML loader
  • Delegate recursive normalization at the end of the process
  • Use constants with Late Static Binding in the abstract class instead of raw strings
  • Move common logic to the abstract class

/cc @mRoca @csarrazi

*
* @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

*
* @return array
*/
protected function setAttribute($data, $attribute, $attributeValue)
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about using the reference operator &$data to avoid useless copy of the array?

Copy link
Contributor

Choose a reason for hiding this comment

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

You can also add an array type hint.

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 prefer to avoid references when possible (http://schlueters.de/blog/archives/125-do-not-use-php-references.html).

Typehint added.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok makes sense.

@GuilhemN
Copy link
Contributor

👍 to merge this.
Just needs to take a decision about the interface.

@GuilhemN
Copy link
Contributor

Sounds good to me
Status: Reviewed

fabpot added a commit that referenced this pull request Dec 26, 2015
This PR was merged into the 2.7 branch.

Discussion
----------

[Serializer] Make metadata interfaces internal

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17113 (comment)
| License       | MIT
| Doc PR        | n/a

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.

They were not marked as `@api` and should now be marked as `@internal` (or even deprecated but it will cause some maintenance headaches).

Commits
-------

3f6cfcd [Serializer] Make metadata interfaces internal
@dunglas
Copy link
Member Author

dunglas commented Dec 30, 2015

ping @symfony/deciders

Would be nice to merge soon to be able to work on #16143 without creating conflicts.


if (!is_int($data['value'])) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be an int.', 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?

@dunglas
Copy link
Member Author

dunglas commented Jan 18, 2016

Status: needs review

ping @symfony/deciders

@@ -41,7 +41,7 @@ public function addAttributeMetadata(AttributeMetadataInterface $attributeMetada
/**
* Gets the list of {@link AttributeMetadataInterface}.
*
* @return AttributeMetadataInterface[]
* @return @return AttributeMetadataInterface[]
Copy link
Member

Choose a reason for hiding this comment

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

typo

@fabpot
Copy link
Member

fabpot commented Jan 25, 2016

Made some small comments, 👍

@dunglas
Copy link
Member Author

dunglas commented Jan 26, 2016

Should be ok now.

@fabpot
Copy link
Member

fabpot commented Jan 26, 2016

Thank you @dunglas.

@fabpot fabpot closed this in 420989f Jan 26, 2016
@dunglas dunglas deleted the max_depth branch January 27, 2016 10:57
@fabpot fabpot mentioned this pull request May 13, 2016
nicolas-grekas added a commit that referenced this pull request Dec 8, 2016
…oNormalize (dunglas)

This PR was squashed before being merged into the 3.1 branch (closes #20530).

Discussion
----------

[Serializer] Remove AbstractObjectNormalizer::isAttributeToNormalize

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | unclear
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

I've introduced this method by error in #17113. It has been forgotten during a refactoring. It has always been unused, is not covered by our test suite and has never been documented.

Technically it's a BC break (because this is a protected method), but I think that it's better to remove it has it has never be intended to be used, it's just a miss. An alternative is to deprecate it and remove it in v4.

Commits
-------

fea18aa [Serializer] Remove AbstractObjectNormalizer::isAttributeToNormalize
xabbuh added a commit to symfony/symfony-docs that referenced this pull request Dec 14, 2016
…, javiereguiluz)

This PR was merged into the 3.1 branch.

Discussion
----------

[Serializer] Docs for the @MaxDepth annotation

Docs for symfony/symfony#17113.

Commits
-------

d7395d3 Make lines shorter to comply with our soft limit of 80 chars per line
93dcc3f Fix comments
97f48e5 [Serializer] Docs for the @MaxDepth annotation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants