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

Self referenced schema encapsulation breaks reference resolution #1196

Closed andreynovikov closed 9 months ago

andreynovikov commented 9 months ago

I have a JSON that by design can contain JSON Schema. I use it at runtime to validate received payload. I have a JSON Schema that describes this JSON:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
        "code": {
            "type": "string"
        },
        "payload": {
            "$ref": "https://json-schema.org/draft/2020-12/schema",
        }
    }
}

When I try to validate my JSON with that schema I get: jsonschema.exceptions.RefResolutionError: Unresolvable JSON pointer: 'meta'. It tries to search inner references of payload schema in my root schema but not in $ref. When I change payload ref to:

        "payload": {
            "$ref": "https://json-schema.org/draft/2019-09/schema"
        }

validation works as expected.

Julian commented 9 months ago

Please share code which reproduces, as both jsonschema.validate({"payload": {"type": "object"}}, yourschema) and jsonschema.validate({"payload": {"type": "objet"}}, yourschema) work fine.