Skip to content

openapi: a discriminator on an inline allOf entry is kept verbatim where beside the allOf it is typed #2052

Open

Description

@OmarAlJarrah

Problem

A discriminator written beside an allOf lowers to the model's Discriminator, and a subtype composing the model takes its DiscriminatorValue from the mapping (docs/compilers/openapi.md#discriminator). Written on an inline entry of the same allOf, next to the properties that declare the tag, the same keyword is kept verbatim under Unmodeled: the model holds no Discriminator and no subtype is tagged.

openapi: 3.1.0
info: {title: t, version: "1"}
paths: {}
components:
  schemas:
    Base: {type: object, properties: {id: {type: string}}}
    PetBeside:
      allOf: [{$ref: '#/components/schemas/Base'}]
      type: object
      properties: {kind: {type: string}}
      discriminator: {propertyName: kind, mapping: {cat: '#/components/schemas/CatBeside'}}
    CatBeside:
      allOf: [{$ref: '#/components/schemas/PetBeside'}, {type: object, properties: {lives: {type: integer}}}]
    PetEntry:
      allOf:
        - $ref: '#/components/schemas/Base'
        - type: object
          properties: {kind: {type: string}}
          discriminator: {propertyName: kind, mapping: {cat: '#/components/schemas/CatEntry'}}
    CatEntry:
      allOf: [{$ref: '#/components/schemas/PetEntry'}, {type: object, properties: {lives: {type: integer}}}]
$ morphic compile --verify --diag-limit 0 spec.yaml >ir.json; echo "exit $?"
info openapi/degraded-construct spec.yaml#/components/schemas/PetEntry/allOf/1: inline allOf branch is merged in place, and nothing reads the rest of it; the branch (discriminator) is kept verbatim under Unmodeled
exit 0
$ jq -c '.types | to_entries[] | select(.key | test("schemas/(Pet|Cat)(Beside|Entry)$")) | {id: (.key | split("/") | last), base: (.value.base.target | split("/") | last), discriminator: .value.discriminator, discriminatorValue: .value.discriminatorValue, unmodeled: (.value.unmodeled // {} | map_values(.reason))}' ir.json
{"id":"CatBeside","base":"PetBeside","discriminator":null,"discriminatorValue":"cat","unmodeled":{}}
{"id":"CatEntry","base":"PetEntry","discriminator":null,"discriminatorValue":null,"unmodeled":{}}
{"id":"PetBeside","base":"Base","discriminator":{"property":"p/openapi/components/schemas/PetBeside/properties/kind","mapping":{"cat":"t/openapi/components/schemas/CatBeside"},"inferred":false},"discriminatorValue":null,"unmodeled":{}}
{"id":"PetEntry","base":"Base","discriminator":null,"discriminatorValue":null,"unmodeled":{"openapi:allOf/1":"degraded_lowering"}}

The two spellings state one schema: kind is a member of the model, the discriminator names it, and cat routes to the subtype. PetBeside is the discriminated base a consumer decodes by kind, with CatBeside tagged cat. PetEntry is a Model over Base with the member kind and no Discriminator, and CatEntry, which composes it, carries no DiscriminatorValue, so nothing says a payload with kind: cat is a CatEntry. The branch survives whole under openapi:allOf/1, so the information is kept, but in a spelling no consumer reads as a discriminator, and the report calls it merged in place with "nothing reads the rest of it".

The rule does not say an entry's discriminator is left out. docs/compilers/openapi.md#allof-inline-residue lists what an entry's merge consumes: its properties, its required list, its type, and "the discriminator the branch writes beside" a oneOf or anyOf, which is a union's. The rule for the keyword on a Model (#discriminator) says nothing of an entry's. So the outcome is neither specified nor reported as a decision.

Cause

lowerAllOf (compilers/openapi/internal/schema/allof.go) lowers the discriminator of the schema it is handed, s.GetDiscriminator(), once the model has its properties. The inline-entry merge reads no discriminator, and the subtype's tag is read from the discriminator of the composing schemas it reaches (subtypeDiscriminatorValue, discriminator.go), never from an entry's. An entry's keyword therefore reaches the generic "kept whole" path (morphic @9562ea46d).

Direction

A draft: read a discriminator an inline entry writes as one written beside the allOf, when its propertyName names a member the composed model holds, so PetEntry lowers as PetBeside does and the subtypes it routes are tagged. Where the entry's tag names a member the model does not hold, keep it as the keyword is kept beside the allOf there. The alternative is to keep the verbatim record and say in #discriminator that an entry's discriminator is residue, with its own info naming the loss of the route; that leaves a consumer decoding by kind to read Unmodeled.

Out of scope

Related: #1698, #1989, #2006, #1643, #1996

On #1996's branch (6daf12a) PetEntry still holds no Discriminator and CatEntry no DiscriminatorValue; the entry's keyword is kept under openapi:discriminator (degraded_lowering) with an info that the model has no home for it, instead of the whole entry under openapi:allOf/1.

Priority: P3, rule 3 (off the golden spec; valid-spec functionality a later emitter needs to route a discriminated hierarchy). jq '[.. | objects | select(has("allOf")) | .allOf[]? | objects | select(has("discriminator"))] | length' openapi.json → 0, and the same over select(has("allOf") and has("discriminator")) → 0, of 58 schemas with a discriminator (spec @8b1a4518, morphic @9562ea46d).

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