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

VSCode extension: add openapi3 preview #6015

Open
wants to merge 61 commits into
base: main
Choose a base branch
from

Conversation

archerzz
Copy link
Member

@archerzz archerzz commented Feb 14, 2025

  • add command to preview the main.tsp of the selected tsp file
    • each main tsp can have its own preview panel
    • preview panel will auto-refresh when a tsp file is changed
  • if multiple swagger definition is generated, command will prompt users to select one
    • the selection will be kept until the preview panel is closed
  • swagger definition is generated to a temp folder
    • temp folders will be cleared on deactivation
  • a minimal version of tsp 0.65.0 is required to simplify implementation
  • due to vsce package not supporting pnpm, I have to copy swagger-ui-dist lib files into our source folder. So, add those files into ignore list of lint and format.

resolve #4858

@archerzz
Copy link
Member Author

I have to copy swagger-ui-dist libs instead of importing them as dependencies, because pnpm is actually not supported in vsce package.

@azure-sdk
Copy link
Collaborator

azure-sdk commented Feb 14, 2025

All changed packages have been documented.

  • typespec-vscode
Show changes

typespec-vscode - feature ✏️

add openapi3 preview

@azure-sdk
Copy link
Collaborator

azure-sdk commented Feb 14, 2025

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@RodgeFu RodgeFu added the ide Issues for VS, VSCode, Monaco, etc. label Feb 14, 2025
@archerzz
Copy link
Member Author

https://github.com/arjun-g/vs-swagger-viewer is not suitable as a dependent extension for previewing swagger, because it only exposes a load method, which doesn't fulfill our requirement:

  • We cannot get the panel instance, which means:
    • we don't know when the panel is closed
    • we cannot manually switch panel from background to front ground
  • It maintains an HTTP server internally, which means more runtime footprint

@archerzz
Copy link
Member Author

List of changes since last review:

  • update ordering of commands and menu items according to comments
  • refactor parseOpenApi3File to make it generic
    • add a new parseOpenApi3File in the openapi3-preview.ts which handles UI specific logic
  • add selected file in the error logs if it's available

@archerzz
Copy link
Member Author

List of changes since last review:

  • move swagger-ui-dist from dependencies to devDependencies
  • use rollup to copy files of swagger-ui-dist
  • do not show pop-up message when user cancels file selection
  • re-use TraverseMainTspFileInWorkspace() in typespec-utils.ts

@timotheeguerin timotheeguerin changed the title feat(vscode): add openapi3 preview VSCode extension: add openapi3 preview Feb 27, 2025
@@ -27,6 +28,24 @@ export async function isDirectory(path: string) {
}
}

export async function createTempDir(): Promise<string | undefined> {
Copy link
Member

Choose a reason for hiding this comment

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

node has mkdtemp which does that too

return;
}

const mainTspFile = selectedFile.endsWith("main.tsp") ? selectedFile : await getMainTspFile();
Copy link
Member

Choose a reason for hiding this comment

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

this is the same issue as #6062, if the user asked for a preview of a file it should respect that and not try to fetch a main.tsp

Copy link
Member Author

Choose a reason for hiding this comment

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

when I discussed this feature with Rodge, we thought we should show the full open api 3 spec v.s. just the selected spec. Sometimes, showing the selected spec may be meaningless. For example, client.tsp is for client codes, so it should not have impact on the Open API 3 output which is for service API.

@RodgeFu Your thought?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah but it's not up to you to decide that for the user. Typespec compile takes any file as entry point it just default to a file called main.tsp when you target a directory compile .

Client.tsp is not the only use case. At the very least there should be a choice the user can make.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is about how the files in project can be compiled properly. I believe in most case, starting compilation from "main.tsp" is still the right way to go and simply using the file user selected as entrypoint is likely not to compile the project as expected. So I think we should keep the "main.tsp" as the default behavior while providing some way for people to have the choice when needed.
Some options I can think about:

  • provide a settings for user to set entrypoint file when needed
  • adding a quickpick step for people to select main.tsp (default) or the selected file when user clicks preview

Actually the first one feels like something should be in tspconfig.yaml instead of vscode. @timotheeguerin , do we have any plan for it there?
open to any better ideas.

Copy link
Member

Choose a reason for hiding this comment

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

right now there is if there is a exports or tspMain(deprecated) in package.json but nothing to have sub project per package. Having many project per package is still quite a common use case(whole of the specs repos)

Maybe we should investigate also providing an entrypoint from the config but the way it works today is the other way around it resolve a config for an entrypoint which many can share

return tmpFolder;
}

export async function clearOpenApi3PreviewTempFolders() {
Copy link
Member

Choose a reason for hiding this comment

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

where is this used?

Copy link
Member Author

Choose a reason for hiding this comment

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

}

async function getOutputFolder(mainTspFile: string): Promise<string | undefined> {
let tmpFolder = openApi3TempFolders.get(mainTspFile);
Copy link
Member

Choose a reason for hiding this comment

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

when does caching become useful here?

Copy link
Member Author

Choose a reason for hiding this comment

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

  • When a tsp file is changed, the file watcher will re-generate open api 3 output and reload the preview panel. At that time, if the corresponding temp folder is already created, it will re-use that folder.
  • When the preview panel is closed and opened again, the same logic will be triggered.

Copy link
Member

Choose a reason for hiding this comment

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

Do you clear the dir in between, if the user goes change the name of services or version it could create ghost files otherwise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ide Issues for VS, VSCode, Monaco, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Preview typespec with swagger-ui in vscode