Inconsistent behaviour in plugins between development and production

AI-generated summary

eatcodetravel reports an inconsistency in how add_to_serializer behaves between development and production environments in Discourse plugins. In development, modifications to parent serializers (e.g., adding :created_at to BasicUserSerializer) are inherited by child serializers due to code reloading. In production, only the parent serializer receives the change. This stems from how ActiveModel::Serializer caches attributes at class definition time. eatcodetravel provides a workaround: monkey-patching ActiveModel::Serializer to add a reload method that merges parent attributes into children, then manually invoking it on descendants after modifying the parent. They note this should ideally be fixed in core for consistency and have opened an issue in the AMS repo for feedback rails-api/active_model_serializers#2254. vinothkannans acknowledges the issue and points to a similar discussion on Meta Discourse. eatcodetravel seeks suggestions for a core fix.

There is an inconsistency on how add_to_serializer works between development and production. This is due how plugins are applied

Given we reload the code in development, every modification we do to a parent serializer, children will pick them up. To reproduce this inconsistency you can execute the code below

add_to_serializer(:basic_user, :created_at) { user.created_at }

If you execute this in development, all descendants of BasicUserSerializer will have the created_at attribute. In production only BasicUserSerializer will have that attribute.

UserNameSerializer._attributes
=> {:id=>:id, :username=>:username, :avatar_template=>:avatar_template, :name=>:name, :title=>:title}
BasicUserSerializer._attributes
=> {:id=>:id, :username=>:username, :avatar_template=>:avatar_template, :created_at=>:created_at}

The problem is due how ActiveModel::Serializer works, and I’m not sure if they want to support this use case, they only get the attributes from the parent once when you call the attributes(*attrs) method.

Workaround

Here’s a workaround to this problem, this is how I’m dealing with it atm.

Inside a plugin do

after_initialize do
  # Monkey patch ActiveModel::Serializer to allow us
  # reload children serializers attributes after parent is modified
  class ::ActiveModel::Serializer
    # Update _attributes with superclass _attributes
    def self.reload
      self._attributes = _attributes.merge(superclass._attributes)
    end
  end

  add_to_serializer(:basic_user, :created_at) { user.created_at }

  # Reload all children serializers, so they include :created_at
  BasicUserSerializer.descendants.each(&:reload)
end

I saw some PRs from you @sam in the AMS repo, maybe you can take a look at this.

Thanks!

FYI there is an similar topic here Plugin working locally But not in live - #4 by vinothkannans. Anyway good catch and your workaround looks awesome.

I want to get this fixed in core, the behaviour should be the same between environments, but not sure how to approach it. I opened an issue in the AMS repo to get feedback there too Runtime modification of parent serializer attributes doesn't update child attributes · Issue #2254 · rails-api/active_model_serializers · GitHub

Any suggestions?