microsoft / microsoft/typespec

[Bug]: OpenAPI3 merges discriminated union envelopes when variants share a payload type

Open
#11,979 2 comments 1 reaction 1 assignee Claimed by @timotheeguerin View on GitHub
bug emitter:openapi3 triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

### Describe the bug

With `@typespec/openapi3` 1.16.0, distinct variants of a discriminated union reuse the first envelope schema when their payload types match. The emitter repeats that schema in `oneOf` and points every discriminator mapping to it. The other envelope schemas are missing from the output.

For example:

```tsp
@discriminated(#{
discriminatorPropertyName: "tag",
envelopePropertyName: "contents"
})
union Limit {
daily: string,
weekly: string,
monthly: string,
}
```

This produces a `oneOf` schema that references `LimitDaily` three times, and rejects any payload with `"tag":"weekly"` or `"tag":"monthly"`.

### Reproduction

[Playground](https://typespec.io/playground/?c=aW1wb3J0ICJAdHlwZXNwZWMvaHR0cCI7CtIZb3BlbmFwaTMiOwoKdXNpbmcgVHlwZVNwZWMuSHR0cDsKCkBzZXJ2aWNlCm5hbWVzcGFjZSBFeGFtcGxlxB1kaXNjcmltaW5hdGVkKCN7CiAgyxNvclByb3BlcnR5TmFtZTogInRhZyIsCiAgZW52ZWxvcGXPH2NvbnRlbnRzIgp9KQp1bmlvbiBMaW1pdCDFWGFpbHk6IHN0cmluZ8RFd2Vla84SbW9udGjME30KCkByb3V0ZSgiL2zESiIpCkBnZXQKb3AgcmVhZCgpOsZi5ADM0Slwb3PFKndyaXRlKEBib2R5IMUdxzXKPQ%3D%3D&tspconfig=ZW1pdDoKICAtICJAdHlwZXNwZWMvb3BlbmFwaTMiCg%3D%3D&vs=%7B%7D)

### Checklist

- [x] Follow our [Code of Conduct](https://github.com/microsoft/typespec/blob/main/CODE_OF_CONDUCT.md)
- [x] Check that there isn't already an issue that request the same bug to avoid creating a duplicate.
- [x] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/Microsoft/typespec/discussions).
- [x] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.