Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made it possible to have refs that include URI-special characters #322

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Fixed
- Made it possible to include colons in a $ref

### Changed
- Reformatted examples in the readme

Expand Down
4 changes: 2 additions & 2 deletions lib/json-schema/attributes/ref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ 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 = 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 != ''
fragment = JSON::Util::URI.unescaped_uri(fragment.gsub('~0', '~').gsub('~1', '/'))
fragment = fragment.gsub('~0', '~').gsub('~1', '/')
if target_schema.is_a?(Array)
target_schema = target_schema[fragment.to_i]
else
Expand Down
2 changes: 1 addition & 1 deletion lib/json-schema/schema/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def read_uri(uri)

def read_file(pathname)
if accept_file?(pathname)
File.read(JSON::Util::URI.unescaped_uri(pathname.to_s))
File.read(JSON::Util::URI.unescaped_path(pathname.to_s))
else
raise JSON::Schema::ReadRefused.new(pathname.to_s, :file)
end
Expand Down
6 changes: 5 additions & 1 deletion lib/json-schema/util/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ def self.file_uri(uri)
Addressable::URI.convert_path(parsed_uri.path)
end

def self.unescaped_uri(uri)
def self.unescape_uri(uri)
Addressable::URI.unescape(uri)
end

def self.unescaped_path(uri)
parsed_uri = parse(uri)

Addressable::URI.unescape(parsed_uri.path)
Expand Down
2 changes: 1 addition & 1 deletion lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ def custom_open(uri)
end
else
begin
File.read(JSON::Util::URI.unescaped_uri(uri))
File.read(JSON::Util::URI.unescaped_path(uri))
rescue SystemCallError => e
raise JSON::Schema::JsonLoadError, e.message
end
Expand Down
14 changes: 14 additions & 0 deletions test/schemas/definition_schema_with_special_characters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"a": {
"$ref": "#/definitions/foo:bar"
}
},
"definitions": {
"foo:bar": {
"type": "integer"
}
}
}
4 changes: 4 additions & 0 deletions test/test_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ def test_definition_schema
assert_valid schema_fixture_path('definition_schema.json'), {"a" => 5}
end

def test_definition_schema_with_special_characters
assert_valid schema_fixture_path('definition_schema_with_special_characters.json'), {"a" => 5}
end

def test_relative_definition
schema = schema_fixture_path('relative_definition_schema.json')
assert_valid schema, {"a" => 5}
Expand Down