Skip to content

Instantly share code, notes, and snippets.

@mikekistler
Created October 31, 2021 23:50
Show Gist options
  • Save mikekistler/0b735aab72b42bafaf60ecfc4fb8be3f to your computer and use it in GitHub Desktop.
Save mikekistler/0b735aab72b42bafaf60ecfc4fb8be3f to your computer and use it in GitHub Desktop.
OpenAPI 3 rendering of Cadl named unions

The Cadl named union:

model Cat {
  meow: int32;
};
model Dog {
  bark: string;
};
union Pet { cat: Cat, dot: Dog }

will be rendered in by the openapi3 emitter as a "oneOf" schema with discriminator"

  "components": {
    "schemas": {
      "Pet": {
        "type": "object",
        "properties": {
          "kind": {
            "type": "string"
          }
        },
        "required": [
          "kind"
        ],
        "discriminator": {
          "propertyName": "kind",
          "mapping": {
            "cat": "#/components/schemas/Cat",
            "dog": "#/components/schemas/Dog"
          }
        },
        "oneOf": [
          {
            "$ref": "#/components/schemas/Cat"
          },
          {
            "$ref": "#/components/schemas/Dog"
          }
        ]
      },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment