Skip to content

openapi: a $ref naming an allOf entry that writes a $dynamicRef reads the entry as every kind #2058

In progress

Description

@OmarAlJarrah

Problem

A $ref whose pointer names an allOf entry denotes that entry's schema alone. When the entry is {$dynamicRef: '#m'} and #m names a string schema, the reference admits strings (JSON Schema Core 2020-12 §8.2.3.2: the $dynamicRef resolves to the outermost $dynamicAnchor of that name in the dynamic scope, here the document's M). The compiler expands it so when the entry is the reference's target, but the conjunct reading that decides an enclosing allOf skips an entry's $dynamicRef and reads the entry as admitting every kind:

openapi: 3.1.0
info: {title: t, version: "1"}
paths: {}
components:
  schemas:
    M: {$dynamicAnchor: m, type: string}
    Q:
      allOf:
        - {type: object, properties: {a: {type: string}}}
        - {$dynamicRef: '#m'}
    ViaEntry:
      allOf:
        - {$ref: '#/components/schemas/Q/allOf/1'}
        - {type: object, properties: {b: {type: string}}}
    ViaM:
      allOf:
        - {$ref: '#/components/schemas/M'}
        - {type: object, properties: {b: {type: string}}}
$ morphic compile --diag-limit 0 spec.yaml >ir.json; echo "exit $?"
info openapi/degraded-construct spec.yaml#/components/schemas/Q/allOf/1: inline allOf branch is merged in place, and nothing reads the rest of it; the branch ($dynamicRef) is kept verbatim under Unmodeled
warning openapi/degraded-construct spec.yaml#/components/schemas/ViaM: the conjuncts of this allOf share no value (allOf/0: string, allOf/1: object), so no instance satisfies this schema, which a composition cannot state
info openapi/dynamic-ref-expanded spec.yaml#/components/schemas/Q/allOf/1/$dynamicRef: $dynamicRef expanded to "t/openapi/components/schemas/M", the one matching $dynamicAnchor in this document
exit 0
$ jq -c '.types | to_entries[] | select(.key | test("schemas/(ViaEntry|ViaM|Q/allOf/1)$")) | {id: (.key | split("/") | .[3:] | join("/")), kind: .value.kind, base: (.value.base.target // "" | split("/") | last), kept: (.value.unmodeled // {} | map_values(.reason))}' ir.json
{"id":"schemas/Q/allOf/1","kind":"scalar","base":"M","kept":{}}
{"id":"schemas/ViaEntry","kind":"model","base":"1","kept":{}}
{"id":"schemas/ViaM","kind":"model","base":"M","kept":{}}

ViaM and ViaEntry admit the same values: a string that is also an object, which no instance is. ViaM draws the warning and ViaEntry draws none. ViaEntry is a Model whose base is the node at Q/allOf/1, a Scalar over M, which is a string, with nothing reported at ViaEntry. The same pointer, Q/allOf/1/$dynamicRef, is also told it was kept verbatim (by the report at Q/allOf/1) and that it expanded to M.

With #1996 (at 766a6e4) ViaEntry gets an info that its $ref is "not an object alone, and a model composes only models" and is kept verbatim under openapi:$ref, so it no longer composes over a string; it still draws no warning where ViaM does, and the report at Q/allOf/1/$dynamicRef says "was not expanded because it is written on an allOf entry beside another conjunct" beside the "expanded to M" report at the same pointer.

Cause

