symfony-docs icon indicating copy to clipboard operation
symfony-docs copied to clipboard

[Serializer] Update serializer.rst

Open sfmok opened this issue 2 years ago • 6 comments

I have checked ObjectNormalizer and I found only methods start by (is, has, get, and can) can be detected automatically for normalising them

Am I wrong?

sfmok avatar Jun 24 '22 19:06 sfmok

In the PropertyInfo component I can find references to add and remove:

https://github.com/symfony/symfony/blob/4a31363c3c9be2192e757c3a32a7e3a90874a2cb/src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php#L38-L48

But in the Serializer component I can't find any references to them. This is what I see:

https://github.com/symfony/symfony/blob/4a31363c3c9be2192e757c3a32a7e3a90874a2cb/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php#L128-L148

javiereguiluz avatar Jul 11 '22 15:07 javiereguiluz

OP probably looks at it from POV of normalization only and is forgetting about denormalization.

ostrolucky avatar Jul 11 '22 20:07 ostrolucky

Object normalization/denoramilzation definitely takes into account adders/removers for array properties. As @javiereguiluz points out, when denormalizing it uses the PropertyInfo component to set the value of an array property. If there is not a function setFoos(array $foos) then it'll look for function addFoo($foo) and function removeFoo($foo).

However, the section that OP is editing is specifically to do with boolean attributes. I would imagine that adders/removers are not considered for boolean properties, however I am not certain of this.

ndench avatar Jul 11 '22 22:07 ndench

ping @mtarld

FabienPapet avatar Jul 12 '22 09:07 FabienPapet

Indeed, as @javiereguiluz and @ndench pointed out, the PropertyInfo can find add and remove methods (therefore the ObjectNormalizer can), but the add and remove methods are related to collections and not boolean.

So I think it doesn't make sense to mention them here. Moreover, IMHO, I'm not sure that we must mention the get method even if it can return a boolean as it's not specific to booleans only (you can have a look at the ReflectionExtractor)

mtarld avatar Jul 12 '22 11:07 mtarld

Thank you all for the clarification.

In fact, I assumed the edited part is talking about booleans attributes based on the first phrase context is about the is method.

I think we can adjust it somehow to be more clear like:

The ObjectNormalizer also takes care of methods starting with :

  • has and can for booleans attributes.
  • add and remove for collections

for get method, I avoided adding it into the list since it's not specific to booleans as @mtarld mentioned

sfmok avatar Jul 17 '22 16:07 sfmok

Thank you @sfmok.

javiereguiluz avatar Oct 04 '22 13:10 javiereguiluz