Skip to content

Commit

Permalink
Expand references of DocuemntStore schemas
Browse files Browse the repository at this point in the history
This is a fix for #92.

Calls `expand_references!` on loaded schemas before adding them to the
`DocumentStore`.  This is intended to fix a bug where complex schemas
that include more than one level of cross-file use of `$ref`s are not
being validated correctly.

This commit includes new test cases that reproduce this issue and show
the effectiveness of the proposed fix.

The inspiration for this fix comes from a command included in the
`json_schema` gem.  In that gem, the stand-alone validator command
expands references within schemas before adding them to the store.
It seems reasonable to do the same here.

See https://github.com/brandur/json_schema/blob/20ccb82d7e18140d88ded508edd6c003865c98a0/lib/commands/validate_schema.rb#L92.
  • Loading branch information
richardlarocque authored and seanpdoyle committed Feb 15, 2019
1 parent 1e5575c commit 3b66b4d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/json_matchers/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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#" },
Expand Down
20 changes: 20 additions & 0 deletions spec/json_matchers/match_json_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 21 additions & 0 deletions test/json_matchers/minitest/assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 3b66b4d

Please sign in to comment.