Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PaperTrail::Version.where_object cannot not find serialized values in YAML object #1461

Closed
grantcox opened this issue Feb 7, 2024 · 1 comment
Labels

Comments

@grantcox
Copy link

grantcox commented Feb 7, 2024

The fundamental issue here is:

  • When storing the object, all values are YAML serialized (eg PaperTrail::Serializers::YAML::dump calls ::YAML.dump object)
  • When querying, the PaperTrail::Serializers::YAML::where_object_condition does not YAML serialize the values before using them in the SQL query clause

this means that some values can be stored but not found. These include:

  • numeric strings (eg "123" or "12.34"
  • YAML reserved strings (eg "yes", "no", "true", "false")
  • strings that look like dates
  • UUIDs that start with three leading numbers

Bug reproduction script:

# frozen_string_literal: true

# Use this template to report PaperTrail bugs.
# Please include only the minimum code necessary to reproduce your issue.
require "bundler/inline"

# STEP ONE: What versions are you using?
gemfile(true) do
  ruby "3.2.2"
  source "https://rubygems.org"
  gem "activerecord", "7.0.8"
  gem "minitest", "5.22.1"
  gem "paper_trail", "15.1.0", require: false
  gem "sqlite3", "1.7.2"
end

require "active_record"
require "minitest/autorun"
require "logger"

# Please use sqlite for your bug reports, if possible.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = nil
ActiveRecord::Schema.define do
  # STEP TWO: Define your tables here.
  create_table :users, force: true do |t|
    t.text :first_name, null: false
    t.timestamps null: false
  end

  create_table :versions do |t|
    t.string :item_type, null: false
    t.integer :item_id, null: false
    t.string :event, null: false
    t.string :whodunnit
    t.text :object, limit: 1_073_741_823
    t.text :object_changes, limit: 1_073_741_823
    t.datetime :created_at
  end
  add_index :versions, %i[item_type item_id]
end
ActiveRecord::Base.logger = Logger.new(STDOUT)
require "paper_trail"

# STEP FOUR: Define your AR models here.
class User < ActiveRecord::Base
  has_paper_trail
end

# STEP FIVE: Please write a test that demonstrates your issue.
class BugTest < ActiveSupport::TestCase

  def perform_using value
    assert_difference(-> { PaperTrail::Version.count }, +2) {
      assert_difference(-> { PaperTrail::Version.where_object(first_name: value).count }, +1) {
        user = User.create(first_name: value)
        user.destroy!
      }
    }
  end

  def test_using_regular_string
    # this one will pass
    perform_using "Grant"
  end

  def test_uuid_with_three_leading_numerals
    perform_using "018f696a-dcf7-4c25-91df-e79e578efd4f"
  end

  def test_with_string_no
    perform_using "no"
  end

  def test_with_date
    perform_using "2024-02-07"
  end

  def test_with_number
    perform_using "1234"
  end

  def test_with_decimal
    perform_using "12.34"
  end
end
Copy link

github-actions bot commented May 8, 2024

This issue has been automatically marked as stale due to inactivity.
The resources of our volunteers are limited.
Bug reports must provide a script that reproduces the bug, using our template. Feature suggestions must include a promise to build the feature yourself.
Thank you for all your contributions.

@github-actions github-actions bot added the Stale label May 8, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant