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

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

Open
bzuidgeest opened this issue Jun 26, 2022 · 6 comments
Open

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

bzuidgeest opened this issue Jun 26, 2022 · 6 comments

Comments

@bzuidgeest
Copy link

bzuidgeest commented Jun 26, 2022

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"
            ]
        }
    }
}
@asaleh81
Copy link

asaleh81 commented Jul 2, 2022

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"
]
}
}
}

@bzuidgeest
Copy link
Author

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?

@lpperras
Copy link

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

@erikmourits
Copy link

Wil this be fixed any time soon?

@bzuidgeest
Copy link
Author

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.

@glopesdev
Copy link

The below commented code may be the cause for this.

if (obj is JsonSchema schema)
{
// Do not follow as the root object might be different than _rootObject, fixes https://github.com/RicoSuter/NJsonSchema/issues/588
// i.e. we should only visit the objects which might be references but not resolve
// because usually the resolved object is touched in another path (not via reference)
//if (schema.Reference != null)
//{
// await VisitAsync(schema.Reference, path, null, checkedObjects, o => schema.Reference = (JsonSchema)o).ConfigureAwait(false);
//}

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.

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

No branches or pull requests

5 participants