active_model_serializers icon indicating copy to clipboard operation
active_model_serializers copied to clipboard

Allow customizing the type of polymorphic association

Open klaaspieter opened this issue 1 year ago • 0 comments

Purpose

Allow customizing the type of polymorphic association. We needed this because in our codebase we have model names that we'd like to change but we cannot without also doing the necessary database migrations. It hasn't been a priority yet but we want our new API to use the new naming already.

Changes

Read the json_key from the association serializer when determining the polymorphic type to use. This changes the JSON serialization:

{
  id: 1,
  title: 'headshot-1.jpg',
  imageable: {
-    type: 'employee',
-    employee: {
+   type: 'custom',
+   custom: {
      id: 42,
      name: 'Zoop Zoopler'
    }
  }
}

Caveats

This is a breaking change. I'd love to hear how this change can be introduced in a way that is least disruptive.

Related GitHub issues

I think https://github.com/rails-api/active_model_serializers/issues/2131 is related

Additional helpful information

n.a.

klaaspieter avatar Nov 13 '24 08:11 klaaspieter