-
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
Create NestedJson adapter. #1073
Conversation
wow! Didn't reviewed it yet, but this is awesome, I'm definitely interest on this. I'll check tonight probably. |
Hi, This adapter raises an error if nesting is too deep, typically in case of circular relations. If you want to change the limit depth, you can do it just giving an argument |
cattr_accessor :default_limit_depth | ||
@@default_limit_depth = 5 | ||
|
||
def serializable_hash options = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yikes, it's a pretty big smell in the Json serializer that we're subclassing it but completely overriding this method, where everything important happens
also, might this belong in the Adapter superclass? It looks on first inspection like it would apply to the JsonApi adapter as well, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is for #fragment_cache
.
The way we cache fragmentation is exactly the same to Adapter::Json
( like Adapter::FlattenJson
as well).
And Adapter::JsonApi
caches differently.
Thanks! |
Hey, sorry for taking so long, was really busy, now I stopped to check it our for real. I know that you needed multi-depth relations and make your own adapter was the fastest way, but IMO the best scenario would be to find a way to add it to json + flatten-json existing adapters. What are your thoughts about it? 😄 |
@joaomdmoura I agree with you. OK, I will add these functionalities on this adaptor.
After covering these, I think we can replace the existing json, flatten-json adapaters. |
7cf7d55
to
33c0300
Compare
Hi, I updated the adapter and replaced existing Json/FlattenJson adapter. This adapter accepts options:
Setting And default values are available setting like: Adapter::Json.default_limit_depth = 5
Adapter::Json.default_check_depth_strategy = :fail For now, I set default values to follow original adapters behavior, and made them to pass all the existing tests untouched. To do so, we need to fix the default values and rewrite the relevant tests. |
I really like this approach, defining all appropriate associations in each serializer and then being able to limit the depth in the controller is perfect. |
One bug I noticed will trying out this branch, is that attributes with the name def Api::V1::CommentSerializer < ActiveModel::Serializer
attribute :id
attribute :name
end you get: undefined method `name' for #<Api::V1::CommentSerializer:0x007fdec7a23408>` This is fixed by defining a method: def name
object.name
end This is the only attribute it happens on that I have found so far. |
And +1 for supporting a |
@bryanrite hi, thanks for your support :)
You can try As I read the code, singular version is for the case when the key and the accessor method are not the same. |
The error is related to #1094. |
Updated to support limitless depth. |
@beauby ah, right. thanks for the comment. |
def fragment_cache(cached_hash, non_cached_hash) | ||
Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash) | ||
def too_deep? depth | ||
if depth > @limit_depth |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!
Moreover, the "depth strategy" is available via wildcards: render json: @posts, include: '*.*.*' This would include all nested relationships 3 levels down. |
I needed an adapter to serialize models with multi-depth relations for my project.
So I wrote it.
I think this might be helpful for some people.