-
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: Don't pass serializer option to associated serializers #949
Conversation
Fixes rails-api#870 Commit af81a40 introduced passing a serializer's 'options' along to its associated model serializers. Thus, an explicit 'each_serializer' passed to render for a singular resource would be passed on as the implicit 'serializer' for its associations. With @bf4
Test failures are a race condition in the specs (what, ivars everywhere? yup) to be address in another issue/PR. |
Would be awesome if we could try to make this one green before merge it 😄 |
@joaomdmoura Like I wrote above, the specs actually pass, but intermittently fail due to global state. I'll write a PR against master, right now, if that helps. |
A number of test were defining and using the same controller MyController = Class.new(ActionController::Base) which was causing some state to leak across tests.
For some reason, the post would sometimes be serialized as "{\"id\":\"1\", + \"type\":\"posts\", \"attributes\":{\"title\":\"New Post\",\"body\":\"Body\"}, \"comments\":[{\"id\":1,\"body\":\"ZOMG A COMMENT\"}], \"blog\":{\"id\":999,\"name\":\"Custom blog\"}, \"author\":{\"id\":1,\"name\":\"Joao Moura.\"}}" instead of: "{\"id\":1, - \"title\":\"New Post\",\"body\":\"Body\", \"comments\":[{\"id\":1,\"body\":\"ZOMG A COMMENT\"}], \"blog\":{\"id\":999,\"name\":\"Custom blog\"},\ "author\":{\"id\":1,\"name\":\"Joao Moura.\"}}" To reproduce prior to this PR: SEED=55284 rake 1) Failure: ActionController::Serialization::ExplicitSerializerTest#test_render_using_explicit_each_serializer [active_model_serializers/test/action_controller/explicit_serializer_test.rb:139]: --- expected +++ actual @@ -1 +1 @@ -"{\"id\":1,\"title\":\"New Post\",\"body\":\"Body\",\"comments\":[{\"id\":1,\"body\":\"ZOMG A COMMENT\"}],\"blog\":{\"id\":999,\"name\":\"Custom blog\"},\"author\":{\"id\":1,\"name\":\"Joao Moura.\"}}" +"{\"id\":\"1\",\"type\":\"posts\",\"attributes\":{\"title\":\"New Post\",\"body\":\"Body\"},\"comments\":[{\"id\":1,\"body\":\"ZOMG A COMMENT\"}],\"blog\":{\"id\":999,\"name\":\"Custom blog\"},\"author\":{\"id\":1,\"name\":\"Joao Moura.\"}}" 137 runs, 211 assertions, 1 failures, 0 errors, 0 skips rake aborted! Command failed with status (1): [ruby -I"lib:test" -r./test/test_helper.rb "/$HOME/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/rake_test_loader.rb" "test/action_controller/adapter_selector_test.rb" "test/action_controller/explicit_serializer_test.rb" "test/action_controller/json_api_linked_test.rb" "test/action_controller/rescue_from_test.rb" "test/action_controller/serialization_scope_name_test.rb" "test/action_controller/serialization_test.rb" "test/adapter/fragment_cache_test.rb" "test/adapter/json/belongs_to_test.rb" "test/adapter/json/collection_test.rb" "test/adapter/json/has_many_test.rb" "test/adapter/json_api/belongs_to_test.rb" "test/adapter/json_api/collection_test.rb" "test/adapter/json_api/has_many_embed_ids_test.rb" "test/adapter/json_api/has_many_explicit_serializer_test.rb" "test/adapter/json_api/has_many_test.rb" "test/adapter/json_api/has_one_test.rb" "test/adapter/json_api/linked_test.rb" "test/adapter/json_test.rb" "test/adapter/null_test.rb" "test/adapter_test.rb" "test/array_serializer_test.rb" "test/serializers/adapter_for_test.rb" "test/serializers/associations_test.rb" "test/serializers/attribute_test.rb" "test/serializers/attributes_test.rb" "test/serializers/cache_test.rb" "test/serializers/configuration_test.rb" "test/serializers/fieldset_test.rb" "test/serializers/generators_test.rb" "test/serializers/meta_test.rb" "test/serializers/options_test.rb" "test/serializers/serializer_for_test.rb" "test/serializers/urls_test.rb" ] /$HOME/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval' /$HOME/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>' Tasks: TOP => default => test (See full trace by running task with --trace)
Created PR into this branch edwardloveall#1 |
@joaomdmoura wee, all green! (not sure if 14439aa and #951 require more attention. I'm inclined to ignore until it's hard to work around.) |
@@ -217,7 +217,7 @@ def each_association(&block) | |||
if serializer_class | |||
serializer = serializer_class.new( | |||
association_value, | |||
options.merge(serializer_from_options(association_options)) | |||
options.except(:serializer).merge(serializer_from_options(association_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.
This is the simplest thing that worked without doing a bunch of refactoring. (We considered adding a ::new_association_serializer
that wraps ::new
but it wasn't a quick win.)
Great! Really awesome paired work here! |
Don't pass serializer option to associated serializers
Fixes #870
Commit af81a40 introduced passing a serializer's 'options'
along to its associated model serializers.
Thus, an explicit 'each_serializer' passed to render for a
singular resource would be passed on as the implicit 'serializer'
for its associations.
With @bf4