Skip to content

Conversation

@tellnes
Copy link
Contributor

@tellnes tellnes commented Nov 10, 2025

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.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 10, 2025

All changed packages have been documented.

  • @typespec/http
  • @typespec/openapi3
Show changes

@typespec/http - fix ✏️

Support nested unions in operation return types

@typespec/openapi3 - fix ✏️

Support nested unions in operation return types

@tellnes tellnes force-pushed the named-unions-response-type branch from 5206b58 to e3f3bda Compare November 10, 2025 15:39
@tellnes
Copy link
Contributor Author

tellnes commented Nov 10, 2025

@microsoft-github-policy-service agree

@tellnes tellnes force-pushed the named-unions-response-type branch 2 times, most recently from 2af7c26 to d1ff4cf Compare November 10, 2025 16:00
@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-type branch from d1ff4cf to 3cdcf40 Compare November 11, 2025 08:05
@tellnes tellnes changed the title fix(http,openapi3): support named unions in operation response types fix(http,openapi3): support nested unions in operation return types Nov 11, 2025
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.
@tellnes tellnes force-pushed the named-unions-response-type branch from 3cdcf40 to b89b5c7 Compare November 11, 2025 10:07
@timotheeguerin timotheeguerin added this pull request to the merge queue Nov 13, 2025
Merged via the queue into microsoft:main with commit 54bbb2e Nov 13, 2025
22 checks passed
github-merge-queue bot pushed a commit that referenced this pull request Nov 27, 2025
…riptions (#8962)

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:

```typespec
@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.
@tellnes tellnes deleted the named-unions-response-type branch November 28, 2025 12:42
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