mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-21 02:44:12 +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: [],
|
||||
};
|
||||
const baseUrl = importBaseUrl(spec);
|
||||
const requestBaseUrl = baseUrl.length > 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<string, string>();
|
||||
const routeLabels = new Map<string, string>();
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user