Skip to content

[Serializer] Add ignored attributes option #24071

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
mdeboer opened this issue Sep 2, 2017 · 6 comments
Closed

[Serializer] Add ignored attributes option #24071

mdeboer opened this issue Sep 2, 2017 · 6 comments

Comments

@mdeboer
Copy link
Contributor

mdeboer commented Sep 2, 2017

Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no
Symfony version 3.3.6

I know the serializer is due for a redesign (#19330) but I'm daring to make a feature request for the time being. Currently (afaik) the mapping (yaml/xml/annotations) lack a way of ignoring attributes and you end up writing a normalizer to do just that.

With a bit of digging I figured it won't be too difficult to add an attribute metadata option to ignore the attribute (as is done for groups and maxDepth options). This can then be read by the (to be written) getIgnoredAttributes getter to get the ignored attributes, instead of accessing the ignoredAttributes array directly.

The ignored attribute is useful for classes that have no groups but want to exclude a property, say the id property. It's cumbersome to add a group specifically to do that.

The YAML could look like this:

My\Fancy\Namespace\Entity\Alien:
  attributes:
    id:
      ignore: true

Or to keep things clean:

My\Fancy\Namespace\Entity\Alien:
  ignoredAttributes:
    - id

Please share your thoughts as I'm willing to implement this 👍

@mdeboer mdeboer changed the title [Serializer] Adding ignore option [Serializer] Add ignored attributes option Sep 2, 2017
@raress96
Copy link

raress96 commented Aug 24, 2018

Maybe an @ignored annotation like in JMSSerializerBundle could also be added.
This would be especially useful when also using the ApiPlatform project with entities with a lot of fields and for fixing circular references easily.

@vsilvestre-accenture
Copy link

This actually exist through ignored_attributes isn't it ?

@javiereguiluz
Copy link
Member

Closing as fixed because of what @vsilvestre-accenture said. Thanks!

@theoaks
Copy link

theoaks commented Apr 15, 2020

That approach is not a neat way to do it... I would have preferred using an annotation. That way, we can abstract certain implementations and not worry about case to case basis. You guys can take a cue from NewtonSoft.Json. Everything they did is using C# attributes (which is the idea behind PHP Annotations)

@raress96
Copy link

@theoaks There is a pull request for this:
#28744

@mdeboer
Copy link
Contributor Author

mdeboer commented Apr 20, 2020

@theoaks in hindsight I agree on this (mind you, this is openened more than two years ago). But yes there is a PR open for this as mentioned.

fabpot added a commit that referenced this issue Apr 24, 2020
This PR was squashed before being merged into the 5.1-dev branch.

Discussion
----------

[Serializer] Add an @ignore annotation

| Q             | A
| ------------- | ---
| Branch?       | master
 Bug fix?      | no
| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #24071
| License       | MIT
| Doc PR        | n/a

Add an `@Ignore` annotation to configure [ignored attributes](https://symfony.com/doc/current/components/serializer.html#ignoring-attributes) in a convenient way, as well as the related XML and YAML loaders.

TODO:

* [x] Add tests

Commits
-------

8526d7c [Serializer] Add an @ignore annotation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants