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

Skip to content

[Serializer > Context (annotation) > Callbacks] Callbacks defined in the attribute Context annotation are ignored #43563

Closed
@yyaremenko

Description

@yyaremenko

Symfony version(s) affected: 5.3

Description
According to this post, the Context annotation allows to add context per attribute.

If you look closely into the code though, you'll discover that

  1. Upon normalization, the callbacks are only retrieved from the context passed explicitly to normalize() method:
    https://github.com/symfony/serializer/blob/5d7f068253ac3e7c62964ebdda491b06d401059a/Normalizer/AbstractObjectNormalizer.php#L187

  2. Upon denormalization, either callbacks passed explicitly to denormalize() or passed as annotation, e.g.

/**
 * @Context({
 *     "denormalizationContext": { "callbacks": { "someValue": "doSomeStuffOnDenormalization" } }
 * })
 */
private int $someValue;

are completely ignored. (?)

https://github.com/symfony/serializer/blob/5d7f068253ac3e7c62964ebdda491b06d401059a/Normalizer/AbstractObjectNormalizer.php#L401

How to reproduce
Add Context annotation to an attribute, with callbacks for normalization / denormalization and see if they are actually called.

Possible Solution
Add the missing functionality

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions