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

Version creation failure with binary fields and JSON serializer #1495

Open
3 tasks done
stac47 opened this issue Sep 23, 2024 · 0 comments
Open
3 tasks done

Version creation failure with binary fields and JSON serializer #1495

stac47 opened this issue Sep 23, 2024 · 0 comments

Comments

@stac47
Copy link

stac47 commented Sep 23, 2024

Thank you for your contribution!

Due to limited volunteers, issues that do not follow these instructions will be
closed without comment.

Check the following boxes:

  • This is not a usage question, this is a bug report
  • This bug can be reproduced with the script I provide below
  • This bug can be reproduced in the latest release of the paper_trail gem

Due to limited volunteers, we cannot answer usage questions. Please ask such
questions on StackOverflow.

Bug reports must use the following template:

# 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.3.5"
  source "https://rubygems.org"
  gem "activerecord", "~> 7.2"
  gem "minitest"
  gem "paper_trail", require: false
  gem "sqlite3"
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.binary :key, 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

PaperTrail.serializer = PaperTrail::Serializers::JSON

# STEP FIVE: Please write a test that demonstrates your issue.
class BugTest < ActiveSupport::TestCase
  def test_1
    assert_difference(-> { PaperTrail::Version.count }, +1) {
      User.create(key: SecureRandom.random_bytes)
    }
  end
end

# STEP SIX: Run this script using `ruby my_bug_report.rb`

Additional Inputs

The error is the following:

JSON::GeneratorError: source sequence is illegal/malformed utf-8

I see in the bug reports one mention in the past of someone who stumbled into the same issue but it was closed because the reporter could simply skip the binary columns.
I may be wrong but I feel this is a real use case: a user may want to reify a version of a record which contain some binary data. That's why I was a bit surprised that the object and object_changes serialization in JSON does not base64 automatically such binary data.
Is there any limitation preventing to handle that case out-of-the box?
Thanks in adavnce for your help,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant