Skip to content

Commit

Permalink
Merge pull request #1093 from collectiveidea/fix-yaml-loading
Browse files Browse the repository at this point in the history
Fix for change in Psych loading
  • Loading branch information
albus522 committed Jun 19, 2019
2 parents e567824 + 7f993da commit 11e7b98
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/delayed/psych_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ def self.create
end

def visit_Psych_Nodes_Mapping(object) # rubocop:disable CyclomaticComplexity, MethodName, PerceivedComplexity
return revive(Psych.load_tags[object.tag], object) if Psych.load_tags[object.tag]
klass = Psych.load_tags[object.tag]
if klass
# Implementation changed here https://github.com/ruby/psych/commit/2c644e184192975b261a81f486a04defa3172b3f
# load_tags used to have class values, now the values are strings
klass = resolve_class(klass) if klass.is_a?(String)
return revive(klass, object)
end

case object.tag
when %r{^!ruby/object}
Expand Down
24 changes: 23 additions & 1 deletion spec/psych_ext_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,32 @@
describe 'Psych::Visitors::ToRuby', :if => defined?(Psych::Visitors::ToRuby) do
context BigDecimal do
it 'deserializes correctly' do
deserialized = YAML.load("--- !ruby/object:BigDecimal 18:0.1337E2\n...\n")
deserialized = YAML.load_dj("--- !ruby/object:BigDecimal 18:0.1337E2\n...\n")

expect(deserialized).to be_an_instance_of(BigDecimal)
expect(deserialized).to eq(BigDecimal('13.37'))
end
end

context 'load_tag handling' do
# This only broadly works in ruby 2.0 but will cleanly work through load_dj
# here because this class is so simple it only touches our extention
YAML.load_tags['!ruby/object:RenamedClass'] = SimpleJob
# This is how ruby 2.1 and newer works throughout the yaml handling
YAML.load_tags['!ruby/object:RenamedString'] = 'SimpleJob'

it 'deserializes class tag' do
deserialized = YAML.load_dj("--- !ruby/object:RenamedClass\ncheck: 12\n")

expect(deserialized).to be_an_instance_of(SimpleJob)
expect(deserialized.instance_variable_get(:@check)).to eq(12)
end

it 'deserializes string tag' do
deserialized = YAML.load_dj("--- !ruby/object:RenamedString\ncheck: 12\n")

expect(deserialized).to be_an_instance_of(SimpleJob)
expect(deserialized.instance_variable_get(:@check)).to eq(12)
end
end
end

0 comments on commit 11e7b98

Please sign in to comment.