Search code examples
jsonschemapython-jsonschema

Multi-stage $ref doesn't work with jsonschema version 3.0.1


It seems that jsonschema version 3.0.1 does not accept multi-stage schema using $refs (while it works with jsonschema version 2.6.0).

I have to make it work under several module versions simply because my code will be running on different computers with different environments.

I verified my jsons on https://www.jsonschemavalidator.net/ (thanks for this link found in another StackOverflow question).

I Tried :

  jsonschema -i myjson.json noRefs.schema.json      --> 2.6.0 = OK, 3.0.1 OK
  jsonschema -i myjson.json usingRefs.schema.json   --> 2.6.0 = OK, 3.0.1 KO

Note : Both *.schema.json worked on https://www.jsonschemavalidator.net/

File myjson.json :

{
  "TopProperty" : {
    "LowerProperty" : {"toto" : "plop"}
  }
}

File noRefs.schema.json :

{
  "type": "object",

  "properties": {
    "TopProperty": {"$ref": "#/schemaTopProperty"}
  },

  "schemaTopProperty": {
    "$id": "schemaTopProperty",
    "type": "object",

    "properties": {
      "LowerProperty": {
        "type": "object",
        "properties": {
          "toto": {"type": "string"}
        }
      }
    }
  }
}

File usingRefs.schema.json :

{
  "type": "object",

  "properties": {
    "TopProperty": {"$ref": "#/schemaTopProperty"}
  },

  "schemaTopProperty": {
    "$id": "schemaTopProperty",
    "type": "object",

    "properties": {
      "LowerProperty": {
        "type": "object",
        "properties": {
          "toto": {"$ref": "#/justAString"}
        }
      }
    }
  },

  "justAString": {
    "$id": "justAString",
    "type": "string"
  }

}

Error message received :

Traceback (most recent call last):                                                                                                         
  File "/usr/bin/jsonschema", line 11, in <module>                                                                                         
    sys.exit(main())                                                                                                                       
  File "/usr/lib/python2.7/site-packages/jsonschema/cli.py", line 67, in main                                                              
    sys.exit(run(arguments=parse_args(args=args)))                                                                                         
  File "/usr/lib/python2.7/site-packages/jsonschema/cli.py", line 78, in run                                                               
    for error in validator.iter_errors(instance):                                                                                          
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 323, in iter_errors                                               
    for error in errors:                                                                                                                   
  File "/usr/lib/python2.7/site-packages/jsonschema/_validators.py", line 274, in properties                                               
    schema_path=property,                                                                                                                  
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 339, in descend                                                   
    for error in self.iter_errors(instance, schema):
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 323, in iter_errors
    for error in errors:
  File "/usr/lib/python2.7/site-packages/jsonschema/_validators.py", line 251, in ref
    for error in validator.descend(instance, resolved):
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 339, in descend
    for error in self.iter_errors(instance, schema):
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 323, in iter_errors
    for error in errors:
  File "/usr/lib/python2.7/site-packages/jsonschema/_validators.py", line 274, in properties
    schema_path=property,
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 339, in descend
    for error in self.iter_errors(instance, schema):
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 323, in iter_errors
    for error in errors:
  File "/usr/lib/python2.7/site-packages/jsonschema/_validators.py", line 73, in items
    for error in validator.descend(item, items, path=index):
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 339, in descend
    for error in self.iter_errors(instance, schema):
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 323, in iter_errors
    for error in errors:
  File "/usr/lib/python2.7/site-packages/jsonschema/_validators.py", line 274, in properties
    schema_path=property,
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 339, in descend
    for error in self.iter_errors(instance, schema):
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 323, in iter_errors
    for error in errors:
  File "/usr/lib/python2.7/site-packages/jsonschema/_validators.py", line 247, in ref
    scope, resolved = validator.resolver.resolve(ref)
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 734, in resolve
    return url, self._remote_cache(url)
  File "/usr/lib/python2.7/site-packages/functools32/functools32.py", line 400, in wrapper
    result = user_function(*args, **kwds)
  File "/usr/lib/python2.7/site-packages/jsonschema/validators.py", line 744, in resolve_from_url
    raise exceptions.RefResolutionError(exc)
jsonschema.exceptions.RefResolutionError: unknown url type: schemaTopProperty

Solution

  • Edit: my previous answer was incorrect.

    TL;DR: You have two options:

    1. Remove the $id properties from the definitions
    2. Use #/ in the $id properties (Example: {"$id": "#/justAString"})

    Details:

    The issue is with the IDs, up until draft-04, $ref and $id were treated at face value, nothing special, but starting with draft-06 these are uri-references, in which case, when descending into {"$id": "schemaTopProperty"}, resolving {"$ref": "justAString"} is no more looking for a fragment justAString at the root structure, but for /justAString under schemaTopProperty host, which is a remote reference.

    Hence my solutions to either remove the $ids which cause the definitions to be URLs (hosts in fact), or to define the $ids as what they are, fragments in the current schema.