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

Serializer should be able to specify serializer class. #112

Open
yxhuvud opened this issue May 13, 2019 · 4 comments
Open

Serializer should be able to specify serializer class. #112

yxhuvud opened this issue May 13, 2019 · 4 comments

Comments

@yxhuvud
Copy link

yxhuvud commented May 13, 2019

Currently, the only way to specify what class to use for when serializing a relationship is to give the renderer the class option with a hash with an entry for each serialized resource.

In fast_jsonapi it is possible to specify that for each relationship in the serializer. Sadly, they have no support for polymorphic relationships and doesn't seem to get support for that anytime soon. That

a: It allows specific different views of the same thing, which allow the database layout to be more decoupled from the jsonapi presentation.

b: For users that doesn't think the default lookup path and naming for serializers are good enough, it allows them to not have to repeat every single serializer every single time they have to serialize something.

c: It allows more than one view of the same type of object in the same serialized response.

So I think it would be extremely nice if it was a feature that was supported.

@beauby
Copy link
Member

beauby commented May 13, 2019

c: It allows more than one view of the same type of object in the same serialized response.

One issue is that it actually allows more than one view of the exact same object. Which one should be in the response document then?

@yxhuvud
Copy link
Author

yxhuvud commented May 13, 2019

My spontaneous reaction is, if specified on a relationship, use the serializer. Otherwise use the current logic.

Yes, this means an object could be included multiple times, if it has multiple representations that are different but included different ways. I'm not certain if that is a feature or a problem. I suppose it may make the implementation a bit more complicated, though I haven't checked to verify.

I suppose it also would also allow the inclusion of a parent of a node in a tree structure without having to render all parents.

@yxhuvud
Copy link
Author

yxhuvud commented May 14, 2019

(and FWIW, I think it should be an error if two candidate serializers produce the same value in the type field in the output).

@beauby
Copy link
Member

beauby commented May 14, 2019

The rationale behind this choice (one serializer per type) is twofold:

  1. perfs: each object is serialized only once (the resources graph is first traversed, and then all resources are serialized),
  2. clarity: the "serializer defined on relationship" yielded much confusion in AMS as people wouldn't know which serializer was being used and why.

Regarding

a: It allows specific different views of the same thing, which allow the database layout to be more decoupled from the jsonapi presentation.

For that you can have different serializers. You should probably not have different views of the same thing within a given document.

b: For users that doesn't think the default lookup path and naming for serializers are good enough, it allows them to not have to repeat every single serializer every single time they have to serialize something.

One can simply define it once, and possibly write a wrapper (which is what jsonapi-rails does).

c: It allows more than one view of the same type of object in the same serialized response.

That should be avoided IMO.

Sorry for not elaborating more, but I'm quite busy at the moment. Maybe @dawidof has an other opinion?

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

No branches or pull requests

2 participants