Skip to content

A reference to the same file inside a referenced file works when a loaded relatively but not by urn: #73

@ghost

Description

I'm very puzzled here - thanks for any info anyone can add!

libs.json:

{
  "$id": "urn:library",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$defs": {
    "address": {
      "title": "An Address",
      "description": "A Description",
      "type": "object",
      "properties": {
        "address": {
          "type": "string"
        },
        "country": {
          "$ref": "#/$defs/country"
        }
      }
    },
    "country": {
      "type": "string"
    }
  }
}

start.json:

{
  "$id": "urn:start",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
    "properties": {
      "home_address": {
        "title": "Home Address",
        "description": "Where the person lives",
        "$ref": "libs.json#/$defs/address"
      }
    }
}

test.py:

import os
import jsonref
import pathlib
import json

base_uri = pathlib.Path(
    os.path.dirname(__file__) + "/"
).as_uri() + "/"

with open("libs.json") as fp:
    lib = json.load(fp)

def _jsonref_loader(uri):
    global lib
    print("LOADING {}".format(uri))
    if uri == "urn:library":
        return lib
    return jsonref.jsonloader(uri)

with open("start.json") as fp:
    start = json.load(fp)

print("TEST 1 - WORKS GREAT!")
print(jsonref.JsonRef.replace_refs(start, base_uri=base_uri, loader=_jsonref_loader))

print("TEST 2 - FAILS!")
start["properties"]["home_address"]["$ref"] = "urn:library#/$defs/address"
print(jsonref.JsonRef.replace_refs(start, base_uri=base_uri, loader=_jsonref_loader))

As comments say, just changing the ref from start.json to lib.json to a urn breaks it.
You get:

TEST 1 - WORKS GREAT!
LOADING file:///.../libs.json
{'$id': 'urn:start', '$schema': 'https://json-schema.org/draft/2020-12/schema', 'properties': {'home_address': {'title': 'An Address', 'description': 'A Description', 'type': 'object', 'properties': {'address': {'type': 'string'}, 'country': {'type': 'string'}}}}}
TEST 2 - FAILS!
LOADING urn:library
LOADING 
Traceback (most recent call last):
...
ValueError: unknown url type: ''

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

Traceback (most recent call last):
...    
jsonref.JsonRefError: Error while resolving `#/$defs/country`: ValueError: unknown url type: ''

_jsonref_loader is called with an empty uri so "unknown url type" is fair enought, but why is it trying to load that in the first place?

(I also have a version of the test script that uses the refrencing library and loads libs.json into a registry, but that breaks the same way)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions