mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-25 21:04:04 +02:00
Preserve base URL variable when OpenAPI servers are omitted (#585)
This commit is contained in:
@@ -63,20 +63,20 @@ 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.
|
||||||
resources.environments.push({
|
const requestBaseUrl = "${[baseUrl]}";
|
||||||
model: "environment",
|
resources.environments.push({
|
||||||
id: importState.generateId("environment"),
|
model: "environment",
|
||||||
workspaceId: workspace.id,
|
id: importState.generateId("environment"),
|
||||||
name: "Global Variables",
|
workspaceId: workspace.id,
|
||||||
variables: [{ name: "baseUrl", value: baseUrl }],
|
name: "Global Variables",
|
||||||
parentModel: "workspace",
|
variables: [{ name: "baseUrl", value: baseUrl }],
|
||||||
parentId: null,
|
parentModel: "workspace",
|
||||||
sortPriority: importState.nextSortPriority(),
|
parentId: null,
|
||||||
});
|
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({
|
||||||
|
|||||||
Reference in New Issue
Block a user