microsoft / microsoft/typespec

[Bug]: @discriminator and @encodedName interact badly

Open
#3,507 4 comments 0 reactions 0 assignees View on GitHub
bug compiler:core triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

### Describe the bug

In the OpenAPI emitter (and likely in other emitter), incoherence can occur when a discriminator property is also annotated with `@encodedName` differently in each variant model of a union.

Discriminator validation only checks that a TypeSpec property with the given name exists in variant models, but does not validate that those properties have the same encoded name in JSON. This leads to OpenAPI output that names a discriminator property that does not actually exist.

This has an obvious problem in OpenAPI, and other emitters are likely to get this wrong without some library support if the discriminator property is renamed in some variants but not others, or is renamed differently in each variant.

### Reproduction

https://typespec.io/playground?c=QGRpc2NyaW1pbmF0b3IoImtpbmQiKQp1bmlvbiBGb28gewogIGE6IEEsCiAgYjogQiwKfQoKbW9kZWwgQcUdQGVuY29kZWROYW1lKCJhcHBsaWNhdGlvbi9qc29uIiwgxVQyIikKICDECjogIkEiOwp9x0VC30XMRTHMRULFRQppbnRlcmZhY2UgRXhhbXBsZcVQb3AgZcYPKGluOuQAySnFBjsKfQ%3D%3D&e=%40typespec%2Fopenapi3&options=%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.