diff --git a/lib/json_matchers/matcher.rb b/lib/json_matchers/matcher.rb index 088c972..325988a 100644 --- a/lib/json_matchers/matcher.rb +++ b/lib/json_matchers/matcher.rb @@ -36,6 +36,10 @@ def build_and_populate_document_store map { |schema_path| Parser.new(schema_path).parse }. each { |schema| document_store.add_schema(schema) } + document_store.to_a.each do |_k, schema| + schema.expand_references!(store: document_store) + end + document_store end 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..b6e2649 100644 --- a/spec/json_matchers/match_json_schema_spec.rb +++ b/spec/json_matchers/match_json_schema_spec.rb @@ -202,6 +202,24 @@ expect(json_as_array).not_to match_json_schema(schema) end + it "validates against a schema that uses nested $refs" do + schema = create(:schema, :referencing_locations, + items: create(:schema, :referencing_locations)) + 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 + schema = create(:schema, :referencing_locations, + items: create(:schema, :referencing_locations)) + 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)