Skip to content

Helper to construct TypeSpec unions out of existing models #1811

Closed
@tjprescott

Description

@tjprescott

Background
PR #1770 required logic that would essentially take the response types of multiple operations with shared routes and emit them as "oneOf" properties in OpenAPI3. That should have been an easy task, especially since the OpenAPI3 emitter already had logic to get the appropriate OpenAPI3 schema for existing TypeSpec types, including Union.

However, this proved infuriatingly difficult because there was no way to simply take two models and say "Please give me a TypeSpec Union of these" that I could pass to that existing logic. If you can't work with the TypeSpec types as output by the compiler, you are screwed and have to invent a bunch of intermediate models to facilitate this. It ended up being way more work than it seems like it should have.

Request
It would be nice to have some kind of helper method that takes an arbitrary number of TypeSpec types and returns a TypeSpec.Union of them, but without all the type graph links that make trying to construct one of these programmatically pretty much impossible (nodes, parents, etc.).

Perhaps there could be a way to generalize this, but essentially constructing TypeSpec types seems to only be possible via the compiler, and it would be useful to be able to create them programmatically as well.

Metadata

Metadata

Assignees

Labels

design:neededA design request has been raised that needs a proposal

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions