python-jsonschema / jsonschema

An implementation of the JSON Schema specification for Python
https://python-jsonschema.readthedocs.io
MIT License
4.58k stars 578 forks source link

Getting schema contents after refs are resolved #1188

Closed nsheff closed 10 months ago

nsheff commented 10 months ago

I'm following the instructions on resolving json refeferences using the new referencing package, and it works for validation. However, the resource.contents continues to provide the schema in its original form (without refs resolved). For some use cases (apart from validation), I would like to be able to get the fully-resolved schema, including with resolved $refs that point to $defs -- but I can't figure out how to do this. The resolved schema clearly exists somewhere for the final validation to work -- would it be possible to make this somehow accessible so that said resolved schema could be used for other purposes as well?

Julian commented 10 months ago

This is actually in general impossible (in general being when you take into account $dynamicRef and not just $ref). It's also discouraged by the JSON Schema team!

In general the main reasons to do this are indeed for non-validation, and generally for tooling which has poor support for $ref itself (thankfully this library now not being one of those).

The recommended solution (by the JSON Schema team) to do what generally is needed for said tooling -- namely to reduce the amount of fancy ref resolution needed such that it's "easier" and self contained -- is the official bundling process. An issue to track adding a bundler to referencing is here.