Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/audited/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ class YAMLIfTextColumnType
class << self
def load(obj)
if text_column?
ActiveRecord::Coders::YAMLColumn.new(Object).load(obj)
begin
ActiveRecord::Coders::YAMLColumn.new(Object).load(obj)
rescue Psych::SyntaxError
obj = obj.gsub("=>", ":")
ActiveRecord::Coders::YAMLColumn.new(Object).load(obj)
end
Comment on lines +22 to +27
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, Can we do it this way? To avoid parsing it twice?

Suggested change
begin
ActiveRecord::Coders::YAMLColumn.new(Object).load(obj)
rescue Psych::SyntaxError
obj = obj.gsub("=>", ":")
ActiveRecord::Coders::YAMLColumn.new(Object).load(obj)
end
begin
obj = obj.gsub("=>", ":")
ActiveRecord::Coders::YAMLColumn.new(Object).load(obj)
end

Second, what happens if we have a value like this `"{"a" => "some value include =>"}"? Could you test it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I don't want to modify a correct JSON parseable string which contains => for eq "{"a": "sample=>text"}"
  2. I don't think "{"a" => "some value include =>"}" is possible as the incorrect data also has pattern which is hash.to_s

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I don't want to modify a correct JSON parseable string which contains => for eq "{"a": "sample=>text"}"
  2. I don't think "{"a" => "some value include =>"}" is possible as the incorrect data also has pattern which is hash.to_s

why is "sample=>text" possible in the first point and not possible in the 2nd point? It should be possible in both, that's why we should find a safer way to replace "=>" with ":" in both cases.

Consider the following:

"{"a": "value of a has a =>"}"
"{"a" => "value of a has a =>"}"

both should be parsed as

"{"a": "value of a has a =>"}"
"{"a": "value of a has a =>"}"

but now, they are parsed as:

"{"a": "value of a has a =>"}"
"{"a": "value of a has a :"}" # this is wrong

I don't want to modify a correct JSON

If a json is already correct, then the proper replace function should not modify it. If a json contains "=>" in its value, we should not replace it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a proper test coverage would be better

else
obj
end
Expand Down