dynamoid icon indicating copy to clipboard operation
dynamoid copied to clipboard

Broken :only and :except options when serialising an object (as_json)

Open UrsaDK opened this issue 5 years ago • 1 comments

Dynamoid relies on ActiveModel to do the serialisation of Document objects. This should allow us to filter document attributes via a custom as_json method, when a document is serialised to JSON. Eg:

class MyDocument
  include Dynamoid::Document
  ...
  def as_json(options = {})
    super(options.merge(except: [:id]))
  end

Unfortunately, this doesn't work as expected because ActiveModel::Serialization.serializable_hash expects attributes.keys to return an array of strings, not symbols:

  • https://github.com/rails/rails/blob/v6.0.2.1/activemodel/lib/active_model/serialization.rb#L133

UrsaDK avatar Mar 04 '20 14:03 UrsaDK

Good catch. It's definitely should be fixed. Added to the backlog.

andrykonchin avatar Mar 05 '20 20:03 andrykonchin