Skip to content

Commit

Permalink
Remove all error masking when decoding serialized data fails?
Browse files Browse the repository at this point in the history
  • Loading branch information
mmangino committed Apr 2, 2013
1 parent a474396 commit 1bf6b53
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
21 changes: 8 additions & 13 deletions activerecord/lib/active_record/coders/yaml_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,15 @@ def dump(obj)
def load(yaml)
return object_class.new if object_class != Object && yaml.nil?
return yaml unless yaml.is_a?(String) && yaml =~ /^---/
begin
obj = YAML.load(yaml)

unless obj.is_a?(object_class) || obj.nil?
raise SerializationTypeMismatch,
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}"
end
obj ||= object_class.new if object_class != Object

obj
rescue ArgumentError , Psych::SyntaxError => e
raise if e.to_s =~ /undefined class/
yaml
obj = YAML.load(yaml)

unless obj.is_a?(object_class) || obj.nil?
raise SerializationTypeMismatch,
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}"
end
obj ||= object_class.new if object_class != Object

obj
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions activerecord/test/cases/coders/yaml_column_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ def test_load_handles_other_classes
assert_equal [], coder.load([])
end

def test_load_swallows_yaml_exceptions
def test_load_doesnt_swallow_yaml_exceptions
coder = YAMLColumn.new
bad_yaml = '--- {'
assert_equal bad_yaml, coder.load(bad_yaml)
assert_raises(Psych::SyntaxError) do
coder.load(bad_yaml)
end
end

def test_load_doesnt_handle_undefined_class_or_module
Expand Down

0 comments on commit 1bf6b53

Please sign in to comment.