voxpupuli / json-schema

Ruby JSON Schema Validator
MIT License
1.52k stars 241 forks source link

Bugfix: Fix fragment when used with extended schema #464

Closed bastelfreak closed 2 years ago

bastelfreak commented 2 years ago

What this PR does

This PR fork resolves an issue with extended schema validation.

From the original author of the patch (circa 2016):

fix fragment when used with extended schema

This refactors some of Validator to fix an issue with validating a fragment of an extended schema against a json object. The problem is that while navigating through the schema, we are using the configured :version option as the validator for each partial schema. This is an issue because if the root of the schema declares a $schema, that information is lost in the traversal.

The refactor does two things. Firstly, it does not write back into @options[:version] and it stops using it outside of the constructor entirely. The only place that should know about this is the initialized schema, and from that point on, the canonical validator should be the schema object's validator since it makes a decision about which to use in its constructor. Now we can reduce complexity by using that validator in the :validate_schema option branch.

The other place that used :version is the schema_from_fragment method, which is where the bug exists. What we're doing here, instead of dealing with schemas and hashes during the iterator, is pulling the schema object out, digging through it using the fragments, and then wrapping the result in a schema using the base_schema's validator.

Tests have been included that demonstrate the problem.

This is a resubmission of https://github.com/voxpupuli/json-schema/pull/434

codecov[bot] commented 2 years ago

Codecov Report

Merging #464 (948ff6c) into master (072d956) will increase coverage by 0.06%. The diff coverage is 88.88%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #464      +/-   ##
==========================================
+ Coverage   89.75%   89.82%   +0.06%     
==========================================
  Files          74       74              
  Lines        1581     1572       -9     
==========================================
- Hits         1419     1412       -7     
+ Misses        162      160       -2     
Impacted Files Coverage Δ
lib/json-schema/validator.rb 84.77% <88.88%> (+0.18%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 072d956...948ff6c. Read the comment docs.