Closed marceloperini closed 4 years ago
@marceloperini
See the answer in this StackOverFlow post:
The stackoverflow post shows that an included
attribute should be rendered. Looking at the test result, the included
attribute is not rendered.
I am experiencing the same issue on Rails 6
.
In order for the custom serializer to take effect, you have to specify the include
option when initializing the serializer. The documentation does not make that clear.
IE
This
post_serializer = PostSerializer.new(@post).serializable_hash
needs to change to
post_serializer = PostSerializer.new(@post, include: [:comments]).serializable_hash
This usage is tricky -- you have to pass custom options every time, instead of the attributes being consistent and defined in the Serializer itself. I was expecting to have something like:
class ProductSerializer
include FastJsonapi::ObjectSerializer
included :components, serializer: ProductComponentSerializer
or even
has_many :components, include: { serializer: ProductComponentSerializer }
Thanks for the clarifications, it help me a lot. But I think this has to be more clear on the documentation
Summary
When define a serializer with
has_many
,has_one
orbelongs_to
relationship withserializer
option, it not being used the correct serializer class for relationship objects.Steps to reproduce
Expected behavior
When defining a serializer class for relationships it is expected to use the correct class.
Actual behavior
System configuration
Ruby version: 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux] FastJson version: master