Skip to content

Add global handlers for all data types#1587

Open
MoviU wants to merge 1 commit into
schmittjoh:masterfrom
MoviU:global-handlers
Open

Add global handlers for all data types#1587
MoviU wants to merge 1 commit into
schmittjoh:masterfrom
MoviU:global-handlers

Conversation

@MoviU
Copy link
Copy Markdown

@MoviU MoviU commented Feb 20, 2025

Q A
Bug fix? no
New feature? yes
Doc updated yes
BC breaks? no
Deprecations? no
Tests pass? yes
License MIT

Allow custom handlers for all types. ("type" => "*").

@scyzoryck
Copy link
Copy Markdown
Collaborator

Hello.
What would be a usage case for it?

Best, Marcin.

@MoviU
Copy link
Copy Markdown
Author

MoviU commented Feb 20, 2025

Hi,

In some cases, the default serialization behavior needs to be customized. For example, in my current use case, if a DTO is marked with a specific attribute (e.g., #[CustomDTO]), I need to apply custom serialization logic—such as inspecting property attributes and executing specific logic when a custom attribute is found.

My current implementation maps a handler to every DTO within a given namespace. This approach relies heavily on Composer’s autoloading and requires cache revalidation whenever changes occur in that namespace (e.g., when a new DTO is created).

Allowing handlers to apply to all types would enable seamless customization of the default serialization process without the need for complex workarounds.

@scyzoryck
Copy link
Copy Markdown
Collaborator

I think adding custom Handler for all classes will create multiple issues - for example with depth exclusions that might not work as expected.
May event system may solve your issues? https://github.com/schmittjoh/serializer/blob/033c9beab9eb708509a3d400e9f0ffeb2d440e71/doc/event_system.rst#serializerpre_serialize
You can change the type to * for any class that needs to handled by your custom Handler.

@MoviU
Copy link
Copy Markdown
Author

MoviU commented Feb 20, 2025

The issue I encountered with events during deserialization is that they operate on an already deserialized instance.

If the DTO is readonly, modifying its properties directly isn't possible. Instead, I would need to create a new instance with the updated data.

However, the event system doesn't allow replacing the deserialized result.

Maybe there is another way to achieve this? What do you think?

@scyzoryck
Copy link
Copy Markdown
Collaborator

Have you checked serializer.pre_deserialize event? It operates on raw data.

@MoviU
Copy link
Copy Markdown
Author

MoviU commented Feb 25, 2025

Yes, I tried using pre_deserialize also. And I think it still isn't useful in my use-case, since I can't change the data directly

@MoviU
Copy link
Copy Markdown
Author

MoviU commented Mar 4, 2025

Hi! Any updates?

@scyzoryck
Copy link
Copy Markdown
Collaborator

Can you try the following please?
In serializer.pre_deserialize event change type of

    public function onPreSerializeEnum(PreSerializeEvent $event): void
    {
        $oldType = $event->getType();
        $event->setType('*', $oldType);
    }

Register custom handler for * type ;-)

Soo in that way I believe you should achieve your goals. :)

@MoviU
Copy link
Copy Markdown
Author

MoviU commented Apr 3, 2025

Hi! Thank you for your help!

I tried using subscriber like this, and it looks like it changes the type for the serialization process. I still want to keep the type, but I want to fallback to default serialization handler ("*") if no specific handler was provided.

That's why unfortunately this won't work for me😔

@MoviU
Copy link
Copy Markdown
Author

MoviU commented May 7, 2025

Hi! Any updates?

@scyzoryck
Copy link
Copy Markdown
Collaborator

Hi!
I'm still not convinced to this idea. @goetas any thoughts from your side on that?

Best.

@MoviU
Copy link
Copy Markdown
Author

MoviU commented May 29, 2025

Hi, @goetas! Any updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants