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

Handle a heterogeneous collection via serializers option #410

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

dvandersluis
Copy link

@dvandersluis dvandersluis commented Apr 26, 2019

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 dvandersluis changed the base branch from master to dev April 30, 2019 03:54
@rmachielse
Copy link

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)

@cazacutudor
Copy link

Hello @dvandersluis, we love to have this feature in place 💓 . 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?

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

Successfully merging this pull request may close these issues.

3 participants