From be00bcd618995ed6f59c40259e4b74b6c97c6c2c Mon Sep 17 00:00:00 2001 From: JP Hastings-Spital Date: Fri, 13 Feb 2015 12:26:21 +0000 Subject: [PATCH] Ignore fragments in schema caching. Fixes #233 --- lib/json-schema/validator.rb | 2 +- test/test_load_ref_schema.rb | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/test_load_ref_schema.rb diff --git a/lib/json-schema/validator.rb b/lib/json-schema/validator.rb index 571a823d..0def1c72 100644 --- a/lib/json-schema/validator.rb +++ b/lib/json-schema/validator.rb @@ -129,7 +129,7 @@ def validate() def load_ref_schema(parent_schema, ref) schema_uri = absolutize_ref_uri(ref, parent_schema.uri) - + schema_uri.fragment = '' return true if self.class.schema_loaded?(schema_uri) schema = @options[:schema_reader].read(schema_uri) diff --git a/test/test_load_ref_schema.rb b/test/test_load_ref_schema.rb new file mode 100644 index 00000000..f37942dc --- /dev/null +++ b/test/test_load_ref_schema.rb @@ -0,0 +1,40 @@ +require File.expand_path('../test_helper', __FILE__) + +class LoadRefSchemaTests < Minitest::Test + def load_other_schema + JSON::Validator.add_schema(JSON::Schema.new( + { + '$schema' => 'http://json-schema.org/draft-04/schema#', + 'type' => 'object', + 'properties' => { + "title" => { + "type" => "string" + } + } + }, + Addressable::URI.parse("http://example.com/schema#") + )) + end + + def test_cached_schema + schema_url = "http://example.com/schema#" + schema = { + "$ref" => schema_url + } + data = {} + load_other_schema + validator = JSON::Validator.new(schema, data) + assert JSON::Validator.schema_loaded?(schema_url) + end + + def test_cached_schema_with_fragment + schema_url = "http://example.com/schema#" + schema = { + "$ref" => "#{schema_url}/properties/title" + } + data = {} + load_other_schema + validator = JSON::Validator.new(schema, data) + assert JSON::Validator.schema_loaded?(schema_url) + end +end