From 1089144b54c1b7c0d6803d5d82b940c110bccb5f Mon Sep 17 00:00:00 2001 From: Tema Bolshakov Date: Sat, 6 Jul 2024 17:32:17 +0200 Subject: [PATCH] Replace URI.normalize_ref with URI2.normalize_ref --- lib/json-schema/attributes/ref.rb | 2 +- lib/json-schema/util/uri.rb | 27 ---------- lib/json-schema/util/uri2.rb | 36 +++++++++++++ test/uri2_util_test.rb | 85 +++++++++++++++++++++++++++++++ test/uri_util_test.rb | 85 ------------------------------- 5 files changed, 122 insertions(+), 113 deletions(-) diff --git a/lib/json-schema/attributes/ref.rb b/lib/json-schema/attributes/ref.rb index 665b9261..f21ff8b0 100644 --- a/lib/json-schema/attributes/ref.rb +++ b/lib/json-schema/attributes/ref.rb @@ -22,7 +22,7 @@ def self.validate(current_schema, data, fragments, processor, validator, options def self.get_referenced_uri_and_schema(s, current_schema, validator) uri, schema = nil, nil - temp_uri = JSON::Util::URI.normalize_ref(s['$ref'], current_schema.uri) + temp_uri = JSON::Util::URI2.normalize_ref(s['$ref'], current_schema.uri) # Grab the parent schema from the schema list schema_key = temp_uri.to_s.split('#')[0] + '#' diff --git a/lib/json-schema/util/uri.rb b/lib/json-schema/util/uri.rb index ffdfc6bf..c240452c 100644 --- a/lib/json-schema/util/uri.rb +++ b/lib/json-schema/util/uri.rb @@ -25,33 +25,6 @@ def absolutize_ref(ref, base) URI2.normalize_uri(uri) end - def normalize_ref(ref, base) - ref_uri = parse(ref) - base_uri = parse(base) - - ref_uri.defer_validation do - if ref_uri.relative? - ref_uri.merge!(base_uri) - - # Check for absolute path - path, fragment = ref.to_s.split('#') - if path.nil? || path == '' - ref_uri.path = base_uri.path - elsif path[0, 1] == '/' - ref_uri.path = Pathname.new(path).cleanpath.to_s - else - ref_uri.join!(path) - end - - ref_uri.fragment = fragment - end - - ref_uri.fragment = '' if ref_uri.fragment.nil? || ref_uri.fragment.empty? - end - - ref_uri - end - def parse(uri) if uri.is_a?(Addressable::URI) uri.dup diff --git a/lib/json-schema/util/uri2.rb b/lib/json-schema/util/uri2.rb index 78b8c24b..2d7d4540 100644 --- a/lib/json-schema/util/uri2.rb +++ b/lib/json-schema/util/uri2.rb @@ -38,6 +38,15 @@ def strip_fragment(uri) def normalize_uri(uri, base_path = Dir.pwd) parse(uri).normalize_uri(base_path) end + + # Normalizes the reference URI based on the provided base URI + # + # @param ref [String, Addressable::URI] + # @param base [String, Addressable::URI] + # @return [Addressable::URI] + def normalize_ref(ref, base) + parse(ref).normalize_ref(base) + end end # Unencodes any percent encoded characters within a path component. @@ -72,6 +81,33 @@ def normalize_uri(base_path = Dir.pwd) dup end end + + # @param base [Addressable::URI, String] + # @return [Addressable::URI] + def normalize_ref(base) + base_uri = self.class.parse(base) + defer_validation do + if relative? + # Check for absolute path + path, fragment = to_s.split('#') + merge!(base_uri) + + if path.nil? || path == '' + self.path = base_uri.path + elsif path[0, 1] == '/' + self.path = Pathname.new(path).cleanpath.to_s + else + join!(path) + end + + self.fragment = fragment + end + + self.fragment = '' if self.fragment.nil? || self.fragment.empty? + end + + self + end end end end diff --git a/test/uri2_util_test.rb b/test/uri2_util_test.rb index 64e18057..bef2a3e5 100644 --- a/test/uri2_util_test.rb +++ b/test/uri2_util_test.rb @@ -50,4 +50,89 @@ def test_normalized_uri_for_file_path_with_host path: '/foo/bar.json',) assert_equal uri, JSON::Util::URI2.normalize_uri(str, '/home') end + + def test_ref_fragment_path + uri = '#some-thing' + base = 'http://www.example.com/foo/#bar' + + assert_equal Addressable::URI.parse('http://www.example.com/foo/#some-thing'), JSON::Util::URI2.normalize_ref(uri, base) + # assert_equal Addressable::URI.parse('http://www.example.com/foo/#'), JSON::Util::URI.absolutize_ref(uri, base) + end + + def test_ref_file_path + uri = '/some/thing' + base = 'http://www.example.com/foo/#bar' + + assert_equal Addressable::URI.parse('http://www.example.com/some/thing#'), JSON::Util::URI2.normalize_ref(uri, base) + # assert_equal Addressable::URI.parse('http://www.example.com/some/thing#'), JSON::Util::URI.absolutize_ref(uri, base) + end + + def test_ref_uri + uri = 'http://foo-bar.com' + base = 'http://www.example.com/foo/#bar' + + assert_equal Addressable::URI.parse('http://foo-bar.com/#'), JSON::Util::URI2.normalize_ref(uri, base) + # assert_equal Addressable::URI.parse('http://foo-bar.com/#'), JSON::Util::URI.absolutize_ref(uri, base) + end + + def test_ref_uri_with_path + uri = 'http://foo-bar.com/some/thing' + base = 'http://www.example.com/foo/#bar' + + assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#'), JSON::Util::URI2.normalize_ref(uri, base) + # assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#'), JSON::Util::URI.absolutize_ref(uri, base) + end + + def test_ref_uri_with_fragment + uri = 'http://foo-bar.com/some/thing#foo' + base = 'http://www.example.com/hello/#world' + + assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#foo'), JSON::Util::URI2.normalize_ref(uri, base) + # assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#'), JSON::Util::URI.absolutize_ref(uri, base) + end + + def test_ref_uri_with_fragment_and_base_with_no_fragment + uri = 'http://foo-bar.com/some/thing#foo' + base = 'http://www.example.com/hello' + + assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#foo'), JSON::Util::URI2.normalize_ref(uri, base) + # assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#'), JSON::Util::URI.absolutize_ref(uri, base) + end + + def test_ref_relative_path + uri = 'hello/world' + base = 'http://www.example.com/foo/#bar' + + # assert_equal Addressable::URI.parse('http://www.example.com/foo/hello/world#'), JSON::Util::URI2.normalize_ref(uri, base) + assert_equal Addressable::URI.parse('http://www.example.com/foo/hello/world#'), JSON::Util::URI.absolutize_ref(uri, base) + end + + def test_ref_addressable_uri_with_host + uri = Addressable::URI.new(host: 'foo-bar.com') + base = 'http://www.example.com/hello/#world' + + assert_equal Addressable::URI.parse('http://www.example.com/foo-bar.com#'), JSON::Util::URI2.normalize_ref(uri, base) + # assert_equal Addressable::URI.parse('http://www.example.com/hello/#world'), JSON::Util::URI.absolutize_ref(uri, base) + end + + def test_ref_addressable_uri_with_host_and_path + uri = Addressable::URI.new(host: 'foo-bar.com', path: '/hello/world') + base = 'http://www.example.com/a/#b' + + assert_equal Addressable::URI.parse('http://www.example.com/foo-bar.com/hello/world#'), JSON::Util::URI2.normalize_ref(uri, base) + # assert_equal Addressable::URI.parse('http://www.example.com/hello/world'), JSON::Util::URI.absolutize_ref(uri, base) + end + + def test_ref_addressable_uri_with_scheme_host_and_path + uri = Addressable::URI.new(scheme: 'https', host: 'foo-bar.com', path: '/hello/world') + base = 'http://www.example.com/a/#b' + + assert_equal Addressable::URI.parse('https://foo-bar.com/hello/world#'), JSON::Util::URI2.normalize_ref(uri, base) + # assert_equal Addressable::URI.parse('https://foo-bar.com/hello/world'), JSON::Util::URI.absolutize_ref(uri, base) + end + + def test_normalize_ref_cache + assert_equal Addressable::URI.parse('http://www.example.com/#foo'), JSON::Util::URI2.normalize_ref('#foo', 'http://www.example.com') + assert_equal Addressable::URI.parse('http://www.example.net/#foo'), JSON::Util::URI2.normalize_ref('#foo', 'http://www.example.net') + end end diff --git a/test/uri_util_test.rb b/test/uri_util_test.rb index 26b7d7e0..1edc8c83 100644 --- a/test/uri_util_test.rb +++ b/test/uri_util_test.rb @@ -49,89 +49,4 @@ def test_validator_clear_cache_for_parse refute_equal(cached_uri, JSON::Util::URI.parse('foo')) end - - def test_ref_fragment_path - uri = '#some-thing' - base = 'http://www.example.com/foo/#bar' - - assert_equal Addressable::URI.parse('http://www.example.com/foo/#some-thing'), JSON::Util::URI.normalize_ref(uri, base) - assert_equal Addressable::URI.parse('http://www.example.com/foo/#'), JSON::Util::URI.absolutize_ref(uri, base) - end - - def test_ref_file_path - uri = '/some/thing' - base = 'http://www.example.com/foo/#bar' - - assert_equal Addressable::URI.parse('http://www.example.com/some/thing#'), JSON::Util::URI.normalize_ref(uri, base) - assert_equal Addressable::URI.parse('http://www.example.com/some/thing#'), JSON::Util::URI.absolutize_ref(uri, base) - end - - def test_ref_uri - uri = 'http://foo-bar.com' - base = 'http://www.example.com/foo/#bar' - - assert_equal Addressable::URI.parse('http://foo-bar.com/#'), JSON::Util::URI.normalize_ref(uri, base) - assert_equal Addressable::URI.parse('http://foo-bar.com/#'), JSON::Util::URI.absolutize_ref(uri, base) - end - - def test_ref_uri_with_path - uri = 'http://foo-bar.com/some/thing' - base = 'http://www.example.com/foo/#bar' - - assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#'), JSON::Util::URI.normalize_ref(uri, base) - assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#'), JSON::Util::URI.absolutize_ref(uri, base) - end - - def test_ref_uri_with_fragment - uri = 'http://foo-bar.com/some/thing#foo' - base = 'http://www.example.com/hello/#world' - - assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#foo'), JSON::Util::URI.normalize_ref(uri, base) - assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#'), JSON::Util::URI.absolutize_ref(uri, base) - end - - def test_ref_uri_with_fragment_and_base_with_no_fragment - uri = 'http://foo-bar.com/some/thing#foo' - base = 'http://www.example.com/hello' - - assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#foo'), JSON::Util::URI.normalize_ref(uri, base) - assert_equal Addressable::URI.parse('http://foo-bar.com/some/thing#'), JSON::Util::URI.absolutize_ref(uri, base) - end - - def test_ref_relative_path - uri = 'hello/world' - base = 'http://www.example.com/foo/#bar' - - assert_equal Addressable::URI.parse('http://www.example.com/foo/hello/world#'), JSON::Util::URI.normalize_ref(uri, base) - assert_equal Addressable::URI.parse('http://www.example.com/foo/hello/world#'), JSON::Util::URI.absolutize_ref(uri, base) - end - - def test_ref_addressable_uri_with_host - uri = Addressable::URI.new(host: 'foo-bar.com') - base = 'http://www.example.com/hello/#world' - - assert_equal Addressable::URI.parse('http://www.example.com/foo-bar.com#'), JSON::Util::URI.normalize_ref(uri, base) - assert_equal Addressable::URI.parse('http://www.example.com/hello/#world'), JSON::Util::URI.absolutize_ref(uri, base) - end - - def test_ref_addressable_uri_with_host_and_path - uri = Addressable::URI.new(host: 'foo-bar.com', path: '/hello/world') - base = 'http://www.example.com/a/#b' - - assert_equal Addressable::URI.parse('http://www.example.com/foo-bar.com/hello/world#'), JSON::Util::URI.normalize_ref(uri, base) - assert_equal Addressable::URI.parse('http://www.example.com/hello/world'), JSON::Util::URI.absolutize_ref(uri, base) - end - - def test_ref_addressable_uri_with_scheme_host_and_path - uri = Addressable::URI.new(scheme: 'https', host: 'foo-bar.com', path: '/hello/world') - base = 'http://www.example.com/a/#b' - - assert_equal Addressable::URI.parse('https://foo-bar.com/hello/world#'), JSON::Util::URI.normalize_ref(uri, base) - assert_equal Addressable::URI.parse('https://foo-bar.com/hello/world'), JSON::Util::URI.absolutize_ref(uri, base) - end - - def test_normalize_ref_cache - assert_equal Addressable::URI.parse('http://www.example.com/#foo'), JSON::Util::URI.normalize_ref('#foo', 'http://www.example.com') - assert_equal Addressable::URI.parse('http://www.example.net/#foo'), JSON::Util::URI.normalize_ref('#foo', 'http://www.example.net') - end end