Skip to content
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

Merged
merged 3 commits into from
Jun 13, 2015

Conversation

edwardloveall
Copy link
Contributor

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

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
@bf4
Copy link
Member

bf4 commented Jun 11, 2015

Test failures are a race condition in the specs (what, ivars everywhere? yup) to be address in another issue/PR.

@joaomdmoura
Copy link
Member

Would be awesome if we could try to make this one green before merge it 😄

@bf4
Copy link
Member

bf4 commented Jun 12, 2015

@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.

bf4 added 2 commits June 12, 2015 11:27
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)
@bf4
Copy link
Member

bf4 commented Jun 12, 2015

Created PR into this branch edwardloveall#1

@bf4
Copy link
Member

bf4 commented Jun 12, 2015

@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))
Copy link
Member

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.)

@joaomdmoura
Copy link
Member

Great! Really awesome paired work here!
Indeed a quick win 😄, simple and solves the problem.
Merging it.

joaomdmoura added a commit that referenced this pull request Jun 13, 2015
Don't pass serializer option to associated serializers
@joaomdmoura joaomdmoura merged commit de23501 into rails-api:master Jun 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants