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

[Serializer] Allow to provide (de)normalization context in mapping

Open fabpot opened this issue 4 years ago • 7 comments

Q A
Feature PR symfony/symfony#39399
PR author(s) @ogizanagi
Merged in 5.3-dev

fabpot avatar Feb 16 '21 06:02 fabpot

Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?

carsonbot avatar Feb 18 '22 12:02 carsonbot

Not yet. Let me add a section in https://symfony.com/doc/current/components/serializer.html in the upcoming days!

ogizanagi avatar Feb 18 '22 13:02 ogizanagi

I'm currently searching for docs about the context attribute. To validate if it maybe can fix my problem I have a subentity and currently it is serialized this way:

{
   "id": 1,
   "subEntity": {
       "firstName": "Test",
       "lastName": "Test"
   }
}

instead I want to have it this way:

{
   "id": 1,
   "firstName": "Test",
   "lastName": "Test"
}

In the JMS Serializer this is done via @Inline attribute. So I'm searching for something similar in the symfony serializer as we want to replace jms with symfony here.

@ogizanagi is that something which is possible via the context attribute?

alexander-schranz avatar Mar 01 '22 16:03 alexander-schranz

is that something which is possible via the context attribute?

Not yet since the ObjectNormalizer does not support such a feature yet. But it'll probably help, since it'll allow to declare locally a context on the attribute of the root entity, to inline the subentity attributes if such a feature was introduced in the ObjectNormalizer.

ogizanagi avatar Mar 01 '22 18:03 ogizanagi

@ogizanagi Thx for your response. Atleast I know that I did not miss something. Still like the #[Context] feature as I use it to have for a subentity another serialization group then for the parent entity. Nice work 👍

alexander-schranz avatar Mar 01 '22 20:03 alexander-schranz

Friendly ping @ogizanagi. Is there anything I can help you with to document this cool new attribute?

wouterj avatar Sep 20 '22 12:09 wouterj

Sorry, I didn't take the time yet to document this. Feel free to do so, otherwise I'll try to keep it in mind whenever I have some time. 🙂

ogizanagi avatar Sep 20 '22 16:09 ogizanagi

Finally took the time to document this 😅 See https://github.com/symfony/symfony-docs/pull/17345

ogizanagi avatar Oct 13 '22 12:10 ogizanagi