Skip to content

Commit 83671ec

Browse files
committed
Make double-serialization-detection influence local to the instance
1 parent e58124c commit 83671ec

File tree

4 files changed

+26
-32
lines changed

4 files changed

+26
-32
lines changed

ruby_event_store-active_record/.mutant.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ matcher:
3434
- RubyEventStore::ActiveRecord::EventRepositoryReader#initialize
3535
- RubyEventStore::ActiveRecord::EventRepository#initialize
3636
- RubyEventStore::ActiveRecord::EventRepository#json_data_type?
37-
- RubyEventStore::ActiveRecord::DoubleSerializationDetector
37+
- RubyEventStore::ActiveRecord::EventRepository#rescue_from_double_json_serialization!
3838
- RubyEventStore::ActiveRecord::SkipJsonSerialization

ruby_event_store-active_record/lib/ruby_event_store/active_record.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
require_relative "active_record/event_repository_reader"
1515
require_relative "active_record/index_violation_detector"
1616
require_relative "active_record/pg_linearized_event_repository"
17-
require_relative "active_record/double_serialization_detector"
1817
require_relative "active_record/version"
1918
if defined?(Rails::Engine)
2019
require_relative "active_record/railtie"

ruby_event_store-active_record/lib/ruby_event_store/active_record/double_serialization_detector.rb

Lines changed: 0 additions & 29 deletions
This file was deleted.

ruby_event_store-active_record/lib/ruby_event_store/active_record/event_repository.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,31 @@ def initialize(model_factory: WithDefaultModels.new, serializer:)
3636
end
3737

3838
def rescue_from_double_json_serialization!
39-
EventRepositoryReader.prepend(DoubleSerializationDetector) if serializer == JSON && json_data_type?
39+
if serializer == JSON && json_data_type?
40+
@repo_reader.instance_eval { alias __record__ record }
41+
42+
@repo_reader.define_singleton_method :unwrap do |column_name, payload|
43+
if String === payload && payload.start_with?("\{")
44+
warn "Double serialization of #{column_name} column detected"
45+
serializer.load(payload)
46+
else
47+
payload
48+
end
49+
end
50+
51+
@repo_reader.define_singleton_method :record do |record|
52+
r = __record__(record)
53+
54+
Record.new(
55+
event_id: r.event_id,
56+
metadata: unwrap("metadata", r.metadata),
57+
data: unwrap("data", r.data),
58+
event_type: r.event_type,
59+
timestamp: r.timestamp,
60+
valid_at: r.valid_at,
61+
)
62+
end
63+
end
4064
end
4165

4266
def append_to_stream(records, stream, expected_version)

0 commit comments

Comments
 (0)