Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Serializer] Can types be aliased ? #33397

Closed
pounard opened this issue Aug 30, 2019 · 3 comments
Closed

[Serializer] Can types be aliased ? #33397

pounard opened this issue Aug 30, 2019 · 3 comments

Comments

@pounard
Copy link
Contributor

pounard commented Aug 30, 2019

Hello,

We're using the Serializer component for almost a year now on a client project, and we reached the point where we need to refactor some code, changing some classes name, but have data stored into database.

Question is: is it possible to create type aliases, for example, \App\Domain\Model\LegacyEntity that would map to \App\Domain\Model\EntityNewVersion ?

I looked at the class discriminator system, but it doesn't seem to have been created for this purpose. Properties can be aliased too (can it be done with something else than annotations by the way ? But that's another question).

I have a few use cases:

  • using the messenger component, we aliases all our message classes with semantic names (and not PHP classes) such as product.purchase for example, that would map to the \App\Domain\Event\PurchageProductMessage, this in order to have more meaningful reports for the client, but also in order to prepare the future where non-PHP applications might be plugged on the same message broker and publish or consume messages as well,

  • we have serialized data dangling around, either in import/export files or in database, often accompanied with the type alongside, which are still PHP classes names as of now, but the same we'd like to alias our entities.

In a development effort we are doing it by decorating multiple services from the messenger and serializer component, but that's an awful lot of code, and I'd love to know if the component gives us an opportunity to do it by configuration ?

Here's the code we have now to decorate the serializer https://gist.github.com/pounard/25d2a8d4502a6bb61e660957196a5773 (still in dev). It's within a custom bundle, which exposes the canonical type map and the aliases map in configuration, we do everything using YAML but never annotations.

Our name mapping serves two purposes:

  • we have a 1:1 mapping between a canonical alias and a PHP class (i.e. my_vendor.my_app.some_entity <-> \App\Entity\SomeEntity,
  • we have a 1:n mapping between a PHP class and its legacy aliases (for the product to continue working gracefully with outdated data after upgrade) where aliases can be either renamed or non existing PHP class names or arbitrary strings.

If there's not bundled solution for this problem, would you be OK with evaluating this idea and implementing it within the component ? I'd be glad to help one way or the other.

Thanks for your time.

@carsonbot
Copy link

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

@carsonbot
Copy link

Just a quick reminder to make a comment on this. If I don't hear anything I'll close this.

@carsonbot
Copy link

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

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

No branches or pull requests

3 participants