qri-io / jsonschema

golang implementation of https://json-schema.org drafts 7 & 2019-09
MIT License
461 stars 54 forks source link

`$defs` doesn't work, but `definitions` does #115

Closed ieure closed 2 years ago

ieure commented 2 years ago

I'm not sure if I'm doing something wrong here or what. I'm using version 0.2.1 of this library.

I have this schema, which declares that it's draft 2019-09, and uses $defs, as draft 2019-09 requires. However, jsonschema v0.2.1 fails to codegen, saying it can't find the reference.

Here's the schema. I've confirmed that it validates against the draft 2019-09 metaschema:

{
    "$schema": "https://json-schema.org/draft/2019-09/schema",

    "$defs": {
        "name": {
            "title": "Name",
            "type": "string",
            "pattern": "^[A-Z][a-z]+$"
        }
    },

    "type": "object",
    "properties": {
        "name": {
            "$ref": "#/$defs/name"
        }
    },
    "required": ["name"]
}

Codegen fails with this error:

Failure generating structs:  processReference: reference "#/$defs/name" not found at "#/properties/name"

If I change $defs to definitions, it works fine:

{
    "$schema": "https://json-schema.org/draft/2019-09/schema",

    "definitions": {
        "name": {
            "title": "Name",
            "type": "string",
            "pattern": "^[A-Z][a-z]+$"
        }
    },

    "type": "object",
    "properties": {
        "name": {
            "$ref": "#/definitions/name"
        }
    },
    "required": ["name"]
}

Given that there are several other issues with the exact opposite problem (#97, #79, #68 — all using <= draft 7 definitions, which don't work), I have no idea why this is happening.

ieure commented 2 years ago

Wrong project, I'm a dummy.