Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Serializer] Unexpected normalization with custom Encoder happens when APP_DEBUG=1 #47085

Copy link
Copy link
Closed
@ostrolucky

Description

@ostrolucky
Issue body actions

Symfony version(s) affected

6.1.2

Description

If custom encoder is used, rules when normalization is hit are different apparently. We expect no normalization happens in either environment (because our encoder doesn't need normalization).

How to reproduce

Have this encoder

final class PhpEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface
{
    public function encode(mixed $data, string $format, array $context = []): string
    {
        return serialize($data);
    }

    public function decode(string $data, string $format, array $context = []): mixed
    {
        return unserialize($data);
    }

    public function supportsEncoding(string $format, array $context = []): bool
    {
        return true;
    }

    public function supportsDecoding(string $format, array $context = []): bool
    {
        return true;
    }
}

then try to serialize object. In APP_ENV=prod we get serialized object as expected (starting with O:)
O:56:". In APP_ENV=dev we get serialized array (unexpected) (starting with a:1:{s:)

Possible Solution

Adjust condition at

return !$this->encoder instanceof NormalizationAwareEncoder;

Additional Context

We were struggling to understand why output of serialization was different between APP_ENV=dev and APP_ENV=prod. Looks like issue is that in dev environment, TraceableEncoder is used. TraceableEncoder has this defined

return !$this->encoder instanceof NormalizationAwareEncoder;

In APP_ENV=prod TraceableEncoder is not used, so this condition is triggered instead

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.