Closed makmic closed 2 years ago
you write
user = Class.new {}.new
render json: user, each_serializer: UserSerializer
but your example is not rendering a collection. Is that a bug in the example?
I wonder because the below is not correct usage of serializers vs collection serializers
each_serializer: UserSerializer, serializer: UserSerializer
If you're going to serialize anonymous things as users, maybe specify the root key? (been a while since I've been in the code, but I'm pretty sure you can specify this)
You are right, I used each_serializer
by mistake in this case. Using only the serializer
argument fixes the problem.
I'll close this issue as I cannot reproduce it with the corrected arguments.
Regarding the root key: I'm using one, it was just omitted here :)
Thanks for helping me out!
Expected behavior vs actual behavior
The serializer should be able to render collection of records that have an anonymous superclass without an exception:
The actual behavior is an exeption:
NoMethodError (undefined method demodulize for nil:NilClass)
. It results from an serializer name lookup using the superclass chain - which should be unnecessary as I'm explicitely stating theeach_serializer
.A workaround is to specify the serializer twice:
While the example above might seem abstract, there are relevant real world use cases. For example, active type form models inject an anonymous class to the inheritance chain.
Steps to reproduce
The most minimal example to reproduce the issue would be this:
This is triggers the same exception as for the
render
statement above - see Backtrace below.Environment
ActiveModelSerializers Version: 0.10.13 Output of
ruby -e "puts RUBY_DESCRIPTION"
: ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux] OS Type & Version:Integrated application and version (e.g., Rails, Grape, etc): Rails 6.0
Backtrace
Additonal helpful information
/