Skip to content

Commit

Permalink
Merge pull request #17252 from imtayadeway/force-task-results-encoding
Browse files Browse the repository at this point in the history
Force UTF-8 encoding on task results
  • Loading branch information
gtanzillo committed Apr 6, 2018
2 parents 2471626 + 0559e91 commit c7d1580
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/models/miq_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,14 @@ def task_results
serializer_name = binary_blob.data_type
serializer_name = "Marshal" unless serializer_name == "YAML" # YAML or Marshal, for now
serializer = serializer_name.constantize
return serializer.load(binary_blob.binary)
result = serializer.load(binary_blob.binary)
return result.kind_of?(String) ? result.force_encoding("UTF-8") : result
end
nil
end

def task_results=(value)
value = value.force_encoding("UTF-8") if value.kind_of?(String)
self.binary_blob = BinaryBlob.new(:name => "task_results", :data_type => "YAML")
binary_blob.binary = YAML.dump(value)
end
Expand Down
23 changes: 23 additions & 0 deletions spec/models/miq_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,29 @@
end
end

describe "#task_results" do
it "forces UTF-8 encoding" do
task = FactoryGirl.create(
:miq_task,
:binary_blob => BinaryBlob.new(
:name => "task_results",
:data_type => "YAML",
:binary => YAML.dump("\xC3\xA4".force_encoding("ASCII-8BIT"))
)
)

expect(task.task_results).to eq("ä")
end
end

describe "#task_results=" do
it "forces UTF-8 encoding" do
task = FactoryGirl.create(:miq_task, :task_results => "\xC3\xA4".force_encoding("ASCII-8BIT"))

expect(task.task_results).to eq("ä")
end
end

private

def create_test_task(name, status, updated)
Expand Down

0 comments on commit c7d1580

Please sign in to comment.