Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tsp-openapi3 - add heuristics for operation response generation #5831

Merged
merged 10 commits into from
Feb 6, 2025

Conversation

chrisradek
Copy link
Member

@chrisradek chrisradek commented Feb 1, 2025

Fixes #3765

This PR primarily changes the way operation return types are handled when converting an Open API 3 doc to TypeSpec.

Previously, a separate model was generated for every possible response per operation. This quickly exploded the number of models generated if multiple status codes or content types were defined for an operation.

Now, those models are in-lined per operation rather than declared. Additionally, some heuristics are employed to trim down each model expression to remove fields that have TypeSpec-defined default values, and whose status code matches a model from the @typespec/http library.

To support @defaultResponse, a GeneratedHelpers.DefaultResponse template is created - only when needed - and is referenced by the generated operations.

I also made 3 changes related to response generation as part of this PR:

  1. Response headers that use $ref are now supported
  2. Responses that use $ref are now supported
  3. Switched to using the OpenAPI parser's $ref resolution to resolve refs instead of the more fragile $ref string parsing that was being used before. Combined with using the parser's bundle feature (which was already used) - this should resolve any edge cases around following $refs that point to different files.

TODO: double-check that the parser-related changes doesn't break the playground importer

@azure-sdk
Copy link
Collaborator

azure-sdk commented Feb 1, 2025

All changed packages have been documented.

  • @typespec/bundler
  • @typespec/openapi3
Show changes

@typespec/openapi3 - feature ✏️

Updates tsp-openapi3 operation response generation to inline expressions and pare down fields with default values. Also adds support for Open API headers and responses $refs.

@typespec/bundler - feature ✏️

Bundler now includes some node polyfills in library rollup builds

@azure-sdk
Copy link
Collaborator

azure-sdk commented Feb 1, 2025

You can try these changes here

🛝 Playground 🌐 Website 📚 Next docs 🛝 VSCode Extension

Copy link
Member

@timotheeguerin timotheeguerin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks so much better, one thing I wish is just if we could maybe not use @defaultResponse

@chrisradek chrisradek enabled auto-merge February 6, 2025 17:44
@chrisradek chrisradek added this pull request to the merge queue Feb 6, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 6, 2025
@chrisradek chrisradek enabled auto-merge February 6, 2025 19:15
@chrisradek chrisradek added this pull request to the merge queue Feb 6, 2025
Merged via the queue into microsoft:main with commit 4d00495 Feb 6, 2025
24 checks passed
@chrisradek chrisradek deleted the tsp-openapi3-simpler-responses branch February 6, 2025 19:47
dmnorc pushed a commit to dmnorc/typespec that referenced this pull request Feb 18, 2025
…osoft#5831)

Fixes microsoft#3765 

This PR primarily changes the way operation return types are handled
when converting an Open API 3 doc to TypeSpec.

Previously, a separate model was generated for every possible response
per operation. This quickly exploded the number of models generated if
multiple status codes or content types were defined for an operation.

Now, those models are in-lined per operation rather than declared.
Additionally, some heuristics are employed to trim down each model
expression to remove fields that have TypeSpec-defined default values,
and whose status code matches a model from the `@typespec/http` library.

To support `@defaultResponse`, a `GeneratedHelpers.DefaultResponse`
template is created - only when needed - and is referenced by the
generated operations.

I also made 3 changes related to response generation as part of this PR:
1. Response headers that use `$ref` are now supported
2. Responses that use `$ref` are now supported
3. Switched to using the OpenAPI parser's `$ref` resolution to resolve
refs instead of the more fragile `$ref` string parsing that was being
used before. Combined with using the parser's `bundle` feature (which
was already used) - this should resolve any edge cases around following
`$refs` that point to different files.

~TODO: double-check that the parser-related changes doesn't break the
playground importer~

---------

Co-authored-by: Christopher Radek <[email protected]>
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.

Openapi3-to-tsp should try to keep status code/responses envelope inline
3 participants