panko_serializer icon indicating copy to clipboard operation
panko_serializer copied to clipboard

Specify serializer inside serializer file

Open Kadaaran opened this issue 4 years ago • 1 comments

Hi there ☀️

Is there a way I can choose inside a serializer to render another serializer based on an attribute ? Here is my code which is triggered into another serializer with has_many :activities, each_serializer: ActivitySerializer

class ActivitySerializer < Panko::Serializer
  attributes :id, :name, :max_capacity

  has_many :levels, each_serializer: LevelSerializer
end

But I'd like to filter has_many: levels based on another attribute called organization inside Level or at least to choose which levels I want to return.

Is it possible ?

Thanks again !

Kadaaran avatar Sep 11 '19 10:09 Kadaaran

@Kadaaran Hi!

If I understand you correctly you want to change the serializer of "levels" instead of LevelSerializer use other serializer based on level attributes?

If so, currently it's not supported since Panko don't have "dynamic runtime", Panko decides which serializers to choose before starting the serialization to optimize for performance.

It's possible to implement this with a bit of hack and not a natural way currently. If this feature is blocking you from using Panko, let's discuss on a proper solution and I will be happy to accept a PR.

yosiat avatar Jan 11 '20 11:01 yosiat