Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not load schema with relative uris definitions #156

Open
manuelgavidia opened this issue May 3, 2021 · 5 comments
Open

Could not load schema with relative uris definitions #156

manuelgavidia opened this issue May 3, 2021 · 5 comments

Comments

@manuelgavidia
Copy link

manuelgavidia commented May 3, 2021

Hello, some help needed with this ...

When validating the following msg.json with schema hierarchy1 works fine:

msg.json:

{
    "msg": "Hello World"
}

Schema hierarchy1:

/root.json

{
    "$schema": "http://json-schema.org/schema#",
    "$id": "http://acme.com/schemas/root.json",
    "properties": {
        "msg": {
            "anyOf": [
                { "$ref": "v1/v1.json#/definitions/msg" },
                { "$ref": "v2.json#/definitions/msg" }
            ]
        }
    },
    "required": ["msg"]
}

/common.json

{
    "$schema": "http://json-schema.org/schema#",
    "$id": "http://acme.com/schemas/common.json",
    "definitions": {
        "attr": {"type": "string"}
    }
}

/v2.json

{
    "$schema": "http://json-schema.org/schema#",
    "$id": "http://acme.com/schemas/v2.json",
    "definitions": {
        "msg": { "$ref": "v1/v1.json#/definitions/msg"}
    }
}

/v1/v1.json

{
    "$schema": "http://json-schema.org/schema#",
    "$id": "http://acme.com/schemas/v1/v1.json",
    "definitions": {
        "msg": { "$ref": "../common.json#/definitions/attr"}
    }
}

When changing to hierarchy2 it validates the doc but also complains inserting the root schema:

$json-schema-validate schemas/root.json < msg.json
setting root schema failed
schema with http://acme.com/schemas/v1/v1.json # /definitions/msg already inserted
document is valid

Schema hierarchy2 with same docs except:

/root.json

{
    "$schema": "http://json-schema.org/schema#",
    "$id": "http://acme.com/schemas/root.json",
    "properties": {
        "msg": {
            "anyOf": [
                { "$ref": "v1/v1.json#/definitions/msg" },
                { "$ref": "v2/v2.json#/definitions/msg" }
            ]
        }
    },
    "required": ["msg"]
}

/v2/v2.json

{
    "$schema": "http://json-schema.org/schema#",
    "$id": "http://acme.com/schemas/v2/v2.json",
    "definitions": {
        "msg": { "$ref": "./../v1/v1.json#/definitions/msg"}
    }
}

I am using tag 2.1.0 and nlohmann/json v3.8.0.

Thanks in advance,

@pboettch
Copy link
Owner

pboettch commented May 3, 2021

Relative paths are not supported I think. If the standard allows it, it could be supported, though.

@pboettch
Copy link
Owner

I think ../ should work as relative paths are supported but not yet by this library...

@pboettch pboettch added the bug label Jun 17, 2021
@pboettch
Copy link
Owner

pboettch commented Dec 10, 2021

It should be the user-supplied loader-callback handling path-lookup and thus evaluating relative-path. Not the validator.

@manuelgavidia
Copy link
Author

Its been a while since I opened this, but as I recall :

  1. user-supplied loader-callback path-lookup as implemented here
    static void loader(const json_uri &uri, json &schema)
    , works fine for relative paths as expected on a linux file system. By this I mean that files are found/read even with it´s path been relative.
  2. the validator stores doc´s uris as written in the docs so looks like it thinks those are two different docs: doc "http://acme.com/schemas/v1/v1.json" and the doc referenced by the uri"./../v1/v1.json" from doc "http://acme.com/schemas/v2/v2.json"

@pboettch
Copy link
Owner

OK; so what would need to be added to the loader callback is return-value containing canonicalized URL after "resolution". Or maybe there should be even a dedicated callback for URL canonicalization which is then use for loading... I don't know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants