json-schema icon indicating copy to clipboard operation
json-schema copied to clipboard

WIP: resolve refs in schemas in one extrapolated hash

Open RST-J opened this issue 10 years ago • 10 comments

This is a first shot at generating a fully resolved schema hash as requested in #32. I commented very verbosely so you can directly follow my ideas. Any comments, improvements et al?

Notes:

  • So far this only works with $ref but can be easily extended for extends too
  • in the test test_linked_list_ref using the file path instead of the URL does not work (see comment)
  • pretty printing a cyclic hash or using it for validation kills kittens

Questions:

  1. Has someone an idea why using the file path in the test does not work?
  2. Is there anything missing to be draft4 compliant?

RST-J avatar Jan 24 '15 01:01 RST-J

I've had a look at this because it's a feature that I'd like to see in the gem. I think the approach is sensible, but I don't know the library very well so there might be some edge cases I'm missing.

I've made a handful of stylistic comments which I hope are helpful!

inglesp avatar Feb 06 '15 19:02 inglesp

Thanks for your feedback. I'll see what I can do about the names and particularly list is really not self describing.

RST-J avatar Feb 07 '15 16:02 RST-J

I tested this branch using the following test:

# test
    context 'testing schema resolver code' do
        it 'does not throw an error' do
            schema = JSON::Validator.get_schema_body('./schemas/iodocs/v2/expanded/iodocs_schema_v2.0.json')
            resolved = JSON::Validator.fully_resolve(schema)
            json = resolved.to_json(max_nesting: 10000)
            json
        end
    end

It generated a NestingError regardless of the depth I allowed in the call above - this is definitely a serialization issue where the object being serialized has infinite depth due to circular references in the way the hashes are related to each other.

JSON::NestingError: nesting of 10000 is too deep
./spec/lib/cli_spec.rb:156:in `to_json'
./spec/lib/cli_spec.rb:156:in `block (3 levels) in <top (required)>'
/Users/bgarrets/.rvm/gems/ruby-2.0.0-p451/gems/ruby-debug-ide-0.4.24/lib/ruby-debug-ide.rb:86:in `debug_load'
/Users/bgarrets/.rvm/gems/ruby-2.0.0-p451/gems/ruby-debug-ide-0.4.24/lib/ruby-debug-ide.rb:86:in `debug_program'
-e:1:in `load'
-e:1:in `<main>'

It's not necessarily enough to create a hash in memory and reuse existing hash object references if they can be reused during buildout. In any sufficiently complex schema where objects are being reused in multiple places in the schema hierarchy you'll end up with circular references that can't then be serialized out.

To get around this, you would need to basically collapse every schema you encounter into the 'new schema file's definitions block, possibly generating new definition entries for schemas that were the root schema in a file, and updating $ref statements to point to the new schema location within the single file schema instead of reaching out to other files.

BGarretson avatar May 15 '15 00:05 BGarretson

The suggestion above is basically the process I had to go through to manually create a single-file schema from the multi-file schema set as a manual workaround for my colleague.

BGarretson avatar May 15 '15 00:05 BGarretson

Alternately, you could monitor for circular references and only allow the method to resolve a schema if it was a directed acyclical graph

BGarretson avatar May 15 '15 00:05 BGarretson

I like your first idea better and will have a look at how to do this when I find time.

RST-J avatar May 18 '15 06:05 RST-J

this has been quiet for a while... any updates or chances it will be finished and merged?

Jubke avatar May 02 '16 12:05 Jubke

Has this been abandoned?

ubhide avatar Jul 06 '20 21:07 ubhide

Any progress on this? This is very trivial feature for a web application that I am working on right now.

DeltaRazero avatar Feb 08 '22 13:02 DeltaRazero

@DeltaRazero we need to rebase this to merge it. If you're interested you can checkout the branch, rebase against our master and submit it as new PR.

bastelfreak avatar Feb 08 '22 13:02 bastelfreak