Preserve base URL variable when OpenAPI servers are omitted (#585)

This commit is contained in:
Gregory Schier
2026-08-19 07:07:18 -07:00
committed by GitHub
parent 36fec8b005
commit a2d54ca774
2 changed files with 34 additions and 14 deletions
+4 -4
View File
@@ -63,9 +63,10 @@ export async function convertOpenApi(contents: string): Promise<ImportPluginResp
httpRequests: [], httpRequests: [],
}; };
const baseUrl = importBaseUrl(spec); const baseUrl = importBaseUrl(spec);
const requestBaseUrl = baseUrl.length > 0 ? "${[baseUrl]}" : ""; // A local spec has no document URL against which OpenAPI's implicit "/"
// server can resolve. Keep the shared variable even when its initial value
if (baseUrl.length > 0) { // is empty so users can configure the host once instead of editing requests.
const requestBaseUrl = "${[baseUrl]}";
resources.environments.push({ resources.environments.push({
model: "environment", model: "environment",
id: importState.generateId("environment"), id: importState.generateId("environment"),
@@ -76,7 +77,6 @@ export async function convertOpenApi(contents: string): Promise<ImportPluginResp
parentId: null, parentId: null,
sortPriority: importState.nextSortPriority(), sortPriority: importState.nextSortPriority(),
}); });
}
const folderIdsByTag = new Map<string, string>(); const folderIdsByTag = new Map<string, string>();
const routeLabels = new Map<string, string>(); const routeLabels = new Map<string, string>();
@@ -229,6 +229,26 @@ describe("importer-openapi", () => {
expect(imported).toBeUndefined(); 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 () => { test("Prefers operation and path servers over the spec base URL", async () => {
const imported = await convertOpenApi( const imported = await convertOpenApi(
JSON.stringify({ JSON.stringify({