Skip to content

openapi: replace the schema package's predictions of what a lowering builds with what it reports #2083

In progress

Description

@OmarAlJarrah

Problem

Several functions in compilers/openapi/internal/schema restate a lowering's dispatch to answer, ahead of the lowering, what it will build or read. Each is a second copy of a decision, and nothing but its tests holds it to the first. With #1996 (at 6630476; ownHomes, nodeTypes and restKinds do not exist on main, composesAsModel and mergeConsumes do) they are:

  • ownHomes and nodeTypes (refinement.go): which bounds the node a refinement builds can hold, "read off what lowers the position rather than off the node", restating the dispatch of lowerElected, lowerUntyped and lowerUndecided.
  • composesAsModel (union.go): whether the siblings of a union lower to a Model; its comment says "It mirrors lower's dispatch".
  • mergeConsumes (allof.go): "the complete statement of what the merge keeps", restating what fillAllOf reads.
  • restKinds (refinement.go): rebuilds the own-keywords part of what admissionWalk.readOwn computes (declaredInstanceTypes(s) & valueKinds(c, s) with claimedKinds(c, s)) to leave a union out.

A mirror out of step shows as a position whose admission and node disagree. With Plain: {items: {type: string}, minItems: 1} and Wrapped: {allOf: [{items: {type: string}}]} in one openapi: 3.1.0 document:

$ morphic compile --diag-limit 0 spec.yaml | jq -c '.types | to_entries[] | select(.key | test("schemas/(Plain|Wrapped)")) | {id: (.key | split("/") | last), kind: .value.kind, inferred: (.value.provenance.inferred // null), unmodeled: ((.value.unmodeled // {}) | keys)}'
{"id":"Plain","kind":"list","inferred":"kind-inference","unmodeled":[]}
{"id":"Wrapped","kind":"scalar","inferred":null,"unmodeled":["openapi:items"]}

