Skip to content

openapi: decide whether an allOf naming only the schema itself is a reference loop #2042

In progress

Description

@OmarAlJarrah

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.FlattenedProperties reads a composition cycle to its fixpoint (docs/ir/models.md#composition), so C's flat shape is its own properties.
  • ir.IsSubtype is irreflexive, so C is not a subtype of itself and the Base edge 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

Related: #642, #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

  1. added
    type:feature
    priority:P4
    on Nov 17, 2026
  2. OmarAlJarrah commented on Nov 30, 2026

    @OmarAlJarrah
    MemberAuthor

    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). An allOf whose one entry is a $ref, writing nothing else that says something, is a link, so C: {allOf: [{$ref: C}]} and the A/B pair each draw one openapi/cyclic-ref error at their least member, every member the top type keeping nothing. A cycle through a schema writing anything else, such as properties beside its entry, stays a composition with pass/composition-cycle, and so does one composing a $ref off the loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:P4type:feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions