-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Nested serializers/attributes #1190
Comments
I really like that block syntax. omg. |
has_many :comments do
attributes :body, :date
belongs_to :author # This would be serialized by PostSerializer::AuthorSerializer, unless we define a PostSerializer::CommentSerializer::AuthorSerializer
end anonymous class or dynamic constant set here are fine with me. I think this interface is most intuitive. I do like always being able to hook into a method or template method like: def include_attributes(included) # I purposely did not use "filter" because it has a specific sense in the scope of JSON API
if current_user.admin?
included
else
included.except([:secret_stuff1, :secret_stuff2])
end
end it's possible to use both of those... I'm unsure about having conditional attributes Perhaps we could just make ActiveModel::Callbacks All in all, I think nesting more than two levels is an anti-pattern, so I prefer to make doing that possible, but uncomfortable. Sometimes easy is a bad thing :) I'm happy with the current state of making it possible to nest, but not as easy as the 'golden path'. In terms of encapsulation and maintenance, the block form is probably the easiest to implement and maintain, and nicely encapsulates one-off nested associations in an intentionally non-reusable way. It only worries me if people start putting blocks in the blocks because it looks so easy. Much of this can also be fudged in the present state by overriding existing methods like Maybe we can make a test case of a models and a desired output hash, and try different implementations that get the test to pass? |
Update: nested implicit serializers are here (#1193). |
Nice, also in love with the Just one question: has_many :comments do
attributes :body, :date
belongs_to :author # This would be serialized by PostSerializer::AuthorSerializer, unless we define a PostSerializer::CommentSerializer::AuthorSerializer
end How this look up logic works? If it didn't found I tried to catch up with the referenced PR and Issues, but this whole nested thing seems to have chunks spread across everywhere 😄 About 3. I don't think |
@joaomdmoura It would look first for the nested serializer, then for the namespaced serializer (if the resource is inside a namespace), then for a global serializer. Possibly, we can add a lookup in the controller's namespace as well. |
After giving it more thoughts, I'm also against |
Nice @beauby. IMO no need for controller namespace look up, i think we have done enough to make it easy to use already 😄 thank you btw, great work with all this. |
Controller namespace lookesup is handy for versioning serializers. Like Api::v1::___ |
Yup, it could but we can put on the end os the list, if we end up having some extra-time maybe 😄 |
This is done. I'm closing the issue, congratz to @beauby for the hard work on this. |
tl;dr
We need better serializer lookup in order to handle namespaces, which brings the opportunity to handle nested serializers, and introduce a friendly syntax to declare them.
As you may have noticed, there is currently some work being done on nested serialization. Different needs have been identified:
include
adapter option (which allows to specify a potentially infinite (via wildcards) inclusion tree)include
adapter option, unless you set it to**
, in which case only the nested serializers are used)I think the state of 1. is satisfying.
For 2., we could either extend the
include
option to handle attributes as well, but this would lead to implicitly defining nested serializers, which is IMO more confusing than actually defining nested serializers (as in #1157). With #1157, nesting serializers works as follows:I would be in favor of introducing the following syntax:
in conjunction with a default behavior of either including all nested relationships, or at least all nested relationships for which a nested serializer has been provided.
For 3., I think we should either have something like
or something more like 0.8.x
or maybe both.
Questions? Suggestions? PRs?
UPDATE:
#1225 brings support for nested serializers (and automatically uses serializers in the same namespace as the resource)
#1226 brings support for the block syntax
The text was updated successfully, but these errors were encountered: