Skip to content

Fix psych disallowedclass error in ruby 3.1 #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/rspec/openapi/schema_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'fileutils'
require 'yaml'
require 'json'
require 'date'
require 'time'
Comment on lines +6 to +7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nits]
require 'yaml' also loads Date. Time is available without requiring anything explicitly.


# TODO: Support JSON
class RSpec::OpenAPI::SchemaFile
Expand All @@ -24,7 +26,12 @@ def edit(&block)
def read
return {} unless File.exist?(@path)

RSpec::OpenAPI::KeyTransformer.symbolize(YAML.safe_load(File.read(@path))) # this can also parse JSON
RSpec::OpenAPI::KeyTransformer.symbolize(
YAML.safe_load(
File.read(@path),
permitted_classes: [Date, Time],
),
) # this can also parse JSON
end

# @param [Hash] spec
Expand Down
41 changes: 41 additions & 0 deletions spec/rspec/schema_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe RSpec::OpenAPI::SchemaFile do
describe '#read' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[imho]
How about adding unquoted dates or times to the OpenAPI schema in spec/apps/ instead of writing tests for the private method?

What should be tested is not whether unquoted dates or times are loaded, but whether the resulting OpenAPI schema output is correct—and whether it can be successfully loaded.

let(:schema_content) do
<<~YAML
openapi: 3.0.0
info:
title: My API
version: 1.0.0
paths:
/:
get:
summary: A test endpoint
parameters:
- name: date
in: query
schema:
type: string
date: 2020-01-02 # Unquoted date
time: 2025-06-10 01:47:28Z
YAML
end

it 'deserializes unquoted dates as Date objects when Date is permitted' do
schema_file = RSpec::OpenAPI::SchemaFile.new('nonexistant/schema.yaml')

expect(File).to receive(:read).and_return(schema_content)
expect(File).to receive(:exist?).and_return(true)

data = nil
expect do
data = schema_file.send(:read)
end.not_to raise_error(Psych::DisallowedClass)
expect(data.dig(:paths, :/, :get, :parameters, 0, :schema, :date).to_s).to eq('2020-01-02')
expect(data.dig(:paths, :/, :get, :parameters, 0, :schema, :time).to_s).to eq('2025-06-10 01:47:28 UTC')
end
end
end