RicoSuter / NJsonSchema

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

Unable to Parse Nested Definitions #450

Open OhRickCode opened 7 years ago

OhRickCode commented 7 years ago

Hello Rico. Thank you for all the work you have done on this. It is much appreciated.

But, alas, I have encountered a possible bug. (These things are sent to test us all.)

Using this code:

string testSchema = "<insert json>";
string json = Encoding.ASCII.GetString(testSchema);
JsonSchema4 schema = NJsonSchema.JsonSchema4.FromJsonAsync(schema).Result;

If I set testSchema to this:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "properties": {
        "homes": {
            "items": {
                "$ref": "#/definitions/homes/myHome"
            }
        }
    },
    "definitions": {
        "lists": {
            "propertiesObject": {
                "$ref": "#/definitions/types/standard"
            }
        },
        "homes": {
            "myHome": {
                "properties": {
                    "inputs": {
                        "$ref": "#/definitions/lists/propertiesObject"
                    }
                }
            }
        },
        "types": {
            "standard": {
                "type": "boolean"
            }
        }
    }
}

I get this exception: InvalidOperationException: Could not resolve the path '#/definitions/lists/PropertiesObject'.

But if I flatten the schema a little (removing "lists"):

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "properties": {
        "homes": {
            "items": {
                "$ref": "#/definitions/homes/myHome"
            }
        }
    },
    "definitions": {
        "propertiesObject": {
            "$ref": "#/definitions/types/standard"
        },
        "homes": {
            "myHome": {
                "properties": {
                    "inputs": {
                        "$ref": "#/definitions/propertiesObject"
                    }
                }
            }
        },
        "types": {
            "standard": {
                "type": "boolean"
            }
        }
    }
}

then the schema is parsed successfully - even though there are still nested definitions in the schema. Hopefully the above code will reproduce the problem for you.

Thanks for all the good work. Ulric.

RicoSuter commented 7 years ago

This is currently not supported bacause "definitions" is a Dictionary<string, JsonSchema4> object and your schema cannot be correctly deserialized... i dont know if we can fix this because .net is fully typed and njs requires jsonschema4 instances for refs

OhRickCode commented 7 years ago

Ah - ok. Well, I will have to go with the work-around of flattening my schema structure.

Cheers, Ulric.

RicoSuter commented 7 years ago

Please try again with the latest version - there have been many fixes for $refs.. Maybe it now works?