(#1996 at 6630476; main lowers both to a Scalar over the top type.) Wrapped is the top type keeping its items, where Plain, the same schema without the allOf, is a List.

Three more readers answer one question, "this pointer is an allOf entry", each its own way and none through the other: failedEntry (statement/table.go, last / and Atoi), writtenAsEntry (schema/admissions.go, ParseUint and the parent's last token) and splitRef (schema/allof_disjoint.go, pointer equality of adjacent branches). statement.Entry already knows.

And naming flattens an allOf on its own: titleWords (naming/titles.go) walks nested allOf to statement.MaxFlattenDepth over raw nodes and Namer.conjoined (naming/naming.go) asks statement.ReadBeside over raw pairs, beside statement.Table's flatten and conjoins. They differ on an entry whose $ref fails (the statement reads it whole) and on a reference merged past the merge bound.

Direction

A draft. Where a lowering reports which keys and which bounds it took, the mirrors answer from that report and are deleted; where it cannot (restKinds), export readOwn's own-keywords part as one function both call. Let a statement.Statement say whether its schema is an entry, and have the three readers ask it; have naming read the table.

Done when git grep -nE 'func (ownHomes|nodeTypes|composesAsModel|mergeConsumes|restKinds|failedEntry|writtenAsEntry)\b' -- compilers lists none and the package's tests and goldens are unchanged. Each mirror is its own change.

Out of scope

The disagreement shown above, which is a defect to file apart. Message text. What naming should name.

Related: #1996, #1991

Priority: P4, rule 1 (a refactor that changes no lowering, validation, IR or emit-contract behaviour). Existence of each function checked with the git grep above at 6630476.

Activity

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

    @OmarAlJarrah
    MemberAuthor

    One more channel that belongs with this, on the branch of #1996 (at 338aac4): the keys a position does not keep travel twice. unreadKeys(s, read keySet, handled []string) in compilers/openapi/internal/schema/unread.go takes what a build read as a bit set and what an election skipped or said as a list of names, for one question, which keys of the domain not to keep. unreadByReference and keepUnreadAcross thread the list, the second appending to a clipped copy, and keepUnread concatenates two lists per node. Only keys of the domain matter to it, so handled can be folded into the set at its four feeders (dispatch.skipped, dispatch.said, compositionCensusHandled, branchesSay), and the lowering then reports one thing, the keys it accounted for.

  3. OmarAlJarrah commented on Nov 19, 2026

    @OmarAlJarrah
    MemberAuthor

    Where this stands after #2132, which removes some of these and is on main at c06d94a (the commit subjects below are those of its branch). It closes nothing here: what follows is what is gone, what stands and what each waits on.

    Gone

    • nodeTypes, with lowerUntyped and lowerUndecided (refactor(compilers/openapi): decide once how a schema's types lower). How a schema whose dispatch picks no keyword family lowers is one value, typingOf: the lowering builds by it and nodeHomes reads it. lowersTypeArray reads it too (refactor(compilers/openapi): ask the type decision for a type list), and so does objectVariant, which counted the index of a type list's object variant in the types the schema writes by hand (refactor(compilers/openapi): ask the type decision for an object variant).
    • composesAsModel (refactor(compilers/openapi): ask the dispatch what a union's body builds). The dispatch among a schema's own keywords is one function, ownDispatch, which the lowering's own dispatch (dispatchAt) returns; a union's classification asks it and typingOf.
    • restKinds' copy of the own-keywords reading (refactor(compilers/openapi): read a schema's own kinds in one place): readOwn and restKinds call one ownKinds, the remedy the Direction gives, and restAdmitsObjects, which met a union holder's type with its value sets itself, calls it too (refactor(compilers/openapi): read a union holder's own kinds by ownKinds); toldWhereWritten still meets the two inside a switch arm, as ownKinds' comment says. restKinds itself stays: it asks for the kinds of a schema less its union, over the flattened entries, which the walk does not compute. Nothing held the old copy to the walk: dropping the value sets from it alone failed no test of the package.

    Stands

    • ownHomes (with nodeHomes) and mergeConsumes wait on openapi: return what a build made, so two readers stop predicting it #2133. The first answers which bounds a node holds for the voices of its allOf, the second what the merge reads of an inline entry; only the builds know either, and no build returns it. Three readers also ask before any build runs (a type list deciding whether its position keeps its principal whole, the marking of the voices the build itself reads, a visit that finds the node built), so they need a build to return what it took (openapi: return what a build made, so two readers stop predicting it #2133) and those readers to read after it.

    • failedEntry and writtenAsEntry wait on openapi: a $ref naming an allOf entry that writes a $dynamicRef reads the entry as every kind #2058. They are two parses of a pointer and they disagree (strconv.Atoi against strconv.ParseUint: a member +3 of a mapping keyed allOf, which a $ref may name, is an entry to the first alone), and both take a member of such a mapping, and an entry a $ref names, for an entry of the composition in hand. Folding them onto either parse changes what such a document lowers to. Each now names the other in a comment; both go when a statement says how its schema is reached.

    • naming's own flattening (titleWords, Namer.conjoined) stands. It disagrees with the statement on an entry whose $ref names nothing, which the statement reads whole: for a property written

      p:
        allOf:
          - $ref: '#/components/schemas/Missing'
            allOf: [{title: Deep Title}]
          - {type: object, properties: {a: {type: string}}}

      main hints the node at p deep_title, read through the failed entry's own allOf, where the statement's entries hold no title and the node would be hinted p. Reading the statement from naming therefore moves a name an identifier is rendered from, which is a versioned change. Not measured: a reference merged in past the merge bound, and a position the bindings hold no schema for.

    Corrections to the text above

    • splitRef never asked whether a pointer is an allOf entry. It asked whether a conjunct is the reference of an entry writing a $ref beside other keys, by comparing the pointers of adjacent conjuncts, and missed an entry whose other keys hold an allOf of their own. It is gone: the statement marks that reference (statement.Entry.Split) and the report of conjuncts sharing no value reads the mark (fix(compilers/openapi): name an entry's $ref apart beside its own allOf), which changes one name in that report's message and nothing else.

    • The channel in the first comment is already one set on main: unreadKeys(s, read keySet), with unreadByReference, keepUnreadAcross and dispatch.handled carrying a keySet.

    • The command under "Done when" prints nothing on any tree, main included: git grep -E takes no \b here (git 2.56, macOS). This one works:

      $ git grep -nP 'func (ownHomes|nodeTypes|composesAsModel|mergeConsumes|restKinds|failedEntry|writtenAsEntry)\b' -- compilers

      On main it lists the seven. On the pull request's head it lists five: ownHomes, mergeConsumes, failedEntry and writtenAsEntry, which stand as above, and restKinds, which should leave the list.

  4. OmarAlJarrah commented on Nov 27, 2026

    @OmarAlJarrah
    MemberAuthor

    One more prediction of what a lowering keeps, in the same package and not listed above: declaresPositionScoped (compilers/openapi/internal/schema/declaration.go, morphic @cfb366317). It gates whether a position hoists an alias on a list of what attachDeclaredAnnotations, internReferenceAlias, declarationOn and PreserveUnknownKeywords keep, and its comment says it "must stay in step with what those actually keep".

    It has not stayed in step. #138, #1053 and #2009 were each a keyword dropped where the list and the keepers disagreed, and each fix (#149, #1217, #1996) added the keyword to the list. #1053's direction, one predicate both read, was not taken:

    $ git grep -n 'must stay in step' -- compilers
    compilers/openapi/internal/schema/declaration.go:159:// It is the gate on hoisting an alias, so it must stay in step with what those

    For this site the issue is done when the gate asks the keepers, or reads what they report, and that git grep lists nothing.

  5. OmarAlJarrah commented on Nov 29, 2026

    @OmarAlJarrah
    MemberAuthor

    Where this stands at #2354's head, 1cf9539a7. Of the seven functions this issue's Done-when names, two went on main with #2132 and the pull request answers three, as it does declaresPositionScoped from the comment of 2026-11-27. failedEntry and writtenAsEntry stand until a later part of it lands, and this issue stays open until then.

    The Done-when grep, in the form that runs, lists four:

    $ git grep -nP 'func (\([^)]*\) )?(ownHomes|nodeTypes|composesAsModel|mergeConsumes|restKinds|failedEntry|writtenAsEntry)\b' -- compilers
    compilers/openapi/internal/schema/admission.go:216:func writtenAsEntry(pointer jsontext.Pointer) bool {
    compilers/openapi/internal/schema/allof.go:560:func mergeConsumes(bs *statement.Schema, key string, flattened bool) bool {
    compilers/openapi/internal/schema/decision.go:659:func restKinds(loweringContext lowering.Ctx, schema *statement.Schema, comp allOfComposition) (kindSet, bool) {
    compilers/openapi/internal/statement/table.go:200:func failedEntry(schema *Schema) bool {
    Function Outcome
    ownHomes replaced: it is boundsHeldAt, computed once by the frame that dispatches a position and passed to the builders (7eb4b822e, a union's homes at fa2465977); #2133 says why no build can return it
    nodeTypes, composesAsModel gone on main since #2132
    mergeConsumes kept, with a comment saying why: the merge reads every inline entry's properties and required whatever the entry builds, so what it reads is known before any build runs (3e493d576)
    restKinds kept, with a comment saying why: no build computes the kinds of a schema less its union, so none can return them (3e493d576)
    failedEntry, writtenAsEntry stand. A later part of #2354 reads an allOf entry as a fact of the composition listing it, as #2058 asks, which removes both
    declaresPositionScoped rebuilt from its keepers (c96c2fdec, eceb87783): the gate is the disjunction of what each keeper writing on a hoisted alias may keep, each read through the list or predicate that keeper reads (annotation.ReadKeeps, declaresBound, keepsUnknownKeywords and the rest). It is now a cheap necessary test before a dry run: the site builds the alias on no registry, runs its keepers on it, and hoists it only where it holds something (9be615eb3), so what the keepers keep decides. Its "must stay in step" comment is gone: git grep -n 'must stay in step' -- compilers lists nothing

    Rebuilding the gate fixed the drift #2324 files: an expanded $dynamicRef or $recursiveRef written alone at a position no carrier holds hoisted an alias holding only its Base, and now lowers to the type of the position it names, as a $ref there does. Against a binary of 7d7208585, the head's testdata/ (314 specs) differs in three specs, by one such alias each. The golden spec's IR and diagnostics are byte-identical but for the version stamp, and so are those of 1,863 OpenAPI 3 specs from a corpus of public API descriptions.

    naming reads a schema's conjuncts from its statement (b03507d81): titleWords reads the statement's entries rather than walking the raw allOf, and Namer.conjoined reads whether a schema conjoins with its reference from the statement too. The name the comment of 2026-11-19 predicted moves: compiled at 7d7208585, the property p there is hinted deep_title, and at the head it is hinted p. That moves a name an identifier is rendered from, so it is recorded under the pull request's IRVersion row (0.74.0). The names pin (TestIRVersion_PinsTheNames) is unchanged, since no position it lists writes such an entry, and the golden spec's IR is byte-identical but for the version stamp.

  6. OmarAlJarrah commented on Nov 30, 2026

    @OmarAlJarrah
    MemberAuthor

    Finished in #2354, which closes this on merge. failedEntry went at 7213104, when an entry whose $ref names nothing became two conjuncts like any other, and writtenAsEntry at acf4512, when an allOf entry became a fact of the composition listing it. The Done-when grep now lists mergeConsumes and restKinds alone, each kept with a comment saying why, as the comment of 2026-11-29 records. One correction to that comment: at c0ff23b the property p there is hinted deep_title again, as on main, since the failed entry's own allOf is now read; no name moves.

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:chore

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions