active_model_serializers icon indicating copy to clipboard operation
active_model_serializers copied to clipboard

has_many not respecting serializer option when using render include: [...]

Open giorgenes opened this issue 8 months ago • 0 comments

Expected behavior vs actual behavior

I had a weird bug happen to me today. After I added a include: [...] clause to my render statement, I notice one of my objects became a dump of the raw object json instead of using the serializer I specified on has_many.

I have a has_many :stuff, serializer: StuffSerializer.

It turned out that my stuff field wasn't included in the include: [...], which caused the framework to do a raw dump of the data. Adding stuff to include: [...] fixed the issue, but I believe this to be a bug.

Steps to reproduce

(e.g., detailed walkthrough, runnable script, example application)

class MySerializer < ActiveModel::Serializer
  belongs_to :something, serializer: SomethingSerializer
  has_many :stuff, serializer: StuffSerializer

  def stuff
     Stuff.all
  end
end

# in the controller
render json: list, include: [:something], serializer: MySerializer

Environment

ActiveModelSerializers Version (commit ref if not on tag): 0.10.13

Output of ruby -e "puts RUBY_DESCRIPTION": ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-darwin22]

OS Type & Version: Macos Sonoma 14.3.1 (23D60)

Integrated application and version (e.g., Rails, Grape, etc): gem "rails", "~> 7.0.6"

Backtrace

(e.g., provide any applicable backtraces from your application)

N/A

Additonal helpful information

(e.g., Gemfile.lock, configurations, PR containing a failing test, git bisect results)

giorgenes avatar Jun 21 '24 01:06 giorgenes