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

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

Open
SkyKnight opened this issue Aug 17, 2023 · 0 comments

Comments

@SkyKnight
Copy link

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" />

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

1 participant