Symfony-docs: Symfony 4.2 : normalize callback deprecated

Created on 3 Dec 2018  路  6Comments  路  Source: symfony/symfony-docs

Hello,

Since the 4.2 version, the setCallbacks is deprecated but there is no where in the doc (and in the Symfony blog) an info of how to set callbacks :
https://github.com/symfony/serializer/blob/ed5657af367082373cb40d08b11550e5a32bd43e/Normalizer/AbstractNormalizer.php#L150

Serializer deprecation hasPR

Most helpful comment

This seems like a useless change to me tbh.
Now I have to pass 5x null to the constructor of a GetSetMethodNormalizer for example.
For now I'm still using the setters but those will be removed eventually..

All 6 comments

Any news on how to replace setCallbacks ?

Hello,

For use callback, use last parameter normalizer ($defaultContext)

See : \Symfony\Component\Serializer\Normalizer\AbstractNormalizer [l. 96 to 109]

public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, array $defaultContext = [])
    {
        $this->classMetadataFactory = $classMetadataFactory;
        $this->nameConverter = $nameConverter;
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);

        if (\is_array($this->defaultContext[self::CALLBACKS] ?? null)) {
            foreach ($this->defaultContext[self::CALLBACKS] as $attribute => $callback) {
                if (!\is_callable($callback)) {
                    throw new InvalidArgumentException(sprintf('The given callback for attribute "%s" is not callable.', $attribute));
                }
            }
        }
    }

Is not testing but from what I see it seems to me to be that

This seems like a useless change to me tbh.
Now I have to pass 5x null to the constructor of a GetSetMethodNormalizer for example.
For now I'm still using the setters but those will be removed eventually..

I found maybe another way

// Line 115 : \Symfony\Component\Serializer\Normalizer\AbstractNormalizer

$callback = $context[self::CALLBACKS][$attribute] ?? $this->defaultContext[self::CALLBACKS][$attribute] ?? $this->callbacks[$attribute] ?? null;
if ($callback) {
    $attributeValue = $callback($attributeValue, $object, $attribute, $format, $context);
}

Use with normalize method ($context parameter).

(Use with serialize method, $context parameter)

we have to be guessing how to replace a method that is deprecated?

I've updated the document for this deprecation at https://github.com/symfony/symfony-docs/pull/11584

Was this page helpful?
0 / 5 - 0 ratings

Related issues

steevanb picture steevanb  路  4Comments

javiereguiluz picture javiereguiluz  路  4Comments

fabpot picture fabpot  路  3Comments

javiereguiluz picture javiereguiluz  路  4Comments

ManInTheBox picture ManInTheBox  路  4Comments