Skip to content

Commit

Permalink
Fix regression in serialization of nil/NULL values
Browse files Browse the repository at this point in the history
Regression introduced in #5012e0d6. This change partially reverts the
change such that it works for Rails 6.1/7.0 and 7.1+.

In Rails 7.1+ the API for `.serialize` changed. At first using
`serialize :parameters, coder: YAML, type: Hash` seems to be backwards
compatible with Rails 6.1/7.0 when reading the documentation/code but
there’s a subtle difference: when the 2nd positional parameter
(denoting the target type) is not provided a default mechanism is used.
While the defaults will use `YAML` as the coder the serialization will
only occur if the column is not `NULL`. So, without an explicit type
parameter `nil`/`NULL` values are not converted to `Hash`. Thus, the
explicit type parameter acts as the fallback for the value itself.

Fixes #386 #384 #382
  • Loading branch information
ur5us committed Feb 14, 2024
1 parent b56f603 commit 2e43ea4
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/public_activity/orm/active_record/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ class Activity < ::ActiveRecord::Base
# Serialize parameters Hash
begin
if table_exists?
serialize :parameters, coder: YAML, type: Hash unless %i[json jsonb hstore].include?(columns_hash['parameters'].type)
unless %i[json jsonb hstore].include?(columns_hash['parameters'].type)
if ::ActiveRecord.version.release < Gem::Version.new('7.1')
serialize :parameters, Hash
else
serialize :parameters, coder: YAML, type: Hash
end
end
else
warn("[WARN] table #{name} doesn't exist. Skipping PublicActivity::Activity#parameters's serialization")
end
Expand Down

0 comments on commit 2e43ea4

Please sign in to comment.