Skip to content

V2019-09 $ref cannot be resolved in array 'additionalItems' #493

@carolkao

Description

@carolkao

Hi, I encounter a reference problem in array 'additionalItems'. Here is my schema:

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "parameters": {
      "type": "array",
      "uniqueItems": true,
      "items": [
        {
          "type": "object",
          "required": [
            "name",
            "value"
          ],
          "properties": {
            "name": {
              "const": "param-required"
            },
            "value": {
              "anyOf": [
                {
                  "$ref": "#/$defs/test-ref1"
                },
                {
                  "$ref": "#/$defs/test-ref2"
                }
              ]
            }
          }
        }
      ],
      "additionalItems": {
        "oneOf": [
          {
            "type": "object",
            "required": [
              "name",
              "value"
            ],
            "properties": {
              "name": {
                "const": "param-optional"
              },
              "value": {
                "anyOf": [
                  {
                    "$ref": "#/$defs/test-ref1"
                  },
                  {
                    "$ref": "#/$defs/test-ref2"
                  }
                ]
              }
            }
          }
        ]
      },
      "minItems": 1,
      "maxItems": 2
    }
  },
  "$defs": {
    "test-ref1": {
      "type": "string",
      "pattern": "^\\{\\{.+\\}\\}$"
    },
    "test-ref2": {
      "type": "integer"
    }
  }
}

When I try to validate the following data:

{
  "parameters": [
    {
      "name": "param-required",
      "value": 123
    },
    {
      "name": "param-optional",
      "value": "{{test}}"
    }
  ]
}

I got the exception:

com.networknt.schema.JsonSchemaException: anyOf/$ref: Reference #/$defs/test-ref1 cannot be resolved
	at com.networknt.schema.RefValidator.<init>(RefValidator.java:48)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

Seems when looping to 'findAncestor' from "$ref": "#/$defs/test-ref1", the 'root' is 'additionalItems' but not the root of the whole document. All reference works fine in 'items' section, only fails at 'additionalItems'. May I know if it is a bug? Or I need to copy all the '$defs' into 'additionalItems'?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions