fix(openapi3): use the first enum value as the server variable default by Thanatos9404 · Pull Request #12302 · microsoft/typespec · GitHub
Skip to content

fix(openapi3): use the first enum value as the server variable default - #12302

Open
Yashvardhan Thanvi (Thanatos9404) wants to merge 2 commits into
microsoft:mainfrom
Thanatos9404:fix/openapi3-server-variable-enum-default
Open

Yashvardhan Thanvi (Thanatos9404) wants to merge 2 commits into
microsoft:mainfrom
Thanatos9404:fix/openapi3-server-variable-enum-default

Conversation

@Thanatos9404

@Thanatos9404 Yashvardhan Thanvi (Thanatos9404) commented Oct 19, 2026 •

Copy link
Copy Markdown

Fixes #6024

A server variable typed as an enum, a union of string literals or a single string literal gets an enum in the emitted document, but without a TypeSpec default its default was always "". OpenAPI 3.1 and 3.2 say default MUST be one of the enum values (3.0 says SHOULD), so those documents were invalid.

In resolveServers, when there is no default and an enum was computed, the first enum value is now used, for all OpenAPI versions. An explicit default is kept, and a plain string variable still gets "". When the first value is used for a variable with more than one possible value, a new missing-server-variable-default warning tells the author to set a default.

One exception: a variable typed as the service's @versioned enum (like apiVersion: Versions) defaults to the version of the document being emitted, so versioned documents don't all default to the oldest version. For this, initializeEmitter passes the version to resolveServers, using the versioning module the emitter already loads.

Testing: three existing tests in servers.test.ts that pinned default: "" now expect the first enum value, and new tests cover explicit defaults and the versioned case. On main 15 of the 57 tests in that file fail; with this change all pass.

A server variable typed as an enum, a union of string literals or a
string literal was emitted with `default: ""` next to its `enum` when
the TypeSpec property had no default. OpenAPI 3.1 and later require the
default to be one of the enum values (3.0 recommends it), so the
emitted document was invalid.

When there is no explicit default and an enum was computed, use the
first enum value. A variable typed as the service's versions enum uses
the version of the document being emitted instead, so each versioned
document defaults to its own version. Explicit defaults and plain
string variables are unchanged. The emitter shares this code for every
OpenAPI version, so 3.0, 3.1 and 3.2 all get the same output.

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:openapi3 Issues for @typespec/openapi3 emitter label Oct 19, 2026
@azure-pipelines

Copy link
Copy Markdown

@pkg-pr-new

pkg-pr-new Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 207198d

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • ✅ @typespec/openapi3
Show changes

@typespec/openapi3 - fix ✏️

Server variables that have an enum but no default now use the first enum value as their default instead of an empty string, since OpenAPI 3.1 and later require the default to be one of the enum values. A server variable typed as the service's versions enum uses the version of each emitted document instead. A new missing-server-variable-default warning is reported when that first value is used for a variable with more than one possible value.

@timotheeguerin
Timothee Guerin (timotheeguerin) removed this pull request from the merge queue due to a manual request Oct 19, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

actually I think this is doing an unexpected behavior here, we should emit a warning to let the author know we set something that is probably not right

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good call. It now reports a missing-server-variable-default warning when it falls back to the first value, so the author knows to set a default (207198d). It doesn't warn when the variable is the service's versions enum, since each document then gets its own version rather than a guess, or when there is only one possible value, and in a versioned service the warning shows once rather than once per version. Happy to warn in those cases too if you'd prefer.

…first enum value

When a server variable can take more than one value and has no default,
the emitter still uses the first value so the document stays valid, but
now reports a missing-server-variable-default warning so the author can
set the default they want. The versions enum case is not a guess and does
not warn, and a variable with a single possible value does not warn
either.

The warning is reported once per parameter, not once per emitted version.
Copilot AI balanced review requested due to automatic review settings October 19, 2026 18:31

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

This branch has not been deployed

No deployments
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: OpenAPI 3.1: Server Variable's default now MUST exist in the enum values, if such values are defined (changed from SHOULD).

3 participants