dynamicExpansionOf (compilers/openapi/internal/schema/admissions.go) returns no expansion for a statement whose pointer names an allOf entry (writtenAsEntry), because no allOf position expands one: the reading is keyed by where the keyword is written, not by who reads it, so a $ref landing on the entry gets that reading too (#1996 at 766a6e4). On main (9562ea4) the same spec draws no warning for ViaEntry, which is a Model over the entry's node.

Direction

A draft: key the reading on how the entry is reached. An entry of an allOf the position lowers masks no kind there; the same schema reached by a $ref is a schema of its own, and its $dynamicRef expands, as it does at Q/allOf/1 as a node, so ViaEntry reads as ViaM does. The alternative is to leave the entry unexpanded for the reference too, which makes the node at Q/allOf/1 (a Scalar over M) say something the reference's reading denies.

Out of scope

Related: #1996, #2009

Priority: P3, rule 3 (off the golden spec: it writes no $dynamicRef). jq '[.. | objects | select(has("$dynamicRef"))] | length' openapi.json → 0 (spec @8b1a4518, morphic @9562ea46d).

Activity

  1. added
    type:bug
    priority:P3
    on Nov 17, 2026
  2. OmarAlJarrah commented on Nov 19, 2026

    @OmarAlJarrah
    MemberAuthor

    A second site of this mechanism, on main at 512f3ee: failedEntry (compilers/openapi/internal/statement/table.go) reads "an allOf entry whose $ref names nothing" off the schema's pointer too, so a $ref naming such an entry reads it as the composition does, whole, and nothing keeps what it writes beside its reference:

    openapi: 3.1.0
    info: {title: t, version: "1"}
    paths: {}
    components:
      schemas:
        E:
          allOf:
            - $ref: '#/components/schemas/Missing'
              type: object
              properties:
                a: {type: string}
        Named:
          $ref: '#/components/schemas/Missing'
          type: object
          properties:
            a: {type: string}
        ViaEntry: {$ref: '#/components/schemas/E/allOf/0'}
        ViaNamed: {$ref: '#/components/schemas/Named'}
    $ morphic compile --diag-limit 0 spec.yaml >ir.json; echo "exit $?"
    error openapi/unresolved-ref spec.yaml#/components/schemas/E/allOf/0: unresolved $ref "#/components/schemas/Missing": it names nothing in the document it points into
    error openapi/unresolved-ref spec.yaml#/components/schemas/Named: unresolved $ref "#/components/schemas/Missing": it names nothing in the document it points into
    info openapi/degraded-construct spec.yaml#/components/schemas/E/allOf/0: this allOf lowered to a node of kind "model", which has no home for type, properties written beside this $ref, which names nothing; kept verbatim under Unmodeled
    exit 1
    $ jq -c '.types | to_entries[] | select(.key | test("schemas/(ViaEntry|Named|E/allOf/0)$")) | {id: (.key | split("/") | .[3:] | join("/")), kind: .value.kind, properties: [.value.properties[]?.name.source], kept: (.value.unmodeled // {} | keys)}' ir.json
    {"id":"schemas/E/allOf/0","kind":"scalar","properties":[],"kept":[]}
    {"id":"schemas/Named","kind":"model","properties":["a"],"kept":["openapi:$ref"]}
    {"id":"schemas/ViaEntry","kind":"scalar","properties":[],"kept":[]}

    ViaEntry and ViaNamed name the same schema object at two pointers. Named is a Model holding a and keeping its $ref; the node at E/allOf/0, which ViaEntry is a use of, is a Scalar over the top type holding neither type nor properties, with no report at it: the one report naming them speaks of E's model, which keeps them under its own keys. The document already fails on the unresolved $ref, so this is what the node says beside the error, not a silent pass.

    Two functions read "an allOf entry" off a pointer, failedEntry here and writtenAsEntry for the $dynamicRef above, and they parse it differently: the first takes the last token by strconv.Atoi, the second by strconv.ParseUint, so a member +3 of a mapping keyed allOf ($ref: '#/x-defs/allOf/+3') is an entry to the first alone, and a member 7 of one is an entry to both, though neither is an entry of any allOf. Folding the two onto either parse changes what such a document lowers to, so they are left as they are, each naming the other in a comment (#2083). Both go when the statement answers how a schema is reached, as the Direction above drafts: an entry is then a fact of the composition listing it, and no pointer is parsed.

  3. OmarAlJarrah commented on Nov 30, 2026

    @OmarAlJarrah
    MemberAuthor

    Fixed in #2354, which closes this on merge. A $ref naming an entry now reads that entry's schema alone, expanded: with the first spec here, ViaEntry draws the openapi/unsatisfiable-schema warning ViaM draws, and both are the closed bottom (acf4512). The comment's spec lowers as it asks from 7213104: E/allOf/0 is the Model holding a and keeping its $ref, as Named is, and ViaEntry composes it as ViaNamed composes Named. failedEntry and writtenAsEntry are gone, so no reader parses a pointer to find an entry.

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:P3type:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions