microsoft / microsoft/typespec

Invalid OA3 for unions

Open
#826 19 comments 3 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

We are not emitting proper OA3 for unions. In [this example](https://cadlplayground.z22.web.core.windows.net/?c=aW1wb3J0ICJAY2FkbC1sYW5nL3Jlc3QiOw0KdXNpbmcgQ2FkbC5SZXN0Ow0KDQpAZGlzY3JpbWluYXRvcigia2luZCIpDQptb2RlbCBCYXNlIHsNCn3EK8YTQSBleHRlbmRzyR0gIMQ1OiAiQcRlyytC2itCyStvcCBmb28oKTrFJTs%3D), I believe we need to emit `oneOf` somewhere. Right now we depend on references to the base type implicitly being a reference to some kind of union of anything that allOf's that type, but these semantics are not supported by the specification. @mikekistler agrees with this analysis.

oneOf two things need to happen for out output to be correct:

1. Any references to `Base` are replaced by an inline `oneOf` union referencing all of the derived types.
2. `Base` becomes an allOf of the derived types and we generate a new type with a name like `BaseCommon` that holds the cadl `Base` type's properties.

The latter is what @mikekistler prefers, and hinges on the fact that by putting discriminator on the base model you're essentially instructing cadl that "every time I reference this type, I'm referring to a union of its subtypes", but it does have the downside that we are generating a name in the openapi output (BaseCommon), and also munging a name users might expect to be generated as-is in client code (e.g. they might expect to see `class A extends Base { }` in their TS/C# code somewhere.

We have a third option here which is to delete @discriminator on models and reserve it only for unions ala #335 where the Cadl author gives a good name to both the base type and the union separately.

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.