diff --git a/ruby_event_store/lib/ruby_event_store/mappers/transformation/preserve_types.rb b/ruby_event_store/lib/ruby_event_store/mappers/transformation/preserve_types.rb index bfbc0a66a0..bf409395ef 100644 --- a/ruby_event_store/lib/ruby_event_store/mappers/transformation/preserve_types.rb +++ b/ruby_event_store/lib/ruby_event_store/mappers/transformation/preserve_types.rb @@ -91,8 +91,8 @@ def load(record) end data_types = types&.fetch(:data, nil) metadata_types = types&.fetch(:metadata, nil) - data = data_types ? restore_type(record.data, data_types) : record.data - metadata = metadata_types ? restore_type(record.metadata, metadata_types) : record.metadata + data = restore_type(record.data, data_types) + metadata = restore_type(record.metadata, metadata_types) Record.new( event_id: record.event_id, @@ -151,7 +151,7 @@ def restore_types(argument, types) end def restore_type(argument, type) - case argument + case type when Hash restore_types(argument, type) when Array diff --git a/ruby_event_store/spec/mappers/transformation/preserve_types_spec.rb b/ruby_event_store/spec/mappers/transformation/preserve_types_spec.rb index b48adf330f..d43b1ec964 100644 --- a/ruby_event_store/spec/mappers/transformation/preserve_types_spec.rb +++ b/ruby_event_store/spec/mappers/transformation/preserve_types_spec.rb @@ -351,6 +351,27 @@ module Transformation ensure Time.zone = current_tz end + + specify "preserves OpenStruct data type passed by stored type lambda" do + transformation = PreserveTypes.new.register( + OpenStruct, + serializer: ->(v) { v.to_h }, + deserializer: ->(v) { OpenStruct.new(v) } + ) + ostruct = OpenStruct.new(foo: "bar") + record = + Record.new( + event_id: uuid, + metadata: {}, + data: ostruct, + event_type: "TestEvent", + timestamp: nil, + valid_at: nil + ) + + expect(transformation.dump(record).metadata[:types]).to eq({ data: "OpenStruct", metadata: {} }) + expect(transformation.load(transformation.dump(record))).to eq(record) + end end end end