hypothesis-jsonschema icon indicating copy to clipboard operation
hypothesis-jsonschema copied to clipboard

Add some way of resolving out-of-schema references

Open Zac-HD opened this issue 5 years ago • 4 comments

I really don't like references to other schemas, because they make the meaning temporally variable and unreliable - the referenced schema can change or become inaccessible. Dealing with them also adds IO, which makes everything harder.

Nonetheless, they are useful and in practice widely used, so we can't ignore them forever. I would therefore like to add a function which fetches external schemas, adds them to a definitions section, and rewrites the $ref keys to point to the new in-schema copies.

The actual from_schema() implementation can then continue to ignore IO, and any problems will at least be isolated in their own function!

Zac-HD avatar Dec 11 '20 23:12 Zac-HD

@Zac-HD where would such a function be placed in the codebase, if we are only to rewrite refs and just add external dependencies as such this could be fairly straightforward to implement...

More desirably, we could have a user-defined Resolver for external references which would point (and load) the schemas and within the library, we'd perform only the write whenever a ref tag that refers to a resolved external schema is encountered.

andylamp avatar May 18 '21 00:05 andylamp

This would be a new top-level function, hypothesis_jsonschema.load_external_refs(schema: Schema) -> Schema.

I'd define it in a new _external_refs.py file, since nothing else should depend on this, and then import whatever machinery is useful from _canonicalise.py. I don't (yet) have any opinion on how it should work, since this is mostly to support workflows that I don't have any real experience with - your proposal sounds good though.

Zac-HD avatar May 18 '21 01:05 Zac-HD

This extremely useful for our use case so I'd be keen to make this happen, just wanted a bit of lee-way on where to start. I think I'll take a jab at this and get back to you if I make any meaningful progress :)

andylamp avatar May 18 '21 01:05 andylamp

Awesome, thanks! Please open a PR once you have something - early feedback is the best kind and I'm always happy to review unfinished contributions.

Zac-HD avatar May 18 '21 02:05 Zac-HD