Skip to content

openapi: decide whether an allOf whose read is cut short lowers to the top type keeping the allOf rather than to a model #2070

In progress

Description

@OmarAlJarrah

Problem

An allOf whose kinds are not an object alone lowers to a use of one of its conjuncts, not to a Model (docs/ir/models.md#an-allof-no-object-satisfies: "A Model says its value is an object"). Where the read of the conjuncts is cut short, docs/compilers/openapi.md#allof-refinement keeps the allOf composing, and the composition is a Model whatever its conjuncts say, so a schema that admits only strings lowers to an object type:

$ python3 -c 'b = "{type: string}"
for _ in range(34): b = "{allOf: [" + b + "]}"
print("openapi: 3.1.0\ninfo: {title: t, version: \"1\"}\npaths: {}\ncomponents:\n  schemas:\n    S: " + b)' > nest34.yaml
$ morphic compile --diag-limit 0 nest34.yaml >ir.json; echo "exit $?"
info openapi/degraded-construct nest34.yaml#/components/schemas/S/allOf/0/allOf/0/…: inline allOf branch is nested past the flattening depth of 32, so its own allOf was not flattened, and the branch is kept verbatim under Unmodeled
exit 0
$ jq -c '.types | to_entries[] | select(.key | test("schemas/S$")) | {kind: .value.kind, base: .value.base.target, unmodeled: (.value.unmodeled // {} | keys | length)}' ir.json
{"kind":"model","base":null,"unmodeled":1}

(the pointer in the report repeats allOf/0 34 times and is shortened here.) The same schema with 33 nested allOfs is a Scalar over string with no diagnostic. The cut can come from the other limits the rule lists too. An allOf of 65 {type: string} entries (64 is a Scalar over string) lowers to a Model with no member and a warning that says what the lowering does: "inline allOf branch declares a type that is not an object, which the composed model asserts it is" (65 reports, one per entry). A const whose value no reading can convert is another, but it also draws an error, so the document is refused there.

The Model is an empty object type for a schema that admits strings. Two tests pin it: TestRefinement_ACappedBranchStaysAComposition (a string under 34 nested allOfs) and TestRefinement_AConstUnreadableStaysAComposition. The behaviour is the one allof-refinement documents for a read cut short, so what is open is whether the documented fallback is the right one. It disagrees with the statement the models document makes of a Model, and with the rule the OpenAPI compiler states for the position (a Model only where the kinds it lowers to are an object alone), which a position whose kinds were not read cannot show.

main (morphic @9562ea46d) and #1996 (at 6630476) lower the specs above alike, apart from the number of entries the 65-entry case keeps (65 on main, 1 with #1996).

Cause

Among the exits that keep an allOf composing, decide (compilers/openapi/internal/schema/refinement.go) returns no refinement when the read does not finish (w.incomplete()) or refinableBranches refuses the conjuncts, and the allOf then lowers as the composition (lowerAllOf, allof.go), a Model whose Base, Mixins and members come from the conjuncts it could read (morphic @9562ea46d).

Direction

A draft; the answer differs by what the read missed.

  1. Keep the Model, and say so: the composition is the fallback because it holds every conjunct, and the warning of the over-wide case already says the model asserts an object. It costs a consumer reading Kind an object type for a schema whose conjuncts may admit none, and the nested case draws only an info.
  2. The top type keeping the allOf verbatim where the part of the read that finished names no object (no conjunct writes properties, required or the like, and none names an object kind), and the Model where it does. It claims least: the position is "any value" with the whole allOf under Unmodeled, as for a schema the compiler cannot read. It costs the composition where the unread conjuncts are the objects that make it one (a 65-entry composition of object $refs is a Model over a Base and 64 Mixins, and the top type loses that), so the rule must say which finished part decides.
  3. Raise the limits so that a schema reaching them is no schema the compiler is expected to lower, and keep option 1 for what remains; the nested case needs 34 levels and the wide case 65 entries, which no document written by hand reaches.

Either answer is stated in docs/compilers/openapi.md#allof-refinement and pinned by the two tests above, which then assert the kind rather than only the Go type.

Out of scope

Related: #1996, #2064, #1568

Priority: P4, rule 2 (a limit or an unconvertible value: the cut is reached only by adversarial depth or width, a cycle or a value no JSON holds). jq '[.. | objects | select(has("allOf")) | .allOf | length] | max' openapi.json → 3, and no openapi/budget-exceeded diagnostic in the golden spec compiled by main or #1996 → 0 (cloudflare/api-schemas@8b1a4518; morphic @9562ea46d and #1996 at 6630476).

Activity

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

    @OmarAlJarrah
    MemberAuthor

    Two residues after #1996 (at 248aba1), which the issue does not say. Its out-of-scope list leaves value sets sharing no member aside as composing an empty Model; #1996 changes that, and it leaves kinds sharing none as they were.

    openapi: 3.1.0
    info: {title: t, version: "1"}
    paths: {}
    components:
      schemas:
        Str: {type: string}
        Int: {type: integer}
        Kinds: {allOf: [{type: string}, {type: integer}]}
        KindRefs: {allOf: [{$ref: '#/components/schemas/Str'}, {$ref: '#/components/schemas/Int'}]}
        EnumEntries: {allOf: [{enum: [a, b]}, {enum: [c, d]}]}
        EnumBeside: {enum: [a, b], allOf: [{enum: [c, d]}]}
    $ morphic compile --diag-limit 0 spec.yaml >ir.json 2>diag.txt; echo "exit $?"
    exit 0
    $ grep '^warning' diag.txt
    warning openapi/degraded-construct spec.yaml#/components/schemas/Kinds: the conjuncts of this allOf share no value (allOf/0: string, allOf/1: integer), so no instance satisfies this schema, which a composition cannot state
    warning openapi/degraded-construct spec.yaml#/components/schemas/KindRefs: the conjuncts of this allOf share no value (allOf/0: string, allOf/1: integer), so no instance satisfies this schema, which a composition cannot state
    warning openapi/degraded-construct spec.yaml#/components/schemas/EnumEntries: the conjuncts of this allOf share no value (allOf/0: 2 values, allOf/1: 2 values), so no instance satisfies this schema, which the top type it lowered as cannot state
    warning openapi/degraded-construct spec.yaml#/components/schemas/EnumBeside: the conjuncts of this allOf share no value (allOf/0: 2 values, enum: 2 values), so no instance satisfies this schema, which the enum it lowered as cannot state
    $ jq -c '.types | to_entries[] | select(.key | test("schemas/(Kinds|KindRefs|EnumEntries|EnumBeside)$")) | {id: (.key | split("/") | last), kind: .value.kind, base: (.value.base.target // null), members: [.value.members[]?.value.str], unmodeled: (.value.unmodeled // {} | keys)}' ir.json
    {"id":"EnumBeside","kind":"enum","base":null,"members":["a","b"],"unmodeled":["openapi:allOf/0"]}
    {"id":"EnumEntries","kind":"scalar","base":"t/prim/any","members":[],"unmodeled":["openapi:allOf/0","openapi:allOf/1"]}
    {"id":"KindRefs","kind":"model","base":null,"members":[],"unmodeled":["openapi:$ref/0","openapi:$ref/1"]}
    {"id":"Kinds","kind":"model","base":null,"members":[],"unmodeled":["openapi:allOf/0","openapi:allOf/1"]}

    (a) Kinds and KindRefs are an empty Model, with the warning that says no instance satisfies them. Conjuncts whose kinds share none admit no value, so what they lower to is the question this issue asks of a read cut short, and here the read finished. On main (9562ea4) KindRefs is a Model with Str and Int as mixins; with #1996 it keeps both $refs verbatim and has no base.

    (b) The same unsatisfiable schema about a value set lowers three ways. Two entries sharing no member (EnumEntries) are the top type keeping each entry under Unmodeled. An enum written beside the allOf (EnumBeside) is the Enum of that enum's members, which allOf/0 contradicts, kept under openapi:allOf/0; each warning says what its lowering cannot state. And Kinds is an object type. On main EnumEntries was an empty Model with infos only and EnumBeside the same Enum with an info, so #1996 moved the entries' spelling and not the one beside the allOf. Whichever answer this issue settles for an allOf no value satisfies (the top type keeping each conjunct, or a Model), the three spellings should give it.

  3. OmarAlJarrah commented on Nov 18, 2026

    @OmarAlJarrah
    MemberAuthor

    One more case the answer here decides: a member of a cycle of allOfs that writes object keywords
    and no type.

    openapi: 3.1.0
    info: {title: t, version: '1'}
    paths: {}
    components:
      schemas:
        N: {properties: {x: {type: string}}, allOf: [{$ref: '#/components/schemas/N2'}]}
        N2: {allOf: [{$ref: '#/components/schemas/N'}]}
        M: {properties: {x: {type: string}}, allOf: [{$ref: '#/components/schemas/M2'}]}
        M2: {properties: {y: {type: string}}}

    With #1996 (at c2bd480), N is a Model of x whose Base is N2, with the two
    composition never ends findings, and its provenance names no kind-inference; M is a Model
    whose Base is M2 and names it. With --opt kind-inference=false, N is the same Model and
    M a Scalar keeping properties: N is a Model by this issue's fallback, not by the policy,
    so the node carries no mark, as docs/compilers/openapi.md#shape-applicator marks only a node the
    policy builds. The compiler's own reading of N's kinds (lowersTo, which reads a cut read's kinds
    off the keywords it writes) does count them as the policy's, which the dead-branch check and the
    election read. If the answer here moves a cut read off the Model, a position like N should take
    the kinds the policy reads, marked, as M does; if it keeps the Model, the two readings stay
    apart and that is worth a sentence in the rule. main (morphic @38da05c38) marks neither.

  4. OmarAlJarrah commented on Nov 30, 2026

    @OmarAlJarrah
    MemberAuthor

    Decided and fixed in #2354 at 7213104, which closes this on merge: an allOf composes a Model only where the kinds it reads are an object alone (docs/compilers/openapi.md#allof-refinement). With the specs here:

    • {type: string} under 34 nested allOfs: the top type, keeping the allOf whole (degraded_lowering) beside the depth report.
    • 65 entries {type: string}: the Scalar over string, with no finding.
    • Kinds, KindRefs: a closed Model with no member, keeping each conjunct. EnumEntries, EnumBeside: the closed empty Enum of string, keeping every set. Each draws one openapi/unsatisfiable-schema warning.
    • N composes N2, marked kind-inference as M is.
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