Skip to content

Commit 3f711c9

Browse files
authored
Merge pull request #2111 from yahonda/backport_2097_to_release61
Fix write_lobs Invalid byte sequence in UTF-8 (#2097)
2 parents 4502b6f + b202639 commit 3f711c9

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/active_record/connection_adapters/oracle_enhanced/database_statements.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,11 @@ def write_lobs(table_name, klass, attributes, columns) #:nodoc:
254254
columns.each do |col|
255255
value = attributes[col.name]
256256
# changed sequence of next two lines - should check if value is nil before converting to yaml
257-
next if value.blank?
257+
next unless value
258258
if klass.attribute_types[col.name].is_a? Type::Serialized
259259
value = klass.attribute_types[col.name].serialize(value)
260+
# value can be nil after serialization because ActiveRecord serializes [] and {} as nil
261+
next unless value
260262
end
261263
uncached do
262264
unless lob_record = select_one(sql = <<~SQL.squish, "Writable Large Object")

spec/active_record/connection_adapters/oracle_enhanced_adapter_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,45 @@ class ::TestSerializedColumn < ActiveRecord::Base
479479
end
480480
end
481481

482+
describe "Binary lob column" do
483+
before(:all) do
484+
schema_define do
485+
create_table :test_binary_columns do |t|
486+
t.binary :attachment
487+
end
488+
end
489+
class ::TestBinaryColumn < ActiveRecord::Base
490+
end
491+
end
492+
493+
after(:all) do
494+
schema_define do
495+
drop_table :test_binary_columns
496+
end
497+
Object.send(:remove_const, "TestBinaryColumn")
498+
ActiveRecord::Base.table_name_prefix = nil
499+
ActiveRecord::Base.clear_cache!
500+
end
501+
502+
before(:each) do
503+
set_logger
504+
end
505+
506+
after(:each) do
507+
clear_logger
508+
end
509+
510+
it "should serialize with non UTF-8 data" do
511+
binary_value = +"Hello \x93\xfa\x96\x7b"
512+
binary_value.force_encoding "UTF-8"
513+
514+
binary_column_object = TestBinaryColumn.new
515+
binary_column_object.attachment = binary_value
516+
517+
expect(binary_column_object.save!).to eq(true)
518+
end
519+
end
520+
482521
describe "quoting" do
483522
before(:all) do
484523
schema_define do

0 commit comments

Comments
 (0)