-
Notifications
You must be signed in to change notification settings - Fork 205
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
Add support for converting OpenAPI3 specs to TypeSpec #3663
Conversation
packages/openapi3/src/cli/actions/compile/transforms/transform-operation-responses.ts
Fixed
Show fixed
Hide fixed
All changed packages have been documented.
Show changes
|
You can try these changes at https://cadlplayground.z22.web.core.windows.net/prs/3663/ Check the website changes at https://tspwebsitepr.z22.web.core.windows.net/prs/3663/ |
packages/openapi3/test/tsp-openapi3/specs/status-code-changes/service.yml
Show resolved
Hide resolved
packages/openapi3/test/tsp-openapi3/output/playground-http-service/main.tsp
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few more things,
fix #3038
This PR updates the
@typespec/openapi3
package to support converting OpenAPI3 specs to TypeSpec.Example usage:
npm install @typespec/openapi3
npx openapi3-to-tsp compile --output-dir ./tsp-output /path/to/openapi-yaml-or-json
What's supported
@info
decorator with OpenAPI3 service info#/components/schemas
into TypeSpec models/scalars.#/components/parameters
into TypeSpec models/model properties as appropriate.allOf
What's not supported (yet)
#/components/requestBodies
and#/components/responses
into models - TypeSpec doesn't seem to generate these and I didn't find examples in the wild where they were defined and actually used so deprioritized.Notes
When going through the TypeSpec -> OpenAPI3 -> TypeSpec loop, the generated TypeSpec is going to be larger than the original. The biggest contribution towards this is because I'm currently generating a model for every possible response on every operation.
I can definitely pare this down with some simple heuristics that take into account what default statusCode/contentTypes are, and extract the referenced body type directly in the operation's return signature. I can also eliminate the
@get
decorators,@route("/")
routes, and likely use some of the response models provided by TypeSpec.Http.However - if I'm using this tool to convert from OpenAPI3 to TypeSpec - I thought it might be preferable to be more explicit in the generated output so there's no mystery on how things actually get defined. Will be interested in feedback on this.
Testing
For tests, I generate TypeSpec files for a number of OpenAPI3 specs. Most of the OpenAPI3 specs I generated from our TypeSpec samples packages. Then I'm able to compare the generated TypeSpec to the corresponding original TypeSpec file. I've also been diffing the OpenAPI3 specs generated from the original and generated TypeSpec files <- these are what typically show no changes outside of known unsupported conversions (e.g. auth).