RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.38k stars 532 forks source link

Error when resolving links in external file #1654

Open dinar007007 opened 9 months ago

dinar007007 commented 9 months ago

Hello!

The error occurs if a external file contains a ref to an object from the same external file. Error text - System.InvalidOperationException: Could not resolve the path '#/myInt' Examples:

njsonschema.json

{
    "type": "object",
    "properties": {
        "bar": {
            "$ref": "./common.json#/myObject"
        }
    }
}

common.json

{
    "myObject": {
        "type": "object",
        "properties": {
            "myIntProp": {
                "$ref": "#/myInt"
            }
        }
    },
    "myInt": {
        "type": "integer"
    }
}

If I add the following definition to the njsonschema.json file, then the schema is read successfully

...
    "definitions": {
        "collection": {
            "$ref": "./common.json"
        }
    }

I don't understand something in the JSON schema standard or, for now, such resolution of refs is not supported in NJsonSchema?

RicoSuter commented 9 months ago

The referenced document must also be a valid json schema file and common.json is not (ie "myObject" is not a valid property of the root object)... you'd need to put the collection of definitions in "definitions":

{
   "definitions": {
       "myObject": {
          ...
       }
   }
}

and reference it via "./common.json#/definitions/myObject"