Skip to content

Commit 5c90b83

Browse files
takaokoujiclaude
andcommitted
Fix Psych::DisallowedClass error for Rails 6.0 with Ruby 2.7+
Add Psych patch to allow loading Date, Time, DateTime classes from YAML fixtures in Rails 6.0 test environment. Rails 6.0 + Ruby 2.7 combination uses Psych 3.1+, which introduced stricter security for YAML.load. The default safe_load behavior rejects Date/Time/DateTime classes, causing test fixtures to fail. Solution: Patch Psych.load to use unsafe_load in test environment. This is safe since we're only loading trusted test fixture files. Fixes error: Psych::DisallowedClass: Tried to load unspecified class: Date Test result: Rails 6.0.6 now passes all 674 tests ✅ Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 44f9266 commit 5c90b83

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

test/test_helper.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ class TestApp < Rails::Application
7878
require 'jsonapi-resources'
7979
require 'pry'
8080

81+
# Fix Psych::DisallowedClass error for Rails 6.0 with Ruby 2.7+
82+
# In test environment, allow all classes from YAML (safe for test fixtures)
83+
if defined?(Psych::VERSION) && Psych::VERSION.to_f >= 3.1 && Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR == 0
84+
require 'psych'
85+
86+
# Patch Psych.load to use unsafe_load in test environment
87+
# This is safe because we're only loading trusted test fixtures
88+
module Psych
89+
class << self
90+
alias_method :safe_load_original, :load
91+
92+
def load(yaml, *args, **kwargs)
93+
# Use unsafe_load to allow Date, Time, DateTime from YAML
94+
unsafe_load(yaml)
95+
end
96+
end
97+
end
98+
end
99+
81100
require File.expand_path('../helpers/value_matchers', __FILE__)
82101
require File.expand_path('../helpers/assertions', __FILE__)
83102
require File.expand_path('../helpers/functional_helpers', __FILE__)

0 commit comments

Comments
 (0)