Skip to content

Commit

Permalink
Use RFC2396_PARSER instead of DEFAULT_PARSER
Browse files Browse the repository at this point in the history
`URI::DEFAULT_PARSER` switched from `URI::RFC2396_PARSER` to
`URI::RFC3986_PARSER`, which complains about escape/unescape:

> /Users/dharsha/repos/json_schemer/lib/json_schemer.rb:116: warning: URI::RFC3986_PARSER.unescape is obsolete. Use URI::RFC2396_PARSER.unescape explicitly.
> /Users/dharsha/repos/json_schemer/lib/json_schemer.rb:254: warning: URI::RFC3986_PARSER.escape is obsolete. Use URI::RFC2396_PARSER.escape explicitly.

This switches back to `URI::RFC2396_PARSER` with a fallback to
`URI::DEFAULT_PARSER` for older Ruby versions.

Related:
- https://bugs.ruby-lang.org/issues/19266
- ruby/uri#107
  • Loading branch information
davishmcclurg committed Jan 21, 2025
1 parent 03193ce commit 5d80c01
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/json_schemer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ class InvalidEcmaRegexp < StandardError; end

WINDOWS_URI_PATH_REGEX = /\A\/[a-z]:/i

# :nocov:
URI_PARSER = URI.const_defined?(:RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::DEFAULT_PARSER
# :nocov:

FILE_URI_REF_RESOLVER = proc do |uri|
raise InvalidFileURI, 'must use `file` scheme' unless uri.scheme == 'file'
raise InvalidFileURI, 'cannot have a host (use `file:///`)' if uri.host && !uri.host.empty?
path = uri.path
path = path[1..-1] if path.match?(WINDOWS_URI_PATH_REGEX)
JSON.parse(File.read(URI::DEFAULT_PARSER.unescape(path)))
JSON.parse(File.read(URI_PARSER.unescape(path)))
end

class << self
Expand Down Expand Up @@ -247,7 +251,7 @@ def resolve(schema, options)
when String
JSON.parse(schema)
when Pathname
base_uri = URI.parse(File.join('file:', URI::DEFAULT_PARSER.escape(schema.realpath.to_s)))
base_uri = URI.parse(File.join('file:', URI_PARSER.escape(schema.realpath.to_s)))
options[:base_uri] = base_uri
if options.key?(:ref_resolver)
FILE_URI_REF_RESOLVER.call(base_uri)
Expand Down
2 changes: 1 addition & 1 deletion test/output_format_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def test_it_escapes_absolute_keyword_location
)

uri = URI(absolute_keyword_location)
json_pointer = URI::DEFAULT_PARSER.unescape(uri.fragment)
json_pointer = JSONSchemer::URI_PARSER.unescape(uri.fragment)
assert_equal(
'/patternProperties/^(a[b]{2}c|#%z\\"<>`|[\\-_.!~0*\'();~1?:@&=+$,])/not',
json_pointer
Expand Down

0 comments on commit 5d80c01

Please sign in to comment.