Skip to content

feat(openapi3): add opt-in enum-style option for annotated enums - #10892

Merged
timotheeguerin merged 9 commits into
microsoft:mainfrom
timotheeguerin:feat/openapi3-enum-style-annotated
Jun 23, 2026
Merged

feat(openapi3): add opt-in enum-style option for annotated enums#10892
timotheeguerin merged 9 commits into
microsoft:mainfrom
timotheeguerin:feat/openapi3-enum-style-annotated

Conversation

@timotheeguerin

Copy link
Copy Markdown
Member

Fixes #5721

Adds a new opt-in enum-style emitter option to @typespec/openapi3. When set to annotated, TypeSpec enums are emitted as OpenAPI 3.1.1 annotated enumerations — a oneOf of const subschemas with per-member title and description taken from @summary and @doc on each enum member. This preserves member-level documentation that otherwise gets dropped by the default enum keyword form.

Behavior

  • Default value is default; existing emitter output is unchanged.
  • annotated is supported on OpenAPI 3.1.0 and 3.2.0 (which support const).
  • For OpenAPI 3.0.0, annotated falls back to the default form and a new enum-style-not-supported warning is reported.
  • Applies only to TypeSpec enum declarations; unions of literals are unchanged.
  • Outer enum-level constraints (@doc, @summary, etc.) are still applied to the wrapping schema via applyConstraints.

Example

/** Type of pet. */
enum PetType {
  /** A loyal canine companion. */
  @summary("Dog")
  Dog: "dog",

  /** A self-sufficient feline. */
  @summary("Cat")
  Cat: "cat",
}

With enum-style: annotated and OpenAPI 3.1.0/3.2.0:

PetType:
  description: Type of pet.
  oneOf:
    - const: dog
      title: Dog
      description: A loyal canine companion.
    - const: cat
      title: Cat
      description: A self-sufficient feline.

Files changed

  • packages/openapi3/src/lib.tsEnumStyle type, enum-style option (+ JSON schema), and enum-style-not-supported warning diagnostic.
  • packages/openapi3/src/openapi.tsenumStyle propagated into ResolvedOpenAPI3EmitterOptions; warning emitted in resolveOptions when paired with 3.0.0.
  • packages/openapi3/src/schema-emitter-3-1.ts — annotated branch in enumSchema (3.2 inherits).
  • packages/openapi3/test/enums.test.ts — 13 new tests covering 3.1/3.2 annotated form (string, number, mixed, with/without docs, default unchanged), 3.0.0 fallback, and warning emission.
  • .chronus/changes/openapi3-enum-style-annotated-*.mdfeature changelog entry.

Add a new `enum-style` emitter option to @typespec/openapi3 with values
`default` (current behavior) and `annotated`. When set to `annotated`,
TypeSpec enums are emitted as OpenAPI 3.1.1 annotated enumerations
(a oneOf of const subschemas with per-member title/description sourced
from @summary/@doc).

Supported on OpenAPI 3.1.0 and 3.2.0; for 3.0.0 the option falls back to
the default form and an `enum-style-not-supported` warning is reported.

Closes microsoft#5721
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:openapi3 Issues for @typespec/openapi3 emitter label Jun 4, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jun 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/openapi3@10892

commit: 453f7a9

@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • @typespec/openapi3
Show changes

@typespec/openapi3 - feature ✏️

Add opt-in enum-strategy emitter option to emit TypeSpec enums as annotated enumerations (a oneOf of const subschemas with per-member title/description). Supported for OpenAPI 3.1.0 and above; emitting with OpenAPI 3.0.0 falls back to the default form and reports a warning.,> ,> yaml,> options:,> "@typespec/openapi3":,> enum-strategy: annotated,> ,> ,> For example, the following TypeSpec:,> ,> typespec,> /** Type of pet. */,> enum PetType {,> /** A loyal canine companion. */,> @summary("Dog"),> Dog: "dog",,> ,> /** A self-sufficient feline. */,> @summary("Cat"),> Cat: "cat",,> },> ,> ,> emits:,> ,> yaml,> PetType:,> description: Type of pet.,> oneOf:,> - const: dog,> title: Dog,> description: A loyal canine companion.,> - const: cat,> title: Cat,> description: A self-sufficient feline.,>

@azure-sdk-automation

azure-sdk-automation Bot commented Jun 4, 2026

Copy link
Copy Markdown

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@timotheeguerin
timotheeguerin marked this pull request as ready for review June 5, 2026 15:55
@microsoft-github-policy-service microsoft-github-policy-service Bot added the meta:website TypeSpec.io updates label Jun 5, 2026

@markcowl markcowl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if @summary exactly corresponds with title, but otherwise, this looks great

@timotheeguerin

Copy link
Copy Markdown
Member Author

Yeah summary is title in openapi

@timotheeguerin
timotheeguerin enabled auto-merge June 5, 2026 21:28
…yle-annotated

# Conflicts:
#	.github/aw/actions-lock.json
#	.github/workflows/issue-triage.lock.yml
@microsoft-github-policy-service microsoft-github-policy-service Bot added the stale Mark a PR that hasn't been recently updated and will be closed. label Jun 22, 2026
…style-annotated

# Conflicts:
#	.github/aw/actions-lock.json
#	.github/workflows/issue-triage.lock.yml
#	website/src/content/docs/docs/emitters/openapi3/reference/emitter.md
@timotheeguerin
timotheeguerin added this pull request to the merge queue Jun 23, 2026
Merged via the queue into microsoft:main with commit 9728385 Jun 23, 2026
33 checks passed
@timotheeguerin
timotheeguerin deleted the feat/openapi3-enum-style-annotated branch June 23, 2026 21:12
mzhongl524 pushed a commit to mzhongl524/typespec that referenced this pull request Jul 17, 2026
…icrosoft#11154)

Extends `enum-strategy: annotated` (microsoft#10892, which fixed microsoft#5721) to unions
of literals. That option previously stopped at `enum` declarations, so
unions of literals still collapsed to a flat `enum`, dropping variant
names, `@summary`, and docs.

```typespec
/** Set of known error types. */
union ErrorType {
  /** Common error for a bad request. */
  @summary("CommonBadRequest")
  commonBadRequest: "https://example.com/errors/bad-request",
}
```

Before: `{ type: string, enum:
["https://example.com/errors/bad-request"] }`

After (with `enum-strategy: annotated`):

```yaml
ErrorType:
  description: Set of known error types.
  anyOf:
    - const: https://example.com/errors/bad-request
      title: CommonBadRequest
      description: Common error for a bad request.
```

## Behavior

- Opt-in via the existing option; `default` output unchanged.
- 3.1.0/3.2.0 only; 3.0.0 falls back and reports the existing
`enum-strategy-not-supported` warning.
- `oneOf` when the union has `@oneOf`, else `anyOf`.
- `title` from `@summary`, `description` from `@doc`, both omitted when
absent, matching the enum handling.
- Non-literal variants (models/scalars) stay as their own members;
discriminated unions untouched.

## Open question

The option is named `enum-strategy` but now also covers unions of
literals (both are "enumerated types" in OpenAPI terms). I broadened its
description rather than renaming it. Happy to add an alias if preferred.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:openapi3 Issues for @typespec/openapi3 emitter eng meta:website TypeSpec.io updates stale Mark a PR that hasn't been recently updated and will be closed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAPI 3.1 - emit annotated enums

2 participants