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

Use unescape for paths before reading files #188

Merged
merged 3 commits into from
Nov 27, 2014
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
5 changes: 3 additions & 2 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def absolutize_ref_uri(ref, parent_schema_uri)

uri = parent_schema_uri.clone
uri.fragment = ''
uri.join(ref_uri.path)
normalized_uri(uri.join(ref_uri.path))
end

# Build all schemas with IDs, mapping out the namespace
Expand Down Expand Up @@ -574,14 +574,15 @@ def custom_open(uri)
if uri.absolute? && uri.scheme != 'file'
open(uri.to_s).read
else
File.read(uri.path)
File.read(Addressable::URI.unescape(uri.path))
end
end

def normalized_uri(data)
uri = Addressable::URI.parse(data)
# Check for absolute path
if uri.relative?
data = data.to_s
data = "#{Dir.pwd}/#{data}" if data[0,1] != '/'
uri = Addressable::URI.convert_path(data)
end
Expand Down
12 changes: 9 additions & 3 deletions test/schemas/ref john with spaces schema.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{ "$schema" : "http://json-schema.org/draft-04/schema#"
, "type" : "string"
, "pattern" : "john"
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"type" : "object",
"properties": {
"first": {
"type": "string",
"enum": ["john"]
}
}
}
12 changes: 9 additions & 3 deletions test/test_uri_related.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_schema_ref_with_empty_fragment
}
}
}
data = { "names" => ['john'] }
data = {"first" => "john" }
assert_valid schema, data
end

Expand All @@ -47,13 +47,19 @@ def test_schema_ref_from_file_with_spaces
"type"=> "array",
"items"=> {
"anyOf"=> [
{ "$ref" => "test/schemas/ref john with spaces schema.json" },
{ "$ref" => "test/schemas/ref john with spaces schema.json" }
]
}
}
}
}
data = { "names" => ['john'] }
data = {"first" => "john" }
assert_valid schema, data
end

def test_schema_from_file_with_spaces
data = { "first" => "john" }
schema = "test/schemas/ref john with spaces schema.json"
assert_valid schema, data
end
end