Open
Description
I think the default behavior of AMS should be to error if a serializer cannot be found. Otherwise, it's easy to stumble into a major security hole.
Imagine you have a controller, maybe something like:
class RecentUsersController < ApplicationController
def index
render json: User.recent.limit(5)
end
end
and a serializer:
class UserSerializer < ActiveModel::Serializer
attribute :first_name
attribute :user_since
end
Now, imagine you either put that serializer at the wrong path by accident, or you named it RecentUserSerializer
, but forgot to specify it by name in the render call. Either way, you're now calling as_json
on the models and handing hashed passwords out over your API.
I'm using something like this to fix the issue, but I think it should be default behavior:
module ErrorOnMissingSerializer
module AsJSONOverride
def as_json(*_)
serializable_resource = ActiveModelSerializers::SerializableResource.new(self)
if serializable_resource.serializer
serializable_resource.as_json
else
raise NotImplementedError, "No serializer was found for #{self.class.name}"
end
end
end
class Railtie < Rails::Railtie
ActiveSupport.on_load :active_record do
ActiveRecord::Base.include ErrorOnMissingSerializer::AsJSONOverride
end
end
end