From 9aaf16479b09a86ad8197167b8f40d89146b3236 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 5 Mar 2026 12:38:46 +0100 Subject: [PATCH] Fix `allow_blank` parsing option to only consider strings. Ref: https://github.com/ruby/json/pull/946 --- CHANGES.md | 1 + lib/json/common.rb | 2 +- test/json/json_common_interface_test.rb | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index ceb17198..b225f4eb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,7 @@ ### Unreleased +* Fix `allow_blank` parsing option to no longer allow invalid types (e.g. `load([], allow_blank: true)` now raise a type error). * Add `allow_invalid_escape` parsing option to ignore backslashes that aren't followed by one of the valid escape characters. ### 2026-02-03 (2.18.1) diff --git a/lib/json/common.rb b/lib/json/common.rb index fe2dd522..230bf080 100644 --- a/lib/json/common.rb +++ b/lib/json/common.rb @@ -880,7 +880,7 @@ def load(source, proc = nil, options = nil) end end - if opts[:allow_blank] && (source.nil? || source.empty?) + if opts[:allow_blank] && (source.nil? || (String === source && source.empty?)) source = 'null' end diff --git a/test/json/json_common_interface_test.rb b/test/json/json_common_interface_test.rb index b6001e3f..37568b55 100644 --- a/test/json/json_common_interface_test.rb +++ b/test/json/json_common_interface_test.rb @@ -150,6 +150,8 @@ def test_load_null assert_equal nil, JSON.load(nil, nil, :allow_blank => true) assert_raise(TypeError) { JSON.load(nil, nil, :allow_blank => false) } assert_raise(JSON::ParserError) { JSON.load('', nil, :allow_blank => false) } + assert_raise(TypeError) { JSON.load([], nil, :allow_blank => true) } + assert_raise(TypeError) { JSON.load({}, nil, :allow_blank => true) } end def test_unsafe_load