diff --git a/CHANGELOG.md b/CHANGELOG.md index c837711e..b46b1f6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/). ### Fixed -- None +- [#1338](https://github.com/paper-trail-gem/paper_trail/pull/1338) - + Support Psych version 4 ## 12.1.0 (2021-08-30) diff --git a/lib/paper_trail/serializers/yaml.rb b/lib/paper_trail/serializers/yaml.rb index 31ffc9d8..0530c990 100644 --- a/lib/paper_trail/serializers/yaml.rb +++ b/lib/paper_trail/serializers/yaml.rb @@ -9,7 +9,7 @@ module YAML extend self # makes all instance methods become module methods as well def load(string) - ::YAML.load string + ::YAML.respond_to?(:unsafe_load) ? ::YAML.unsafe_load(string) : ::YAML.load(string) end # @param object (Hash | HashWithIndifferentAccess) - Coming from diff --git a/spec/paper_trail/serializers/yaml_spec.rb b/spec/paper_trail/serializers/yaml_spec.rb index 92ef85a7..94aef5c6 100644 --- a/spec/paper_trail/serializers/yaml_spec.rb +++ b/spec/paper_trail/serializers/yaml_spec.rb @@ -22,6 +22,19 @@ module Serializers expect(described_class.load(hash.to_yaml)).to eq(hash) expect(described_class.load(array.to_yaml)).to eq(array) end + + it "calls the expected load method based on Psych version" do + # Psych 4+ implements .unsafe_load + if ::YAML.respond_to?(:unsafe_load) + allow(::YAML).to receive(:unsafe_load) + described_class.load("string") + expect(::YAML).to have_received(:unsafe_load) + else # Psych < 4 + allow(::YAML).to receive(:load) + described_class.load("string") + expect(::YAML).to have_received(:load) + end + end end describe ".dump" do