Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Validation fails with multi-file schemas with relative file names

Aperta
#1,454 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python

Direzione di ricerca

Inizia con il riproduttore in top/test.py ed esamina referencing/_core.py intorno alla riga 680, dove urljoin modifica il $ref relativo durante la ricerca. Conferma il comportamento con l’albero degli schemi fornito e determina perché il riferimento annidato viene risolto in modo diverso; il lavoro è completato quando la validazione ha esito positivo per il riproduttore e la copertura di regressione protegge i riferimenti relativi tra schemi annidati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

I have a complex web of JSON schemas that serve to specify a data model. Since we do not want to make the schemas world visible, and want to be able to move the entire code tree without breaking, we want to use relative file names for $ref. We are using a Registry instance to find the schema files. The $refs are of the form "../somedir/schemafile.json", as the schemas are put into parallel directories, being too numerous to fit well into a single directory.

It turns out that in some cases the $ref strings are mangled, so that the lookup fails, for which file varies a bit between different tests.

There might be a better way of achieving the desired result that I just do not know, but this behaviour does not look right.

Reproducing the problem

The problem can be reproduced with the following files:

top/test,py

import json
import jsonschema
from pathlib import Path
from referencing import Registry, Resource
from referencing.exceptions import NoSuchResource

BASE = Path(__file__).parent
def retrieve_from_filesystem(uri: str):
    if not uri.startswith("../"):
        raise NoSuchResource(ref=uri)
    path = BASE / Path(uri.removeprefix("../"))
    contents = json.loads(path.read_text())
    return Resource.from_contents(contents)

registry = Registry(retrieve=retrieve_from_filesystem)


if __name__ == "__main__":
    instance = BASE / "dttest" / "tst_instance.json"
    schema = BASE / "schem" / "DropImageData.json"
    jsonschema.validate(
        instance=json.load(open(instance)),
        schema=json.load(open(schema)),
        registry=registry
    )

top/dttest/tst_instance.json

{
  "mimeType":"image/jpeg",
  "data": "some UUencoded data would be here"
}

top/schem/DropImage.json

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "mimeType": {
        "allOf": [
          {
            "$ref": "../schem/ImageMimeType.json"
          }
        ]
      }
    },
    "required": ["mimeType"]
}

top/schem/DropImageData.json

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "description": "DropImage, containing image as attached data.",
    "title": "DropImageData",
    "type": "object",
    "allOf": [
        {
            "$ref": "../schem/DropImage.json"
        }
    ]
}

top/schem/ImageMimetype.json

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "title": "ImageMimetype",
    "type": "string",
    "enum": [
                "image/png",
                "image/jpeg"
    ]
}

Error output:

Traceback (most recent call last):
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/jsonschema/validators.py", line 462, in _validate_reference
    resolved = self._resolver.lookup(ref)
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/referencing/_core.py", line 684, in lookup
    raise exceptions.Unresolvable(ref=ref) from None
referencing.exceptions.Unresolvable: ../schem/ImageMimeType.json

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/rhfogh/pycharm/mxlims_data_model/mxlims/test2/test.py", line 21, in <module>
    jsonschema.validate(
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/jsonschema/validators.py", line 1330, in validate
    error = exceptions.best_match(validator.iter_errors(instance))
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/jsonschema/exceptions.py", line 479, in best_match
    best = max(errors, key=key, default=None)
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/jsonschema/validators.py", line 383, in iter_errors
    for error in errors:
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/jsonschema/_keywords.py", line 334, in allOf
    yield from validator.descend(instance, subschema, schema_path=index)
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/jsonschema/validators.py", line 431, in descend
    for error in errors:
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/jsonschema/_keywords.py", line 275, in ref
    yield from validator._validate_reference(ref=ref, instance=instance)
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/jsonschema/validators.py", line 431, in descend
    for error in errors:
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/jsonschema/_keywords.py", line 296, in properties
    yield from validator.descend(
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/jsonschema/validators.py", line 431, in descend
    for error in errors:
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/jsonschema/_keywords.py", line 334, in allOf
    yield from validator.descend(instance, subschema, schema_path=index)
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/jsonschema/validators.py", line 431, in descend
    for error in errors:
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/jsonschema/_keywords.py", line 275, in ref
    yield from validator._validate_reference(ref=ref, instance=instance)
  File "/home/rhfogh/Software/miniconda3/envs/mxlims/lib/python3.10/site-packages/jsonschema/validators.py", line 464, in _validate_reference
    raise exceptions._WrappedReferencingError(err) from err
jsonschema.exceptions._WrappedReferencingError: Unresolvable: ../schem/ImageMimeType.json

Some limited debugging shows that the validation fails when referencing._core.lookup is called, in a case where
self._base_uri == ref
As a result ref is converted from '../schem/ImageMimeType.json' to a uri of 'schem/ImageMimeType.json' , which cannot be found in the Registry lookup. The problem happens in line 680 of _core.py, where the urljoin function removes the '../' prefix.
It is notable that this does not happen the first time the program gets to this line, so that the ref '../schem/DropImage.json' is processed correctly, presumably because self._base_url is an empty string at this point.

Versions used

jsonschema version 4.26.0
Python 3.10
JSONschema version 2020-12

Lingua principale
Python
Stelle
5k
Fork
694
Merge medio
1g 20h
PR unite (30g)
6

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python-jsonschema/jsonschema

Tutte le issue di python-jsonschema/jsonschema

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.