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

Serializer not found if the association's serializer is namespaced but the model is not #2295

Closed
Hirurg103 opened this issue Oct 20, 2018 · 2 comments

Comments

@Hirurg103
Copy link

Hello! I put all the serializers under the API namespace. My problem was that parent serializer
could not find serializers for the associated array of objects. I added the following patch to my
codebase to temporary fix this issue:

#lib/active_model_serializers/lazy_association_patch.rb

# AMS v0.10.7 doesn't respect `namespace` option in the `has_many` association
#
#   class API::UserSerializer < ActiveModel::Serializer
#     has_many :changesets, namespace: API
#   end
#
# To make AMS take in the account namespace when it finds serializer for collection's objects
# has been patched `instantiate_serializer` method in the `ActiveModel::Serializer::LazyAssociation`

require 'active_model/serializer/lazy_association'
class ActiveModel::Serializer::LazyAssociation
  def instantiate_serializer(object)
    serializer_options = association_options.fetch(:parent_serializer_options).except(:serializer)
    serializer_options[:serializer_context_class] = association_options.fetch(:parent_serializer).class
    serializer = reflection_options.fetch(:serializer, nil)
    serializer_options[:serializer] = serializer if serializer

    # forward namespace to the collection serializer
    serializer_options[:namespace]  = reflection_options[:namespace] if reflection_options[:namespace]

    serializer_class.new(object, serializer_options)
  end
end

Then I looked into the test suite for AMS and was able to reproduce this bug with fixtures which are used in the AMS tests

class Post < ::Model
  associations :comments

  def latest_comments
    comments[0..3]
  end
end
class Comment < ::Model; end

class ResourceNamespace
  class PostSerializer < ActiveModel::Serializer
    has_many :latest_comments, namespace: ResourceNamespace
 end
  class CommentSerializer     < ActiveModel::Serializer; end
end

@comment = Comment.new
@post = Post.new(comments: [@comment])
@post_serializer = ResourceNamespace::PostSerializer.new(@post)

@post_serializer.associations.first.association.lazy_association.serializer.first
=> nil

But it should be ResourceNamespace::CommentSerializer

I've added a failing test which tests serializing of not namespaced objects using namespaced serializers and the fix above seems to made it green

I plan to open a PR related to this bug.

--
It would be cool to omit namespace: API every time I define associations in a namespaced serializer

@Hirurg103
Copy link
Author

Hi @bf4, do you think it is mine changes broke the build?

@Hirurg103
Copy link
Author

I think this can be closed now because #2296 is merged. Thanks @bf4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant