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

Add support for converting OpenAPI3 specs to TypeSpec #3663

Merged
merged 45 commits into from
Jul 2, 2024

Conversation

chrisradek
Copy link
Member

@chrisradek chrisradek commented Jun 26, 2024

fix #3038
This PR updates the @typespec/openapi3 package to support converting OpenAPI3 specs to TypeSpec.

Example usage:

  1. npm install @typespec/openapi3
  2. npx openapi3-to-tsp compile --output-dir ./tsp-output /path/to/openapi-yaml-or-json

What's supported

  • Parse OpenAPI3 specs in yml/json formats (via 3rd party package)
  • Generates file namespace based on OpenAPI3 service name
  • Populates @info decorator with OpenAPI3 service info
  • Converts #/components/schemas into TypeSpec models/scalars.
  • Converts #/components/parameters into TypeSpec models/model properties as appropriate.
  • Generates a response model for every operation/statusCode/contentType combination.
  • Operation tags
  • Generates TypeSpec operations with routes/Http Method decorators
  • Generates docs/extension decorators
  • Most schema decorators
  • Model inheritance via allOf
  • Discriminators

What's not supported (yet)

  • auth
  • deprecated directive
  • combining multiple versions of an OpenAPI3-defined service into a single TypeSpec project
  • converting #/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.
  • emitting warnings/FIXMEs for unexpected/unsupported scenarios
  • Probably a lot more that I'm still discovering

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).

@azure-sdk
Copy link
Collaborator

azure-sdk commented Jun 26, 2024

All changed packages have been documented.

  • @typespec/openapi3
Show changes

@typespec/openapi3 - feature ✏️

Adds support for converting OpenAPI3 specs to TypeSpec via the new tsp-openapi3 CLI included in the @typespec/openapi3 package.

@azure-sdk
Copy link
Collaborator

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/

@chrisradek chrisradek added the emitter:openapi3 Issues for @typespec/openapi3 emitter label Jun 26, 2024
@chrisradek chrisradek marked this pull request as ready for review June 27, 2024 02:32
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.

Just a few more things,

.chronus/changes/oa3-to-ts-2024-5-26-13-27-36.md Outdated Show resolved Hide resolved
packages/openapi3/tsconfig.json Outdated Show resolved Hide resolved
packages/openapi3/package.json Outdated Show resolved Hide resolved
packages/openapi3/package.json Outdated Show resolved Hide resolved
packages/openapi3/package.json Outdated Show resolved Hide resolved
@chrisradek chrisradek enabled auto-merge July 2, 2024 22:31
@chrisradek chrisradek added this pull request to the merge queue Jul 2, 2024
Merged via the queue into microsoft:main with commit 8354a75 Jul 2, 2024
21 checks passed
@chrisradek chrisradek deleted the oa3-to-ts branch July 2, 2024 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emitter:openapi3 Issues for @typespec/openapi3 emitter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature request] OpenAPI 3.0 to TypeSpec converter
7 participants