fast_jsonapi
fast_jsonapi copied to clipboard
Handle a heterogeneous collection via serializers option
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.
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)
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?