Context: I've spent the last week writing a code generation script that converts JSON schemas into Python dataclasses and C++ classes; and today I stumbled into your wonderful project!
My schemas are all file-based, an in my parser I uses the python-jsonschema/referencing library with a custom fetcher to handle the relative file references:
import json
from pathlib import Path
from urllib.parse import urlparse
import referencing
# Could use an object that holds the base folder as a property, using this for simplicity
BASE_FOLDER = Path()
def retrieve(self, uri:str) -> referencing.Resource:
parsed_uri = urlparse(uri)
p = Path(parsed_uri.path).relative_to("/")
with open(BASE_FOLDER / p) as f:
return referencing.Resource.from_contents(json.load(f))
def do_parsing():
# Add the first file
with open("Foo.schema.json", "r") as fp:
jmap = json.load(fp)
resource = referencing.Resource.from_contents(jmap)
# Initial registry
registry = referencing.Registry(retrieve=retrieve)
# Recursively parse the schema, when `$ref` is found:
# The value of $ref goes here
# lookup is great!!
referenced_json_schema = registry.resolver().lookup(jmap["$ref"])
# referenced_json_schema is a Python dict with the JSON data
# Update the registry with the referenced schema's ID
registry = referenced_json_schema @ registry
Looking at datamodel-code-generator/datamodel_code_generator/reference.py
, I think there's a lot of overlap between that and referencing.
Check out the referencing docs.
Cheers!
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too