diff --git a/lib/json_matchers/matcher.rb b/lib/json_matchers/matcher.rb index 088c972..25cc592 100644 --- a/lib/json_matchers/matcher.rb +++ b/lib/json_matchers/matcher.rb @@ -34,7 +34,8 @@ def build_and_populate_document_store Dir.glob("#{JsonMatchers.schema_root}/**/*.json"). map { |path| Pathname.new(path) }. map { |schema_path| Parser.new(schema_path).parse }. - each { |schema| document_store.add_schema(schema) } + map { |schema| document_store.add_schema(schema) }. + each { |schema| schema.expand_references!(store: document_store) } document_store end diff --git a/spec/factories.rb b/spec/factories.rb index 21a12e8..829d4f0 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -68,6 +68,7 @@ initialize_with do FakeSchema.new(name, { + "id": "file:/#{name}.json#", "$schema": "https://json-schema.org/draft-04/schema#", "type": "array", "items": { "$ref": "file:/#{items.name}.json#" }, diff --git a/spec/json_matchers/match_json_schema_spec.rb b/spec/json_matchers/match_json_schema_spec.rb index 3f4b6eb..f3a0d04 100644 --- a/spec/json_matchers/match_json_schema_spec.rb +++ b/spec/json_matchers/match_json_schema_spec.rb @@ -202,6 +202,26 @@ expect(json_as_array).not_to match_json_schema(schema) end + it "validates against a schema that uses nested $refs" do + items = create(:schema, :referencing_locations) + schema = create(:schema, :referencing_locations, items: items) + + json = build(:response, :location) + json_as_array = [[json.to_h]] + + expect(json_as_array).to match_json_schema(schema) + end + + it "fails against a schema that uses nested $refs" do + items = create(:schema, :referencing_locations) + schema = create(:schema, :referencing_locations, items: items) + + json = build(:response, :invalid_location) + json_as_array = [[json.to_h]] + + expect(json_as_array).not_to match_json_schema(schema) + end + it "validates against a schema referencing with 'definitions'" do schema = create(:schema, :referencing_definitions) diff --git a/test/json_matchers/minitest/assertions_test.rb b/test/json_matchers/minitest/assertions_test.rb index ac474de..590f0d3 100644 --- a/test/json_matchers/minitest/assertions_test.rb +++ b/test/json_matchers/minitest/assertions_test.rb @@ -171,6 +171,27 @@ class AssertResponseMatchesSchemaTest < JsonMatchers::TestCase refute_matches_json_schema(json_as_array, schema) end + test "validates against a schema that uses nested $refs" do + items = create(:schema, :referencing_locations) + schema = create(:schema, :referencing_locations, items: items) + + json = build(:response, :location) + json_as_array = [[json.to_h]] + + assert_matches_json_schema(json_as_array, schema) + end + + test "fails against a schema that uses nested $refs" do + items = create(:schema, :referencing_locations) + schema = create(:schema, :referencing_locations, items: items) + + json = build(:response, :invalid_location) + json_as_array = [[json.to_h]] + + refute_matches_json_schema(json_as_array, schema) + end + + test "validates against a schema referencing with 'definitions'" do schema = create(:schema, :referencing_definitions)