fix: oas should import requests where possible#9632
fix: oas should import requests where possible#9632jackkav wants to merge 3 commits intoKong:developfrom
Conversation
7621ab2 to
2570ca5
Compare
| let insomnia5Import: ExportedModel[] = []; | ||
| if (contentStr.startsWith('type: ')) { | ||
| const { data, error } = tryImportV5Data(contentStr); | ||
| insomnia5Import = data as ExportedModel[]; | ||
| v5Error = error; | ||
| } |
There was a problem hiding this comment.
I did this because the error spam was killing my debugging experience
| @@ -251,6 +255,7 @@ export async function scanResources(importEntries: ImportEntry[]): Promise<ScanR | |||
| unitTests, | |||
| unitTestSuites, | |||
| requests: [...requests, ...websocketRequests, ...grpcRequests, ...socketIoRequests], | |||
| requestGroups, | |||
There was a problem hiding this comment.
This was most of what was broken about imports before, if im not mistaken
| // in order to support import from api spec yaml | ||
| if (resourceCacheItem?.importer?.id && isApiSpecImport(resourceCacheItem.importer)) { | ||
| // support import from both insomnia export and api spec yaml | ||
| if (resources.find(isApiSpec) || isApiSpecImport(resourceCacheItem.importer)) { |
There was a problem hiding this comment.
this makes the next code support both v5 and oas
| if (isGitProject(project)) { | ||
| await models.workspaceMeta.update(workspaceMeta, { | ||
| gitFilePath: `${newWorkspace.name}-${newWorkspace._id}.yaml`, | ||
| }); | ||
| } |
There was a problem hiding this comment.
moved this down since it was duplicated
2570ca5 to
0157b67
Compare
There was a problem hiding this comment.
Pull request overview
This PR adjusts import scanning and workspace creation to better detect API spec vs Insomnia exports, aiming to ensure OpenAPI/Swagger imports can include generated requests (and related group structure) where possible.
Changes:
- Tightened Insomnia v5 detection during scanning to avoid mis-detecting non-v5 content.
- Exposed
requestGroupsin scan results alongside requests. - Updated new-workspace import flow to create design workspaces when API specs are involved, and adjusted git workspace metadata handling.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packages/insomnia/src/routes/import.scan.tsx | Removes commented-out import parsing snippet from the cURL scan path. |
| packages/insomnia/src/common/import.ts | Updates scan heuristics, includes request groups in scan output, and changes new-workspace import behavior for API specs and git projects. |
| packages/insomnia-smoke-test/tests/smoke/command-palette.test.ts | Adds an extra navigation step in the command palette smoke test. |
Comments suppressed due to low confidence (2)
packages/insomnia/src/common/import.ts:618
- The condition
resources.find(isApiSpec) || isApiSpecImport(resourceCacheItem.importer)treats any import that contains anApiSpecmodel as a raw API-spec-YAML import. For Insomnia exports that include anApiSpecresource (e.g. v5 spec workspace export / workspace duplication), this will incorrectly writeapiSpec.contentsfromresourceCacheItem.content(which can be the entire export payload/JSON) and forcecontentType: 'yaml'. Split the cases: for OAS importers, use the raw content string; for Insomnia exports containing anApiSpecresource, use that resource'scontents/contentTypeand preserve the workspace scope/name from the exported workspace.
// support import from both insomnia export and api spec yaml
if (resources.find(isApiSpec) || isApiSpecImport(resourceCacheItem.importer)) {
newWorkspace = await models.workspace.create({
name: workspaceToImport?.name,
scope: 'design',
parentId: projectId,
});
await models.apiSpec.updateOrCreateForParentId(newWorkspace._id, {
contents: resourceCacheItem.content as string | undefined,
contentType: 'yaml',
fileName: workspaceToImport?.name,
});
packages/insomnia/src/common/import.ts:618
- The updated import path for API spec vs Insomnia export (the
resources.find(isApiSpec)branch) changes workspace creation and apiSpec persistence logic, but there are no unit tests covering this regression-prone behavior (e.g. importing an Insomnia v5 export that contains an ApiSpec should persistapiSpec.contentsfrom the model, not the raw file, and should still import generated requests/groups). Add tests inpackages/insomnia/src/common/__tests__/import.test.tsthat exerciseimportResourcesToProject()for an import file containing an ApiSpec resource.
// support import from both insomnia export and api spec yaml
if (resources.find(isApiSpec) || isApiSpecImport(resourceCacheItem.importer)) {
newWorkspace = await models.workspace.create({
name: workspaceToImport?.name,
scope: 'design',
parentId: projectId,
});
await models.apiSpec.updateOrCreateForParentId(newWorkspace._id, {
contents: resourceCacheItem.content as string | undefined,
contentType: 'yaml',
fileName: workspaceToImport?.name,
});
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| if (isGitProject(project)) { | ||
| await models.workspaceMeta.update(workspaceMeta, { | ||
| gitFilePath: `${newWorkspace.name}-${newWorkspace._id}.yaml`, |
There was a problem hiding this comment.
gitFilePath is derived from newWorkspace.name without sanitization. Workspace names can contain path separators or other unsafe characters, which can create invalid paths or unintended directories in git-backed projects. Use the existing safeToUseInsomniaFileNameWithExt() helper (and path.join if needed) to generate a safe .yaml file name.
| gitFilePath: `${newWorkspace.name}-${newWorkspace._id}.yaml`, | |
| gitFilePath: safeToUseInsomniaFileNameWithExt(`${newWorkspace.name}-${newWorkspace._id}`, 'yaml'), |
| let insomnia5Import: ExportedModel[] = []; | ||
| if (contentStr.startsWith('type: ')) { | ||
| const { data, error } = tryImportV5Data(contentStr); | ||
| insomnia5Import = data as ExportedModel[]; | ||
| v5Error = error; | ||
| } |
There was a problem hiding this comment.
scanResources only attempts Insomnia v5 import when contentStr.startsWith('type: '). Valid v5 YAML may include a BOM, leading whitespace, --- document start, or comments before type:, which would skip v5 parsing and change behavior/error reporting. Consider using contentStr.trimStart() and/or a regex that tolerates YAML preambles (and ideally verifies the value matches *.insomnia.rest/5.0) rather than a strict prefix match.
| @@ -251,6 +255,7 @@ export async function scanResources(importEntries: ImportEntry[]): Promise<ScanR | |||
| unitTests, | |||
| unitTestSuites, | |||
| requests: [...requests, ...websocketRequests, ...grpcRequests, ...socketIoRequests], | |||
| requestGroups, | |||
| workspaces, | |||
There was a problem hiding this comment.
requestGroups is added to the returned scan result, but ScanResult interface does not declare this property. This makes the new data invisible to typed consumers and encourages any usage. Add requestGroups?: RequestGroup[] (and necessary import/type) to ScanResult to keep the contract accurate.
| const { data: insomnia5Import, error } = tryImportV5Data(contentStr); | ||
| v5Error = error; | ||
| let insomnia5Import: ExportedModel[] = []; | ||
| if (contentStr.startsWith('type: ')) { |
There was a problem hiding this comment.
Not sure if this will be true for all yaml imports.
Maybe we can improve the error logging instead?
This appears to have worked previously. Just had to fix the api spec detection