NJsonSchema icon indicating copy to clipboard operation
NJsonSchema copied to clipboard

The schema reference path '#/$defs/.....' has not been resolved.

Open bzuidgeest opened this issue 2 years ago • 6 comments

I recently found this library and wanted to try the code generation. Unfortunately I was not able to get it to work. At least not with a recent version. 10.1 seems to work with my schema but latest (10.7.2 at the moment) gives an error "The schema reference path '#/$defs/JoinConfiguration' has not been resolved."

#1004 #1514 #1301 seem to be simmilar faults. Unfortunaly I am not smart enough to find the fault myself.

using NJsonSchema;
using NJsonSchema.CodeGeneration.CSharp;

var filePath = @"Configuration.json";
var schema = JsonSchema.FromFileAsync(filePath).Result;
var generator = new CSharpGenerator(schema);
var file = generator.GenerateFile();


File.WriteAllText("test.cs", file);
{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "title": "Configuration",
    "properties": {
        "ObjectConfigurations": {
            "type": "array",
            "items": {
                "$ref": "#/$defs/ObjectTypeConfiguration"
            }
        }
    },
    "additionalProperties": false,
    "$defs": {
        "ObjectTypeConfiguration": {
            "type": "object",
            "properties": {
                "AllowProjection": {
                    "type": "boolean"
                },
                "ServiceId": {
                    "type": "string"
                },
                "ConfigurationId": {
                    "type": "string"
                },
                "ObjectType": {
                    "type": "string"
                },
                "JoinApplicationMethod": {
                    "type": "string",
                    "enum": [
                        "ApplyAll",
                        "FirstSuccesFull"
                    ],
                    "default": "ApplyAll"
                },
                "JoinConfigurations": {
                    "type": "array",
                    "items": {
                        "$ref": "#/$defs/JoinConfiguration"
                    }
                }
            },
            "additionalProperties": false,
            "required": [
                "AllowProjection",
                "ServiceId",
                "ConfigurationId",
                "ObjectType",
                "JoinApplicationMethod",
                "JoinConfigurations"
            ]
        },
        "JoinConfiguration": {
            "type": "object",
            "properties": {
                "Joins": {
                    "type": "array",
                    "items": {
                        "$ref": "#/$defs/Join"
                    }
                },
                "SortOrder": {
                    "type": "integer"
                }
            },
            "additionalProperties": false,
            "required": [
                "Joins",
                "SortOrder"
            ]
        },
        "Join": {
            "type": "object",
            "properties": {
                "ToObject": {
                    "type": "string"
                },
                "Relations": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                        "$ref": "#/$defs/JoinRelation"
                    }
                }
            },
            "additionalProperties": false,
            "required": [
                "ToObject",
                "Relations"
            ]
        },
        "JoinRelation": {
            "type": "object",
            "properties": {
                "FromPath": {
                    "type": "string"
                },
                "ToPath": {
                    "type": "string"
                }
            },
            "additionalProperties": false,
            "required": [
                "FromPath",
                "ToPath"
            ]
        }
    }
}

bzuidgeest avatar Jun 26 '22 18:06 bzuidgeest

Replace "$defs" with "definitions" then for each type you reference change to syntax "$ref": "#/definitions/{type}". Below is the your schema updated:

{ "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "title": "Configuration", "properties": { "ObjectConfigurations": { "type": "array", "items": { "$ref": "#/definitions/ObjectTypeConfiguration" } } }, "additionalProperties": false, "definitions": { "ObjectTypeConfiguration": { "type": "object", "properties": { "AllowProjection": { "type": "boolean" }, "ServiceId": { "type": "string" }, "ConfigurationId": { "type": "string" }, "ObjectType": { "type": "string" }, "JoinApplicationMethod": { "type": "string", "enum": [ "ApplyAll", "FirstSuccesFull" ], "default": "ApplyAll" }, "JoinConfigurations": { "type": "array", "items": { "$ref": "#/definitions/JoinConfiguration" } } }, "additionalProperties": false, "required": [ "AllowProjection", "ServiceId", "ConfigurationId", "ObjectType", "JoinApplicationMethod", "JoinConfigurations" ] }, "JoinConfiguration": { "type": "object", "properties": { "Joins": { "type": "array", "items": { "$ref": "#/definitions/Join" } }, "SortOrder": { "type": "integer" } }, "additionalProperties": false, "required": [ "Joins", "SortOrder" ] }, "Join": { "type": "object", "properties": { "ToObject": { "type": "string" }, "Relations": { "type": "array", "minItems": 1, "items": { "$ref": "#/definitions/JoinRelation" } } }, "additionalProperties": false, "required": [ "ToObject", "Relations" ] }, "JoinRelation": { "type": "object", "properties": { "FromPath": { "type": "string" }, "ToPath": { "type": "string" } }, "additionalProperties": false, "required": [ "FromPath", "ToPath" ] } } }

asaleh81 avatar Jul 02 '22 01:07 asaleh81

Thanks for looking into this issue. From what I can find:

In drafts 06 and 07 $defs keyword was named definitions, this has changed starting with draft 2019-09. Don’t worry, definitions can still be used (you can use any name, it doesn’t really matter).

My schema is marked with "$schema": "https://json-schema.org/draft/2020-12/schema",

So is this version of the draft not supported in njsonschema? Or is this a bug?

bzuidgeest avatar Jul 18 '22 19:07 bzuidgeest

I have the same question as @bzuidgeest. Is this a bug or it's just not supporting newer version of JSON Schema?

lpperras avatar Nov 24 '22 18:11 lpperras

Wil this be fixed any time soon?

erikmourits avatar Jul 03 '23 08:07 erikmourits

Wil this be fixed any time soon?

Condering this question its age, I believe you can answer your own question.

I switched to https://quicktype.io/. It does what I need to.

bzuidgeest avatar Jul 03 '23 12:07 bzuidgeest

The below commented code may be the cause for this.

https://github.com/RicoSuter/NJsonSchema/blob/3585d60e949e43284601e0bea16c33de4c6c21f5/src/NJsonSchema/Visitors/AsyncJsonReferenceVisitorBase.cs#L86-L96

Essentially what is happening is that the call to UpdateSchemaReferencesAsync uses an async visitor to resolve any dangling nested references stored inside ExtensionData (e.g. where $defs references are stored), but unfortunately because of this blocked code path those references will never be touched.

Replacing with a non-async visitor does work, since the above code path is uncommented in that case.

glopesdev avatar Jan 25 '24 01:01 glopesdev