diff --git a/lib/json-schema/attributes/ref.rb b/lib/json-schema/attributes/ref.rb index 895c38ae..cfe52666 100644 --- a/lib/json-schema/attributes/ref.rb +++ b/lib/json-schema/attributes/ref.rb @@ -32,7 +32,7 @@ def self.get_referenced_uri_and_schema(s, current_schema, validator) if ref_schema # Perform fragment resolution to retrieve the appropriate level for the schema target_schema = ref_schema.schema - fragments = JSON::Util::URI.parse(JSON::Util::URI.unescape(temp_uri)).fragment.split('/') + fragments = JSON::Util::URI.parse(JSON::Util::URI.unescape_uri(temp_uri)).fragment.split('/') fragment_path = '' fragments.each do |fragment| if fragment && fragment != '' diff --git a/lib/json-schema/schema/reader.rb b/lib/json-schema/schema/reader.rb index 76e389a8..6bf6469e 100644 --- a/lib/json-schema/schema/reader.rb +++ b/lib/json-schema/schema/reader.rb @@ -130,7 +130,7 @@ def read_uri(uri) def read_file(pathname) if accept_file?(pathname) - File.read(JSON::Util::URI.unescape_path(pathname.to_s)) + File.read(JSON::Util::URI.unescaped_path(pathname.to_s)) else raise JSON::Schema::ReadRefused.new(pathname.to_s, :file) end diff --git a/lib/json-schema/util/uri.rb b/lib/json-schema/util/uri.rb index fad8e709..b6b3be3d 100644 --- a/lib/json-schema/util/uri.rb +++ b/lib/json-schema/util/uri.rb @@ -8,6 +8,8 @@ class URI < Addressable::URI SUPPORTED_PROTOCOLS = %w(http https ftp tftp sftp ssh svn+ssh telnet nntp gopher wais ldap prospero) class << self + alias unescape_uri unescape + # @param uri [String, Addressable::URI] # @return [Addressable::URI, nil] def parse(uri) @@ -24,8 +26,8 @@ def file_uri(uri) # @param uri [String, Addressable::URI # @return [String] - def unescape_path(uri) - parse(uri).unescape_path + def unescaped_path(uri) + parse(uri).unescaped_path end # Strips the fragment from the URI. @@ -37,8 +39,8 @@ def strip_fragment(uri) # @param uri [String, Addressable::URI] # @return [Addressable::URI] - def normalize_uri(uri, base_path = Dir.pwd) - parse(uri).normalize_uri(base_path) + def normalized_uri(uri, base_path = Dir.pwd) + parse(uri).normalized_uri(base_path) end # Normalizes the reference URI based on the provided base URI @@ -58,7 +60,7 @@ def absolutize_ref(ref, base) # Unencodes any percent encoded characters within a path component. # # @return [String] - def unescape_path + def unescaped_path self.class.unescape_component(path) end @@ -76,7 +78,7 @@ def strip_fragment # # @param base_path [String] the base path to use for relative URIs. Defaults to the current working directory. # @return [Addressable::URI] the normalized URI or nil - def normalize_uri(base_path = Dir.pwd) + def normalized_uri(base_path = Dir.pwd) if relative? if path[0, 1] == '/' self.class.file_uri(self) @@ -122,7 +124,7 @@ def absolutize_ref(base) if ref.absolute? ref else - self.class.strip_fragment(base).join(ref.path).normalize_uri + self.class.strip_fragment(base).join(ref.path).normalized_uri end end end diff --git a/lib/json-schema/validator.rb b/lib/json-schema/validator.rb index 63127df4..0a4d3b87 100644 --- a/lib/json-schema/validator.rb +++ b/lib/json-schema/validator.rb @@ -321,7 +321,7 @@ def schema_loaded?(schema_uri) end def schema_key_for(uri) - key = Util::URI.normalize_uri(uri).to_s + key = Util::URI.normalized_uri(uri).to_s key.end_with?('#') ? key : "#{key}#" end @@ -534,7 +534,7 @@ def initialize_schema(schema, default_validator) self.class.add_schema(schema) rescue JSON::Schema::JsonParseError # Build a uri for it - schema_uri = Util::URI.normalize_uri(schema) + schema_uri = Util::URI.normalized_uri(schema) if !self.class.schema_loaded?(schema_uri) schema = @options[:schema_reader].read(schema_uri) schema = JSON::Schema.stringify(schema) @@ -574,7 +574,7 @@ def initialize_data(data) if @options[:json] data = self.class.parse(data) elsif @options[:uri] - json_uri = Util::URI.normalize_uri(data) + json_uri = Util::URI.normalized_uri(data) data = self.class.parse(custom_open(json_uri)) elsif data.is_a?(String) begin @@ -583,7 +583,7 @@ def initialize_data(data) data = strict_convert ? data : self.class.parse(data) rescue JSON::Schema::JsonParseError begin - json_uri = Util::URI.normalize_uri(data) + json_uri = Util::URI.normalized_uri(data) data = self.class.parse(custom_open(json_uri)) rescue JSON::Schema::JsonLoadError, JSON::Schema::UriError # Silently discard the error - use the data as-is @@ -595,7 +595,7 @@ def initialize_data(data) end def custom_open(uri) - uri = Util::URI.normalize_uri(uri) if uri.is_a?(String) + uri = Util::URI.normalized_uri(uri) if uri.is_a?(String) if uri.absolute? && Util::URI::SUPPORTED_PROTOCOLS.include?(uri.scheme) begin URI.open(uri.to_s).read @@ -604,7 +604,7 @@ def custom_open(uri) end else begin - File.read(JSON::Util::URI.unescape_path(uri)) + File.read(JSON::Util::URI.unescaped_path(uri)) rescue SystemCallError => e raise JSON::Schema::JsonLoadError, e.message end diff --git a/test/uri_util_test.rb b/test/uri_util_test.rb index 40dd031a..2c66d10f 100644 --- a/test/uri_util_test.rb +++ b/test/uri_util_test.rb @@ -6,7 +6,7 @@ def test_normalized_uri uri = Addressable::URI.new(scheme: 'https', host: 'www.google.com', path: 'search',) - assert_equal uri, JSON::Util::URI.normalize_uri(str, '/home') + assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home') end def test_normalized_uri_with_empty_fragment @@ -15,7 +15,7 @@ def test_normalized_uri_with_empty_fragment host: 'www.google.com', path: 'search', fragment: nil,) - assert_equal uri, JSON::Util::URI.normalize_uri(str, '/home') + assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home') end def test_normalized_uri_with_fragment @@ -24,7 +24,7 @@ def test_normalized_uri_with_fragment host: 'www.google.com', path: 'search', fragment: 'foo',) - assert_equal uri, JSON::Util::URI.normalize_uri(str, '/home') + assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home') end def test_normalized_uri_for_absolute_path @@ -32,7 +32,7 @@ def test_normalized_uri_for_absolute_path uri = Addressable::URI.new(scheme: 'file', host: '', path: '/foo/bar.json',) - assert_equal uri, JSON::Util::URI.normalize_uri(str, '/home') + assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home') end def test_normalized_uri_for_relative_path @@ -40,7 +40,7 @@ def test_normalized_uri_for_relative_path uri = Addressable::URI.new(scheme: 'file', host: '', path: '/home/foo/bar.json',) - assert_equal uri, JSON::Util::URI.normalize_uri(str, '/home') + assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home') end def test_normalized_uri_for_file_path_with_host @@ -48,7 +48,7 @@ def test_normalized_uri_for_file_path_with_host uri = Addressable::URI.new(scheme: 'file', host: 'localhost', path: '/foo/bar.json',) - assert_equal uri, JSON::Util::URI.normalize_uri(str, '/home') + assert_equal uri, JSON::Util::URI.normalized_uri(str, '/home') end def test_uri_parse