Skip to content

Recursive (Circular) model $ref doesn't dereference unless wrapped in anyOf (sibling keys alongside $ref keys) #370

Open
@iggyfisk

Description

@iggyfisk

Pydantic recently released an update changing their JSON schema generator.

Previously the generated schema looked like

{
    "$defs": {
        "RecursiveModel": {
            "additionalProperties": false,
            "properties": {
                "value": {
                    "description": "A string",
                    "type": "string"
                },
                "children": {
                    "description": "Children with strings",
                    "items": {
                        "$ref": "#/$defs/RecursiveModel"
                    },
                    "type": "array"
                }
            },
            "required": [
                "value",
                "children"
            ],
            "title": "RecursiveModel",
            "type": "object"
        }
    },
    "allOf": [
        {
            "$ref": "#/$defs/RecursiveModel"
        }
    ]
}

which dereferences properly with json-schema-ref-parser. However, now the generated schema looks like

{
    "$defs": {
        "RecursiveModel": {
            "additionalProperties": false,
            "properties": {
                "value": {
                    "description": "A string",
                    "type": "string"
                },
                "children": {
                    "description": "Children with strings",
                    "items": {
                        "$ref": "#/$defs/RecursiveModel"
                    },
                    "type": "array"
                }
            },
            "required": [
                "value",
                "children"
            ],
            "title": "RecursiveModel",
            "type": "object"
        }
    },
    "$ref": "#/$defs/RecursiveModel"
}

which results in an unresolved '$ref': '#/%24defs/RecursiveModel/properties/children/items' after $RefParser.dereference(). As far as I can tell the newer schema should be valid.

Here's a full script showing the difference, and unresolved $ref

import $RefParser from "@apidevtools/json-schema-ref-parser";

async function main() {
  console.log("----OLD----");
  const old_dereferenced = await $RefParser.dereference({
    "$defs": {
      "RecursiveModel": {
        "additionalProperties": false,
        "properties": {
          "value": {
            "description": "A string",
            "type": "string",
          },
          "children": {
            "description": "Children with strings",
            "items": {
              "$ref": "#/$defs/RecursiveModel",
            },
            "type": "array",
          },
        },
        "required": ["value", "children"],
        "title": "RecursiveModel",
        "type": "object",
      },
    },
    "allOf": [
      {
        "$ref": "#/$defs/RecursiveModel",
      },
    ],
  });
  console.log(old_dereferenced["$defs"]["RecursiveModel"]["properties"]);

  console.log("----NEW----");
  const new_dereferenced = await $RefParser.dereference({
    "$defs": {
      "RecursiveModel": {
        "additionalProperties": false,
        "properties": {
          "value": {
            "description": "A string",
            "type": "string",
          },
          "children": {
            "description": "Children with strings",
            "items": {
              "$ref": "#/$defs/RecursiveModel",
            },
            "type": "array",
          },
        },
        "required": ["value", "children"],
        "title": "RecursiveModel",
        "type": "object",
      },
    },
    "$ref": "#/$defs/RecursiveModel",
  });
  console.log(new_dereferenced["$defs"]["RecursiveModel"]["properties"]);
}

main();

I get the output

----OLD----
<ref *1> {
  value: { description: 'A string', type: 'string' },
  children: {
    description: 'Children with strings',
    items: {
      additionalProperties: false,
      properties: [Circular *1],
      required: [Array],
      title: 'RecursiveModel',
      type: 'object'
    },
    type: 'array'
  }
}
----NEW----
{
  value: { description: 'A string', type: 'string' },
  children: {
    description: 'Children with strings',
    items: {
      '$defs': [Object],
      '$ref': '#/%24defs/RecursiveModel/properties/children/items'
    },
    type: 'array'
  }
}

Is this expected? Should I be using some different option?

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