Skip to content

Commit

Permalink
Fix a broken spec due to Psych >= 4
Browse files Browse the repository at this point in the history
Since psych4.0, the load method has been safe_load, which causes the following error: YAML#load_dj retains the existing safeYAML support, but uses the version of psych that implements unsafe_load. In YAML#load_dj

```
  1) YAML autoloads the class of an anonymous struct
     Failure/Error:
       expect do
         yaml = "--- !ruby/struct\nn: 1\n"
         object = YAML.load(yaml)
         expect(object).to be_kind_of(Struct)
         expect(object.n).to eq(1)
       end.not_to raise_error

       expected no Exception, got #<Psych::DisallowedClass: Tried to load unspecified class: Struct> with backtrace:
         # (eval):2:in `struct'
         # ./spec/yaml_ext_spec.rb:28:in `block (3 levels) in <top (required)>'
         # ./spec/yaml_ext_spec.rb:26:in `block (2 levels) in <top (required)>'
     # ./spec/yaml_ext_spec.rb:26:in `block (2 levels) in <top (required)>'
```
  • Loading branch information
willnet committed Oct 25, 2021
1 parent 0ce29d1 commit b4ddd3d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/delayed/syck_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ def self.yaml_tag_read_class(name)
module YAML
def load_dj(yaml)
# See https://github.com/dtao/safe_yaml
# When the method is there, we need to load our YAML like this...
respond_to?(:unsafe_load) ? load(yaml, :safe => false) : load(yaml)
# When the module is there, we need to load our YAML like this...
if Object.const_defined?(:SafeYAML)
load(yaml, :safe => false)
elsif respond_to?(:unsafe_load) # psych >= 3.3.2
unsafe_load(yaml)
end
end
end
2 changes: 1 addition & 1 deletion spec/yaml_ext_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
it 'autoloads the class of an anonymous struct' do
expect do
yaml = "--- !ruby/struct\nn: 1\n"
object = YAML.load(yaml)
object = load_with_delayed_visitor(yaml)
expect(object).to be_kind_of(Struct)
expect(object.n).to eq(1)
end.not_to raise_error
Expand Down

0 comments on commit b4ddd3d

Please sign in to comment.