From a2d54ca77476a1523a2fa687d81c506321e3490f Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Wed, 19 Aug 2026 07:07:18 -0700 Subject: [PATCH] Preserve base URL variable when OpenAPI servers are omitted (#585) --- plugins/importer-openapi/src/index.ts | 28 ++++++++++---------- plugins/importer-openapi/tests/index.test.ts | 20 ++++++++++++++ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/plugins/importer-openapi/src/index.ts b/plugins/importer-openapi/src/index.ts index d9f2fb96..417cd496 100644 --- a/plugins/importer-openapi/src/index.ts +++ b/plugins/importer-openapi/src/index.ts @@ -63,20 +63,20 @@ export async function convertOpenApi(contents: string): Promise 0 ? "${[baseUrl]}" : ""; - - if (baseUrl.length > 0) { - resources.environments.push({ - model: "environment", - id: importState.generateId("environment"), - workspaceId: workspace.id, - name: "Global Variables", - variables: [{ name: "baseUrl", value: baseUrl }], - parentModel: "workspace", - parentId: null, - sortPriority: importState.nextSortPriority(), - }); - } + // A local spec has no document URL against which OpenAPI's implicit "/" + // server can resolve. Keep the shared variable even when its initial value + // is empty so users can configure the host once instead of editing requests. + const requestBaseUrl = "${[baseUrl]}"; + resources.environments.push({ + model: "environment", + id: importState.generateId("environment"), + workspaceId: workspace.id, + name: "Global Variables", + variables: [{ name: "baseUrl", value: baseUrl }], + parentModel: "workspace", + parentId: null, + sortPriority: importState.nextSortPriority(), + }); const folderIdsByTag = new Map(); const routeLabels = new Map(); diff --git a/plugins/importer-openapi/tests/index.test.ts b/plugins/importer-openapi/tests/index.test.ts index 68ca24fb..b9dd9977 100644 --- a/plugins/importer-openapi/tests/index.test.ts +++ b/plugins/importer-openapi/tests/index.test.ts @@ -229,6 +229,26 @@ describe("importer-openapi", () => { expect(imported).toBeUndefined(); }); + test("Creates an editable baseUrl variable when OpenAPI omits servers", async () => { + const imported = await convertOpenApi( + JSON.stringify({ + openapi: "3.0.4", + info: { title: "Serverless OpenAPI Test", version: "1.0.0" }, + paths: { + "/api/widgets": { get: { responses: {} } }, + }, + }), + ); + + expect(imported?.resources.environments).toEqual([ + expect.objectContaining({ + name: "Global Variables", + variables: [{ name: "baseUrl", value: "" }], + }), + ]); + expect(imported?.resources.httpRequests[0]?.url).toBe("${[baseUrl]}/api/widgets"); + }); + test("Prefers operation and path servers over the spec base URL", async () => { const imported = await convertOpenApi( JSON.stringify({