-
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
Add regression test for fields in JSON/Attributes adapter #1285
Conversation
def test_fields_with_no_associations_include_option | ||
serializer = ArraySerializer.new([@first_post, @second_post]) | ||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) | ||
actual = adapter.serializable_hash(fields: [:id]) |
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.
The option should not be passed to serializable_hash
but to the adapter's constructor.
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 can fix it. But I should inform you that fields are working out of the box because there is a bug here: https://github.com/rails-api/active_model_serializers/blob/master/lib/action_controller/serialization.rb#L50-L51
and not because the adapter's constructor passes the fields option to the serializer. (the adapter's options are in instance_options whereas the serializer options are in options all Attributes methods)
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.
Actually, you should replace those 3 lines with:
actual = ActiveModel::SerializableResource.new([@first_post, @second_post], adapter: :json, fields: [:id])
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.
Yes I agree, but it's not working even in master, I will send a regression test now.
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.
actual = ActiveModel::SerializableResource.new([@first_post, @second_post], adapter: :json, fields: [:id])
@vasilakisfil plz amend per @beauby
# maybe some setup to explain
actual = ActiveModel::SerializableResource.new([@first_post, @second_post], adapter: :json, fields: [:id]).as_json
expected = { posts: [{id: 1}, {id: 2}] }
assert_equal(actual, expected)
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.
Also, I thought fields was JSON API specific. I'm not against adding some of the JSON API semantics to the JSON adapter, though
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.
@bf4 Well currently the Json adapter does support a lesser version of the fields
option (namely it allows to specify the attributes but just for the primary data). Support is broken though.
So, for the record: --- expected
+++ actual
@@ -1 +1 @@
-{:posts=>[{:id=>1}, {:id=>2}]}
+{:posts=>[{:id=>1, :comments=>[], :blog=>{:id=>999}, :author=>{:id=>1}}, {:id=>2, :comments=>[], :blog=>{:id=>999}, :author=>{:id=>1}}]} |
It is expected that the (first-level) associations be included, as the |
So there are two issues here:
|
@vasilakisfil Currently, associations are handled through the |
@beauby you can still filter associations based on includes. Fields on association is optional meaning that if you don't provide any fields for an association it won't apply and thus it will render all attributes of the associations (like it is now). I want and I believe that you should be able to select in the JSON/Attributes adapters specific fields of an association. Do you have something else to suggest ? |
I'm not convinced. Could you show me some use cases? |
@beauby the same use cases that apply on filtering the attributes of the main association, the same apply on filtering the attributes of an association. Am I missing something ? |
Sure sure, but In what situation would you really need to control that at the adapter level? You can already design your serializers in such a way that it covers most use cases I think. Do you have an (real-world) example where this feature would be needed? |
I thought we had agreed that we need that in the constructor level in another thread #1141 (comment) Permissions is one reason: I don't want to have multiple serializers for every role, neither do I want to have the authorization logic in the serializers. |
Plus, isn't that possible currently in the JSONAPI adapter? |
I was talking about JsonApi in that thread. The reason I'm not convinced is because the Json/Attributes adapters are clearly not good choices when you start having complex needs, and I think it's better if users realize this sooner than later. |
I disagree with that but OK. So should I continue working on that PR or should I start creating my own adapter ? |
Currently in the JsonApi adapter we support only the "array syntax", that is |
@vasilakisfil This decision is not final, let's hear other people's thoughts (cc @joaomdmoura @bf4 @NullVoxPopuli) |
I think fields shouldn't touch associations -- that (I believe) would cause a lot of confusion around the choice of the word fields and with existing functionality. maybe the same thing could be accomplished by also specifying the include option?
|
fields don't touch associations unless you have one under includes. I don't I think the main issue here is: do we want filtering the attributes on
|
@@ -85,6 +85,20 @@ def test_include_option | |||
|
|||
assert_equal(expected, actual) | |||
end | |||
|
|||
def test_fields_with_no_associations_include_option |
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.
(test name out of date)
ah, I see what you're saying. I would be in favor of mimicing what json api is currently doing. |
Add regression test for fields in JSON/Attributes adapter fix regression tests
@NullVoxPopuli @vasilakisfil @beauby If I understand the JSON API spec correctly, Given an 'article' of type 'articles" that has an 'author' of type 'people':
|
@rails-api/ams Are we still interested in this? |
@remear Yes |
Closing due to inactivity |
Regression test showing how fields fails when there are associations