Skip to content

Conversation

@tellnes
Copy link
Contributor

@tellnes tellnes commented Nov 10, 2025

feat(http): support documentation on union variants for response descriptions

Allows using @doc on unions to provide a unified description for all
responses in that union, regardless of individual variant descriptions.
This gives users explicit control over response descriptions when
multiple response types share the same status code.

Example:

@doc("The resource conflicts with an existing resource")
union Conflict {
  DuplicateName: DuplicateNameError;
  DuplicateId: DuplicateIdError;
}
op createResource(): Resource | Conflict;

The union's @doc takes precedence over individual variant descriptions.
For nested unions, the innermost union's @doc takes precedence, allowing
fine-grained control over different groups of responses.

This provides explicit control for cases where the default behavior
(using the first variant's description) is not sufficient. Future
enhancements could include intelligent description combining when
variants have different descriptions but no union @doc is specified.


This MR depends on #8961
The commit from that MR is included here since Github does not support
stacked MRs from forks.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 10, 2025

All changed packages have been documented.

  • @typespec/http
Show changes

@typespec/http - feature ✏️

support documentation on union variants for response descriptions

@timotheeguerin
Copy link
Member

/azp run typespec - pr tools

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@azure-sdk
Copy link
Collaborator

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@tellnes tellnes force-pushed the named-unions-response-doc branch 2 times, most recently from fb1c184 to d0d3f3f Compare November 11, 2025 10:04
@tellnes tellnes changed the title feat(http): support @doc on unions for response descriptions feat(http): support documentation on union variants for response descriptions Nov 11, 2025
@tellnes tellnes force-pushed the named-unions-response-doc branch from d0d3f3f to 7378cf2 Compare November 11, 2025 10:07
github-merge-queue bot pushed a commit that referenced this pull request Nov 13, 2025
…8961)

Nested unions (e.g., `op read(): A | (B | C)` or using a named union
like `union Conflict { ... }`) were not being expanded recursively when
used as operation return types, causing responses to be missing from
the generated OpenAPI output.

This fix:
- Adds recursive union expansion in processResponseType() to handle
  nested unions at any level
- Updates getResponses() to group responses by status code before
  processing to properly handle cases where multiple union variants
  map to the same status code
- Ensures response descriptions are taken from the first variant when
  multiple variants share a status code

The most common case is named unions, but this also fixes any scenario
where unions are nested within other unions in the return type.

----

The reasons for why I want this fixed is because I want to be able to
control the description on responses when there is multiple responses on
the same status code. See
#8962 for details.
…riptions

Allows using @doc on unions to provide a unified description for all
responses in that union, regardless of individual variant descriptions.
This gives users explicit control over response descriptions when
multiple response types share the same status code.

Example:
  @doc("The resource conflicts with an existing resource")
  union Conflict {
    DuplicateName: DuplicateNameError;
    DuplicateId: DuplicateIdError;
  }
  op createResource(): Resource | Conflict;

The union's @doc takes precedence over individual variant descriptions.
For nested unions, the innermost union's @doc takes precedence, allowing
fine-grained control over different groups of responses.

This provides explicit control for cases where the default behavior
(using the first variant's description) is not sufficient. Future
enhancements could include intelligent description combining when
variants have different descriptions but no union @doc is specified.
@tellnes tellnes force-pushed the named-unions-response-doc branch from 7378cf2 to c6fc35e Compare November 27, 2025 12:53
@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 27, 2025

Open in StackBlitz

npm i https://pkg.pr.new/microsoft/typespec/@typespec/http@8962
npm i https://pkg.pr.new/microsoft/typespec/@typespec/openapi@8962
npm i https://pkg.pr.new/microsoft/typespec/@typespec/openapi3@8962

commit: c6fc35e

@timotheeguerin timotheeguerin added this pull request to the merge queue Nov 27, 2025
Merged via the queue into microsoft:main with commit 5fadc90 Nov 27, 2025
23 checks passed
@tellnes tellnes deleted the named-unions-response-doc branch November 28, 2025 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants