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

Migrating From RefResolver, unable to resolve $ref #1213

Closed aryabharat closed 7 months ago

aryabharat commented 7 months ago

Subject: Issue with $ref resolution after updating to the latest jsonschema version

Hi there 👋,

I hope this finds you well. I recently updated the code to the latest jsonschema version, and as part of the process, I refactored the following code:

    resolver = RefResolver(file_prefix + schema_path.replace("\\", "/"), schema, store=SCHEMA_STORE)
    schema_id = schema.get('$id', '')
    if schema_id:
        resolver.store[schema_id] = schema
    # RefResolver creates a new store internally; persist it so we can use the same mappings every time
    SCHEMA_STORE = resolver.store
    validator = STIXValidator(schema, resolver=resolver, format_checker=Draft202012Validator.FORMAT_CHECKER)

The updated code looks like:

    registry = Registry().with_resource(file_prefix + schema_path.replace("\\", "/"), schema)
    validator = STIXValidator(schema,  registry=registry, format_checker=Draft202012Validator.FORMAT_CHECKER)
    return validator

However, during testing, I encountered the following issue: jsonschema.exceptions._RefResolutionError: <urlopen error [Errno 2] No such file or directory: '../common/core.json'>

The JSON schema is using $ref as follows:

json Copy code

{
  "$ref": "../common/core.json"
}

After reviewing the documentation, I couldn't find any information on how to handle the file URI in this context.

Am I missing something or doing something incorrectly? Your insights and guidance would be highly appreciated.

Thank you for your time and assistance! 🙏