jsonschema
jsonschema copied to clipboard
Provide a helper to fully resolve $refs in a schema
I want to generate some documentation/description (probably some input forms) from a json schema that contains $refs. Is there a way in jsonschema to retrieve a json schema with $refs resolved and inlined, so I can iterate through the schema?
So from a schema like this:
{
"definitions": {
"some_object": {
"type": "object",
"title": "My definition",
"description": "Some description",
"properties": {
"name": {
"type": "string"
},
"value": {
"type": "number"
}
}
}
},
"properties": {
"list_of_objects": {
"type": "array",
"title": "Some stuff",
"description": "List of stuff",
"items": {"$ref": "#/definitions/some_object"}
}
}
}
I want to have this structure in my Python object (notice that the {$ref:...} section is replaced by the actual definition):
{
"definitions": {
"some_object": {
"type": "object",
"title": "My definition",
"description": "Some description",
"properties": {
"name": {
"type": "string"
},
"value": {
"type": "number"
}
}
}
},
"properties": {
"list_of_objects": {
"type": "array",
"title": "Some stuff",
"description": "List of stuff",
"items": {
"type": "object",
"title": "My definition",
"description": "Some description",
"properties": {
"name": {
"type": "string"
},
"value": {
"type": "number"
}
}
}
}
}
}
I am aware that with self-referencing schemas this will lead to an infinite structure, but hopefully this is feasible with reasonable limitations (eg. excluding self-refs).
jsonref https://pypi.org/project/jsonref/ resolves references so that you can have fully expanded schemas.
I think @Julian previously decided that this belongs in a separate library: https://github.com/Julian/jsonschema/pull/419
I would like a helper function in jsonschema, though, as https://github.com/gazpachoking/jsonref seems to not be actively maintained (all its tests are failing on CI, so it's not clear that it would even be easy to take maintenance of).
@jpmckinney that was my stance early on, but given nothing popped up, and folks still seem to want this, I'm willing to entertain a helper function (essentially for the reason you mentioned).
So yeah a PR is welcome to add this.
Would #419 be a starting point? e.g. if you re-open it, it'd be possible to see how bad the conflicts are (if any).
Probably it's a good one yeah -- though I can't reopen it because GH prevents reopening PRs that targeted the old master branch now that it targets main :( -- but if you or someone wants to take what's there and (while preserving authorship of the commits) push it as a new PR it certainly may be a good basis to start from.
Is it possible to edit the PR to point it to a new base branch?
I think I commented here, when I ran into trouble with jsonref and PyPy https://github.com/open-contracting/ocdskit/issues/178, and thought to check whether jsonschema could resolve $ref.
At any rate, the library I was working on has other issues with PyPy. So, I'm not sure I still have a need at the moment.
Is it possible to edit the PR to point it to a new base branch?
Not as far as I know :(
At any rate, the library I was working on has other issues with PyPy. So, I'm not sure I still have a need at the moment.
Fair enough.