OptimumCode / json-schema-validator

The JSON schema validation library that works with https://github.com/Kotlin/kotlinx.serialization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug: uri-reference in root `$id` results in incorrect reference resolution

jrhok opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Question

Hello,

We are currently trying to use your library with KMM in order to validate multiple JSON schemas which use references ($ref), see the following example.

foo.json
request.json

But we did not manage to use absolute path "/myproject/enums/foo" nor relative paths "enums/foo"

We ended up using myproject://enums/foo for the ids and refs, which is working fine but does not look like the best / appropriate solution.

We wanted to check with you if it might be a bug in the library or if we are misunderstanding the JSON Schema references/id usage ?

Library version

0.0.10

Anything else?

No response

Hi, @jrhok.

This is actually really interesting question. It seems like the library indeed have a problem when the root $id is not an absolute uri. I tried to load your schemes and the uri for foo.json was resolved as /myproject/enums/myproject/enums/foo that is definitely incorrect. I will create an issue to resolve this. I will try to fix it as soon as I can. Thank you for bringing this to my attention!

In the mean time, I could suggest use something like this instead (until I fix this)

foo.json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://myproject/enums/foo",
  "type": "integer",
  "enum": [
    0,
    1,
    2
  ]
}
request.json
{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://myproject/data/request",
    "type": "object",
    "properties": {
        "foo": {
            "type": "array",
            "items": {
                "$ref": "/enums/foo"
            }
        },
        "foobar": {
            "$ref": "/enums/foo"
        }
    }
}

Thank you for your quick feedbacks !
We came up with the same workaround, thanks !

Hi, @jrhok. I will merge the fix today - I hope you can use the snapshot version until the release is published.

Small note about the references:

In your example:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "myproject/data/request",
    "type": "object",
    "properties": {
        "foo": {
            "type": "array",
            "items": {
                "$ref": "enums/foo"
            }
        },
        "foobar": {
            "$ref": "/myproject/enums/foo"
        }
    }
}

you tried to use the relative reference enums/foo. You want it to be resolved into myproject/enums/foo however, this will not happen. This will be resolved into myproject/data/enums/foo.

This happens because when you use the reference that does not start with / the resolution logic is the following:

  1. Find the closest $id to the place where you use the reference;
  2. Drop the last part segment;
  3. Append the value from $ref to the URI we have on step 2.

So, the only option here would be to use the absolute reference in the main JSON schema.

I have merged changes. Once snapshot (and later release) is published I will leave a comment here. Kindly, reopen the issue if you still have the same problem after updating to the newer version

Snapshot has been published

Fixed in release 0.0.11