Skip to content

Commit

Permalink
Replace URI.normalize_ref with URI2.normalize_ref
Browse files Browse the repository at this point in the history
  • Loading branch information
bolshakov authored and bastelfreak committed Aug 19, 2024
1 parent a9b6c7f commit 1089144
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 113 deletions.
2 changes: 1 addition & 1 deletion lib/json-schema/attributes/ref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] + '#'
Expand Down
27 changes: 0 additions & 27 deletions lib/json-schema/util/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions lib/json-schema/util/uri2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
85 changes: 85 additions & 0 deletions test/uri2_util_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
85 changes: 0 additions & 85 deletions test/uri_util_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1089144

Please sign in to comment.