Repository navigation
Description
Problem
A schema whose allOf holds one $ref to that schema itself lowers to a Model whose Base is itself, where the same schema written as a bare $ref is a reported loop that lowers to the top type:
openapi: 3.1.0
info: {title: t, version: "1"}
paths: {}
components:
schemas:
C: {allOf: [{$ref: '#/components/schemas/C'}]}$ morphic compile --verify --diag-limit 0 spec.yaml >ir.json; echo "exit $?"
warning pass/composition-cycle spec.yaml#/components/schemas/C: composition never ends: base t/openapi/components/schemas/C leads back into a cycle of bases and mixins
exit 0
$ jq -c '.types["t/openapi/components/schemas/C"] | {kind, base: .base.target, props: [.properties[]?.name.source]}' ir.json
{"kind":"model","base":"t/openapi/components/schemas/C","props":[]}
$ jq -c '[.diagnostics[] | [.code, .severity]]' ir.json
[["pass/composition-cycle","warning"]]The document draws the one warning, from pass.Validate, and nothing from the compiler. The bare spelling is handled by the compiler (docs/compilers/openapi.md#reference-cycles):
$ cat bare.yaml # C: {$ref: '#/components/schemas/C'}
$ morphic compile --verify --diag-limit 0 bare.yaml >ir.json; echo "exit $?"
error openapi/cyclic-ref bare.yaml#/components/schemas/C: cyclic $ref: the chain of $refs through #/components/schemas/C comes back to where it started and never reaches a schema
exit 1
$ jq -c '.types["t/openapi/components/schemas/C"] | {kind, base: .base.target}' ir.json
{"kind":"scalar","base":"t/prim/any"}A cycle of two (A: {allOf: [B]}, B: {allOf: [A]}) compiles the same way: two models, each the other's base, two pass/composition-cycle warnings and nothing else. With a property beside the entry (C: {allOf: [{$ref: C}], properties: {a: {type: string}}}) the model also declares a.
JSON Schema fixes no set of values for such a schema. C is satisfied by an instance exactly when C is, an equation every set of values satisfies, and an implementation evaluating it recurses at one instance location without end, which Core 2020-12, in its discussion of loops, treats as an error in the schema, not a valid schema with a value set. Nothing in the document says that. What a consumer of the IR can do with the result:
ir.FlattenedPropertiesreads a composition cycle to its fixpoint (docs/ir/models.md#composition), soC's flat shape is its own properties.ir.IsSubtypeis irreflexive, soCis not a subtype of itself and theBaseedge is no subtype relation.- A target has no class to extend: a base of length one is an inheritance edge no language can write, so each target must discover the cycle and drop the edge.
Expected, to decide: either the position is the loop docs/compilers/openapi.md#reference-cycles already reports (an openapi/cyclic-ref error, lowered to the top type) because an allOf of references to itself never reaches a schema, or the document keeps the cycle as the IR's documented cycle and docs/compilers/openapi.md says so beside the loop rule.
Cause
reference-cycles finds a chain of $refs that never reaches a schema. An allOf of one $ref is not read as a link of that chain: docs/compilers/openapi.md#allof-refinement keeps an allOf a composition where "the read does not finish: a cycle", so the position lowers to a Model over its entry, whose Base closes on itself. Only pass.Validate notices, as the pass/composition-cycle warning that docs/ir/models.md#composition ("Knots") describes (morphic @9562ea46d).
Direction
A draft: treat an allOf whose entries are $refs and which writes nothing else the position keeps, as a link of the $ref chain, so that a closed chain of such links, $ref or single-entry allOf alike, is one openapi/cyclic-ref at its least member. A cycle that reaches a schema declaring something of its own (properties beside the entry) is a different case: the model is a real type over an edge that never ends, and keeps pass/composition-cycle.
Out of scope
- Whether the IR may hold a composition cycle at all, settled for the IR in
docs/ir/models.md#compositionand reported bypass/composition-cycle(pass: report inheritance cycles and types with no finite value #642). - The member settlement of two models composing each other (openapi: two models composing each other can settle one member to two types #1995).
Priority: P4, rule 2 (guards against a corrupt spec: a schema that never ends). A compile of openapi.json for the warning:
$ morphic compile --diag-limit 0 openapi.json | jq '[.diagnostics[] | select(.code == "pass/composition-cycle")] | length'
0(spec @8b1a4518, morphic @9562ea46d).
Activity
test(compilers/openapi): pin the reach decisions' issue rows
OmarAlJarrah commentedon Nov 30, 2026
Decided and fixed in #2354 at acf4512, which closes this on merge: a pure link is part of the reference chain (
docs/compilers/openapi.md#reference-cycles). AnallOfwhose one entry is a$ref, writing nothing else that says something, is a link, soC: {allOf: [{$ref: C}]}and theA/Bpair each draw oneopenapi/cyclic-referror at their least member, every member the top type keeping nothing. A cycle through a schema writing anything else, such aspropertiesbeside its entry, stays a composition withpass/composition-cycle, and so does one composing a$refoff the loop.