0024 — A recursive schema keeps its $refs as references to definitions
Status: Accepted. Amended by ADR 0025: a target
that is not recursive is kept as a definition too when its copies would make the load much
larger than the schema, and a $ref that cannot be followed is a reference to a name the
schema does not define.
Context
jlens-schema reads a JSON Schema into a tree of shapes (Shape): each field and each array’s
items own their shape, and the target of a $ref is copied in its place. A recursive schema,
one where a $ref leads back to itself ("$ref": "#", a definition that refers to itself, two
that refer to each other), has no such tree. Until 0.32 the load copied 64 levels and failed
whole with $ref nesting is too deep, which left an application nothing to check its
expressions against (roadmap 0.33 #8). Trees, linked lists, expressions and the Optional[Node]
of generated schemas (anyOf: [{"$ref": …}, {"type": "null"}]) are common.
Three ways to hold a recursive schema were weighed:
- Copy the target a fixed number of levels. The tree stays, but an expression deeper than the bound gets no completion and no warning, and the size multiplies: a node with two references to itself is 2ⁿ shapes at level n, and n definitions that all refer to each other are n! paths before one repeats.
- Copy lazily, a level when an expression first reaches it. The shapes keep one address
each, which the typing relies on, but the schema then changes while it is shared between
threads, a walk over everything (
**, a value read whole) never ends by itself, and a shape cloned away from its schema loses what it refers to. - Keep the reference, by name, and follow it where the shapes are read.
Decision
- A recursive
$refbecomes a reference by name.Schemahasdefinitions, a map from names to shapes, andShapehasreferences, the names of the definitions the value may also have the shape of. A value has the shape of any of them, and of the shape itself when it has types of its own:anyOf: [{"$ref": "#/$defs/node"}, {"type": "null"}]is a shape of typenullthat refers tonode, andallOf: [{"$ref": "#/$defs/base"}]besidepropertiesis an object that also has the fields ofbase. The typing already takes a type as a set of shapes, so the references add members to the set and nothing is merged. - Only what is recursive is a definition. The load finds the
$refs that lead back to themselves (the strongly connected components of the graph of references) before it reads a shape. Every other$refis copied in place as before: a schema that loaded until 0.32 gives the same shapes. A recursive root is itself a reference to the definition#. (Since ADR 0025, a schema whose copies would pass the load’s budget keeps the targets copied most as definitions.) - The names are those of
#/$defs/…, and the text of the$refotherwise.Schema::to_json_schemawrites the definitions under$defsand the references as$ref(one) oranyOf(several), and what it writes reads back as the same schema. - Who reads a shape follows its references, with the schema’s definitions at hand:
Schema::shape_at, the overrides, the typing (completion, hover, the diagnostics) and what an expression uses. They go as deep as the path or the expression goes. A walk over everything stops where it meets a shape again:**gives each shape once, and under a value read whole, what an expression uses lists what is under each definition once, at the place nearest to that value, first in the order of the paths. - A reference that leads nowhere is unknown. A name the schema does not define, or references that only lead to each other, leave their place without a shape: nothing is completed and nothing is warned under it.
- A variable is declared with its schema (
LanguageService::with_variable_schema), as its definitions are read with it.with_variable, which takes a shape alone, stays: the references of such a shape are unknown.
Consequences
- A path of the overrides that goes through a reference changes the definition, and so every
place that refers to it:
children[].labelin a tree describes thelabelof every node. - Annotations beside a recursive
$ref(description,title) describe that field in completion and hover. Other keywords beside it count only with atype: a shape without types that refers to a definition stands for the definition alone. ShapeandSchemagained a public field each, and aSchemabuilt by hand must givedefinitions;..Schema::default()does. Code that walksShape::fieldsandShape::itemsby itself does not see through a reference:Schema::membersgives the shapes one stands for.- What an expression uses of a recursive schema is finite, and still sound for the comparison of two schemas: a change inside a definition shows in the one place it is listed. Where that place is does not depend on the order the schema is written in.
- A schema nested more than 64 levels without recursion still fails, now with
the schema is nested too deep.