fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

Handle a heterogeneous collection via serializers option

Open dvandersluis opened this issue 5 years ago • 2 comments

Proposal for allowing for a heterogeneous collection (or a single item of unknown type) to be serialized in a single response.

I settled on injecting the serializers to use in the call rather than having more metadata inside the serializer itself. I think this makes it clearer what the intent is, and also doesn't pollute the serializer itself.

VehicleSerializer.new(
  [car, bus],
  serializers: { Car: CarSerializer, Bus: BusSerializer }
)

I foresee using this with ApplicationSerializer or the like as a base class (this makes more semantic sense to me). I experimented a bit with adding a generic abstract serializer to use, but I don't know if that's appropriate for this change. Another way to do this that I like would be to have a static method on ObjectSerializer, such as:

FastJsonapi::ObjectSerializer.serialize(
  [car, bus],
  serializers: { Car: CarSerializer, Bus: BusSerializer }
)

but to do so we would need a base class to instantiate.

I welcome any feedback, especially in terms of the interface.

References #225, #393.

dvandersluis avatar Apr 26 '19 21:04 dvandersluis

Maybe it would be even cleaner to support polymorphic: true, just the way it works with relationships. That way you would get:

VehicleSerializer.new([car, bus], polymorphic: true)

rmachielse avatar Aug 27 '19 10:08 rmachielse

Hello @dvandersluis, we love to have this feature in place :heartbeat: . As you may already know, most of us have moved to https://github.com/jsonapi-serializer/jsonapi-serializer, if you don't mind, do you want to continue this pull-request there?

cazacutudor avatar Sep 14 '20 08:09 cazacutudor