-
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
Fix #876 - include meta when using json adapter with custom root #936
Fix #876 - include meta when using json adapter with custom root #936
Conversation
Looks good! Should also have a test for the array serializer |
@@ -75,7 +75,7 @@ def meta_key | |||
end | |||
|
|||
def root | |||
serializer.json_key | |||
@options.fetch(:root, serializer.json_key) |
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.
Probably marginally better to have @options.fetch(:root) { serializer.json_key }
so that the fallback is only evaluated when the key is missing.
Cool! Thank you both of you! A quick win 😄 . Also agree that was cleaver to not use #915 for now. |
Looking great!! Question: should any of these tests be in test/serializers/meta_test.rb (or should any of them be failing right now?) I think it would be great to add a test around a meta key present, but no root given in the render, to show that meta is ignored in the Array scenario. I don't know if there's an advantage to also testing the below root-related settings:
Also related: |
Yes, I agree with everything @bf4 is saying. We need moar teeeeests |
Great, would be awesome if we could already add some of this tests in this PR 😄 |
76777b6
to
c4a76c2
Compare
The tests in test/serializers/meta_test.rb do not fail because they pass the I've altered meta_test.rb to partition the options in the same way as the serialization controller so they fail without my changes, and added a new test to check for the presence of meta when using the array serializer and a custom root. Commits squashed with a hopefully cool and clear enough commit message 😎 |
Not sure why the CI tests are failing - it seems to be falling over on one unrelated test when run against Rubinius on Rails 4.0 ... |
@chrisbranson CI is green. All of the failures I see are pre-existing and ignored. Your'e good :) |
@bf4 Good news - it was failing earlier but seems to have sorted itself out now 😄 |
ActiveModel::Serializer::Adapter::Json.new(serializer) | ||
adapter_opts, serializer_opts = | ||
options.partition { |k, _| ActionController::Serialization::ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] } | ||
|
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.
wow is this screaming for a refactor of
active_model_serializers/lib/action_controller/serialization.rb
Lines 37 to 49 in 35fb9de
define_method renderer_method do |resource, options| | |
@_adapter_opts, @_serializer_opts = | |
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] } | |
if use_adapter? && (serializer = get_serializer(resource)) | |
@_serializer_opts[:scope] ||= serialization_scope | |
@_serializer_opts[:scope_name] = _serialization_scope | |
# omg hax | |
object = serializer.new(resource, @_serializer_opts) | |
adapter = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts) | |
super(adapter, options) |
I remember reading through all these tests and code while trying to figure out how to test my serializers. I ended up doing something like in my comment in 878 which was a lot more work that you'd think. Also, too bad minitest doesn't have shared examples, though I think ActiveModel::Lint might be a fine pattern.
@chrisbranson This is so great! Thanks for doing all the actual work! Lemme know if you'd rather limit the scope of work (and comments) in any way and defer some work to future PRs. (especially since I'm not even a collab, just have experience with this code not working right and wanna help :) |
I think this PR is done for the issue it was trying to address - but as I discovered earlier today and you've highlighted in your comments there is considerable scope for refactoring. That said, what's there does work as expected now. Much as I'd like to wade in and help out, I'm not going to get much more time in the near future to focus on non-essential changes simply due to day job time commitments - I've got an API to finish 😄 |
👍 @joaomdmoura @kurko LGTM |
c4a76c2
to
d34bba0
Compare
Updated with the cleaner coding style as recommended by @bf4 👍 |
Waiting for this pull request to be accepted and starting using the |
@iMacTia are you saying you're waiting to use this PR or you have another PR you want to make (ref plz)? |
I mean I'm waiting for this one, should solve #882 as well if I got it right, isn't it? |
@iMacTia what I do in this situation is in my Gemfile # Bundling from source until https://github.com/rails-api/active_model_serializers/pull/936 is merged
gem "active_model_serializers", github: "insphire/active_model_serializers", branch: "fix-meta-with-custom-root" |
Awesome work fellows! Really happy that you guys made it! |
Include meta when using json adapter with custom root
This PR fixes issue #876 where meta was not being included when using the JSON adapter and a custom root.