diff --git a/plugins/importer-openapi/src/index.ts b/plugins/importer-openapi/src/index.ts index a39d076a..ea1acac0 100644 --- a/plugins/importer-openapi/src/index.ts +++ b/plugins/importer-openapi/src/index.ts @@ -481,11 +481,12 @@ function parseSpec(contents: string): unknown { function isOpenApiSpec(value: unknown): value is UnknownRecord { const spec = toRecord(value); const openapi = versionString(spec.openapi); + return isRecord(spec.paths) && (/^3(\.|$)/.test(openapi ?? "") || isSwagger2(spec)); +} + +function isSwagger2(spec: UnknownRecord): boolean { const swagger = versionString(spec.swagger); - return ( - isRecord(spec.paths) && - (/^3(\.|$)/.test(openapi ?? "") || swagger === "2.0" || swagger === "2") - ); + return swagger === "2.0" || swagger === "2"; } function versionString(value: unknown): string | undefined { @@ -713,8 +714,7 @@ function importServerEnvironments(spec: UnknownRecord): { name: string; url: str .filter(({ url }) => url.length > 0); if (servers.length === 0) { const hasSwaggerServer = - stringAt(spec, "swagger") === "2.0" && - (stringAt(spec, "host") != null || stringAt(spec, "basePath") != null); + isSwagger2(spec) && (stringAt(spec, "host") != null || stringAt(spec, "basePath") != null); return [ { name: hasSwaggerServer ? "Server 1" : "Default", diff --git a/plugins/importer-openapi/tests/index.test.ts b/plugins/importer-openapi/tests/index.test.ts index 7bc08796..ec441265 100644 --- a/plugins/importer-openapi/tests/index.test.ts +++ b/plugins/importer-openapi/tests/index.test.ts @@ -1251,6 +1251,14 @@ describe("importer-openapi", () => { ); expect(imported?.resources.httpRequests[0]?.url).toBe("${[baseUrl]}/a"); + // The numeric version must feed server detection too, or the selectable + // environment overrides baseUrl with an empty value + expect(imported?.resources.environments[1]).toEqual( + expect.objectContaining({ + name: "Server 1", + variables: [{ name: "baseUrl", value: "https://example.com" }], + }), + ); }); test("Normalizes body types to Yaak's editors", async () => {