Skip to content

JSON Schema recursive types #103

@Gallaecio

Description

@Gallaecio

Support recursive type definition (e.g. an item class has a field of its same type) by implementing $refs support, and use $refs as well when a given type is used more than once in the schema.

For example, given:

@dataclass
class Node:
    parent: Node | None
    children: list[Node] | None

Be able to get a JSON Schema like:

{
    "$defs": {
        "Node": {
            "type": "object",
            "properties": {
                "parent": {
                    "anyOf": [
                        {"type": "null"},
                        {"$ref": "#/$defs/Node"}
                    ]
                },
                "children": {
                    "anyOf": [
                        {"type": "null"},
                        {
                            "type": "array",
                            "items": {"$ref": "#/$defs/Node"},
                        }
                    ]
                }
            }
        },
    },
    "$ref": "#/$defs/Node"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions