Newtonsoft.Json.Schema icon indicating copy to clipboard operation
Newtonsoft.Json.Schema copied to clipboard

"Object reference not set to an instance of an object." exception when unevaluatedProperties defines specific types

Open SkyKnight opened this issue 1 year ago • 0 comments

This is my intentionally complicated example JSON schema:

{
    "$id": "configuration-entry",
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "allOf": [
        {
            "if": {
                "properties": { "type": { "const": "form" } }
            },
            "then": {
                "allOf": [
                    {
                        "properties": {
                            "components": {
                                "type": "object",
                                "additionalProperties": {
                                    "type": "object",
                                    "allOf": [
                                        {
                                            "if": {
                                                "oneOf": [
                                                    {"properties": { "type": { "const": "text" } }},
                                                    {"properties": { "type": { "const": "text-plus" } }}
                                                ]
                                            },
                                            "then": {
                                                "unevaluatedProperties": {
                                                    "type": ["string", "number"]
                                                }
                                            }
                                        },
                                        {
                                            "if": {
                                                "properties": { "type": { "const": "image" } }
                                            },
                                            "then": {
                                                "unevaluatedProperties": {
                                                    "type": ["string", "number"]
                                                }
                                            }
                                        }
                                    ],
                                    "properties": {
                                        "type": {
                                            "type": "string"
                                        }
                                    },
                                    "required": ["type"]
                                }
                            }
                        },
                        "required": ["components"]
                    }
                ]
            }
        }
    ],
    "properties": {
        "type": {
            "type": "string"
        }
    },
    "required": ["type"]
}

And my example wrong simple JSON file:

{
    "type": "form",
    "components": {
        "first": {
            "type": "text",
            "param1": 1,
            "alignement": "left"
        },
        "second": {
            "type": "image",
            "param1": "1",
            "meta": {
                "location": "nowhere",
                "date": "now"
            }
        },
        "third": {
            "type": "text-plus",
            "param1": 1,
            "alignement": "left"
        }
    }
}

Expectation: validation error showing me that $.components.second.meta element is invalid (should be number or string instead of object).

Current situation:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Newtonsoft.Json.Schema.Infrastructure.Validation.SchemaScope.GetValidationErrors()
   at Newtonsoft.Json.Schema.Infrastructure.Validation.ObjectScope.EvaluateTokenCore(JsonToken token, Object value, Int32 depth)
   at Newtonsoft.Json.Schema.Infrastructure.Validation.Validator.ValidateCurrentToken(JsonToken token, Object value, Int32 depth)
   at Newtonsoft.Json.Schema.JSchemaValidatingReader.ValidateCurrentToken()
   at Newtonsoft.Json.Schema.SchemaExtensions.Validate(JToken source, JSchema schema, SchemaValidationEventHandler validationEventHandler)
   at Newtonsoft.Json.Schema.SchemaExtensions.IsValid(JToken source, JSchema schema, IList`1& errors)

ajv command line shows correct JSON data error:

>ajv -s schema.json -d data.json --spec=draft2020
strict mode: use allowUnionTypes to allow union type keyword at "configuration-entry#/allOf/0/then/allOf/0/properties/components/additionalProperties/allOf/0/then/unevaluatedProperties" (strictTypes)
strict mode: use allowUnionTypes to allow union type keyword at "configuration-entry#/allOf/0/then/allOf/0/properties/components/additionalProperties/allOf/1/then/unevaluatedProperties" (strictTypes)
data.json invalid
[
  {
    instancePath: '/components/second/meta',
    schemaPath: '#/allOf/0/then/allOf/0/properties/components/additionalProperties/allOf/1/then/unevaluatedProperties/type',
    keyword: 'type',
    params: { type: [Array] },
    message: 'must be string,number'
  }
]

library version: <PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.15" />

SkyKnight avatar Aug 17 '23 12:08 SkyKnight