-
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
Infer serializer from collection name for empty results root #1867
Infer serializer from collection name for empty results root #1867
Conversation
@mchitten, thanks for your PR! By analyzing the annotation information on this pull request, we identified @bf4, @Empact and @RomanKapitonov to be potential reviewers |
0025853
to
b0553f2
Compare
begin | ||
# 3. use json_key from serializer from collection name | ||
key ||= serializer_from_resource(object.name.safe_constantize.new, ActiveModel::Serializer, options).json_key | ||
rescue NoSerializerError, NameError |
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.
Sort of wishing I had merged #1767 at this point...
b0553f2
to
f1aed83
Compare
@@ -9,6 +9,7 @@ Features: | |||
Fixes: | |||
|
|||
- [#1833](https://github.com/rails-api/active_model_serializers/pull/1833) Remove relationship links if they are null (@groyoh) | |||
- [#1867](https://github.com/rails-api/active_model_serializers/pull/1867) Infer collection root from collection name if possible (@mchitten) |
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.
this is really something like
Infer collection root preferably from the serializer for the collection name then the collection name, if a named collection
?
f1aed83
to
521058a
Compare
def json_key | ||
'resource' | ||
end | ||
end |
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.
Seems you are wanting these in the global scope. Can we perhaps dynamically create and remove these classes or alias exiting ones? (.e.g SerializedResource = Post...)
521058a
to
efa3d8b
Compare
efa3d8b
to
145ae97
Compare
@bf4 Ready for another look when you get the chance |
Hey @bf4, any additional thoughts on this? Thanks! |
Object.const_set(:SerializedResource, serialized_resource) | ||
Object.const_set(:SerializedResourceSerializer, serializer) | ||
|
||
resource.define_singleton_method(:name) { 'SerializedResource' } |
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.
- I'd rather this be in a block so that we can also teardown the classes after defining them
- I think we can consolidate the code here a little
def with_serializable_named_model(name)
model = Class.new(::Model) do
def self.name
'SerializableResource'
end
end
Object.const_set(:SerializableResource, model)
serializer = Class.new(ActiveModel::Serializer) do
def json_key
name
end
end
Object.const_set(:SerializableResourceSerializer, serializer)
yield model
ensure
Object.send(:remove_const, :SerializableResource)
Object.send(:remove_const, :SerializableResourceSerializer)
end
then
def test_json_key_with_resource_with_name_and_serializer
with_serializable_named_collection('resources') do |model|
serializer_instance = collection_serializer.new([model])
assert_equal 'resources', serializer_instance.json_key
end
end
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.
(would also be good to note for the reviewer how the test would fail before the code changes)
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.
Not sure how important to your use case, but note that in the test I changed
- collection_serializer.new(model)
+ collection_serializer.new([model])
since the collection serializer should take in an array of models, or at least something enumerable. Was that a bug in the test or in your implementation?
Will this be merged?? |
@dyoganand This needed some revisions. Would you be interested in taking it on? |
same issue: #2087 (comment) |
When is this PR going to be merged? I am being bothered exactly at the moment by the bug which this PR potentially fixes up. |
2 years... Two years! And this isn't merged... |
@samuelpismel at this point, if you want newish support / a library that easier to maintain, I'd use http://jsonapi-rb.org/ (by @beauby who also is a member of @rails-api/ams ) if you don't have a jsonapi style api, jbuilder is probably good enough. This project has gone through so many rewrites... :-\ Apologies for the circumstances. :( Please see Status of AMS |
Thanks man, you are correct, sorry about that. |
Purpose
In the event that a collection object is empty and has a name, it's possible to determine the serializer from the name of the object. This may not always be the case -- this could raise
NameError
for a class that does not exist, orNoSerializerError
if its matching serializer does not exist, in which case this falls back to the originalname.underscore
implementation.Changes
Updates
CollectionSerializer
to try to determine serializer from object name.Related GitHub issues
Possibly(?) a fix for #1745.