From 6f1147d2180e0c730063f772a0daa1f3bfae2d13 Mon Sep 17 00:00:00 2001 From: Jonas Peschla Date: Tue, 25 Nov 2014 22:11:10 +0100 Subject: [PATCH 1/3] Use unescape for paths before reading files --- lib/json-schema/validator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json-schema/validator.rb b/lib/json-schema/validator.rb index 36fb45fc..3609a1d5 100644 --- a/lib/json-schema/validator.rb +++ b/lib/json-schema/validator.rb @@ -574,7 +574,7 @@ 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 From 3fac158379b0d82419e07dabf94ca8105f96f396 Mon Sep 17 00:00:00 2001 From: Jonas Peschla Date: Thu, 27 Nov 2014 21:57:13 +0100 Subject: [PATCH 2/3] Ensure normalized_uri is used for ref URIs too --- lib/json-schema/validator.rb | 3 ++- test/schemas/ref john with spaces schema.json | 12 +++++++++--- test/test_uri_related.rb | 6 +++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/json-schema/validator.rb b/lib/json-schema/validator.rb index 3609a1d5..ff17c27e 100644 --- a/lib/json-schema/validator.rb +++ b/lib/json-schema/validator.rb @@ -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 @@ -582,6 +582,7 @@ 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 diff --git a/test/schemas/ref john with spaces schema.json b/test/schemas/ref john with spaces schema.json index 6d5cc9eb..3d75f0c5 100644 --- a/test/schemas/ref john with spaces schema.json +++ b/test/schemas/ref john with spaces schema.json @@ -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"] + } + } } diff --git a/test/test_uri_related.rb b/test/test_uri_related.rb index 004d217f..f47db499 100644 --- a/test/test_uri_related.rb +++ b/test/test_uri_related.rb @@ -34,7 +34,7 @@ def test_schema_ref_with_empty_fragment } } } - data = { "names" => ['john'] } + data = { "first" => "john" } assert_valid schema, data end @@ -47,13 +47,13 @@ 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 end From e59c52824deabb18bc700dbf3726e0355b6213e9 Mon Sep 17 00:00:00 2001 From: Jonas Peschla Date: Thu, 27 Nov 2014 22:45:53 +0100 Subject: [PATCH 3/3] Add test with schema from path with spaces --- test/test_uri_related.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/test_uri_related.rb b/test/test_uri_related.rb index f47db499..d3de2ab6 100644 --- a/test/test_uri_related.rb +++ b/test/test_uri_related.rb @@ -34,7 +34,7 @@ def test_schema_ref_with_empty_fragment } } } - data = { "first" => "john" } + data = {"first" => "john" } assert_valid schema, data end @@ -53,7 +53,13 @@ def test_schema_ref_from_file_with_spaces } } } + 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