Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 122 additions & 1 deletion src/executor/commander-builder.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { Command } from "commander";
import { loadSpec } from "../parser/loader.js";
import { extractOperations } from "../parser/extractor.js";
import { buildCommands } from "./commander-builder.js";
import type { RuntimeConfig } from "./types.js";
import type { Operation } from "../parser/types.js";
import path from "node:path";

const FIXTURE = path.resolve("test/fixtures/petstore.yaml");
Expand All @@ -15,6 +16,8 @@ const config: RuntimeConfig = {
output: "json",
verbose: false,
quiet: false,
dryRun: false,
validate: false,
};

describe("buildCommands", () => {
Expand Down Expand Up @@ -103,3 +106,121 @@ describe("buildCommands", () => {
expect(listCmd.description()).toBe("List all pets");
});
});

describe("collectParams JSON parsing", () => {
beforeEach(() => {
vi.restoreAllMocks();
});

it("parses object params from JSON strings", async () => {
const op: Operation = {
id: "createVideos",
method: "POST",
path: "/videos",
summary: "Create video",
description: "",
params: [
{ name: "name", in: "body", type: "string", required: true, description: "" },
{ name: "content", in: "body", type: "object", required: true, description: "" },
],
bodyRequired: true,
security: [],
};

let capturedBody: unknown;
vi.stubGlobal("fetch", vi.fn().mockImplementation((_url: string, init: RequestInit) => {
capturedBody = JSON.parse(init.body as string);
return Promise.resolve({
status: 200,
statusText: "OK",
headers: new Map([["content-type", "application/json"]]),
text: () => Promise.resolve("{}"),
});
}));

const spec = await loadSpec(FIXTURE);
const program = new Command();
program.exitOverride();
buildCommands(program, [{ tag: "Videos", description: "", operations: [op] }], config, spec);

await program.parseAsync(["node", "test", "Videos", "create", "--name", "Test", "--content", '{"text":"hello"}']);

expect(capturedBody).toEqual({ name: "Test", content: { text: "hello" } });

vi.unstubAllGlobals();
});

it("parses array params from JSON strings", async () => {
const op: Operation = {
id: "createItem",
method: "POST",
path: "/items",
summary: "Create items",
description: "",
params: [
{ name: "tags", in: "body", type: "array", required: true, description: "" },
],
bodyRequired: true,
security: [],
};

let capturedBody: unknown;
vi.stubGlobal("fetch", vi.fn().mockImplementation((_url: string, init: RequestInit) => {
capturedBody = JSON.parse(init.body as string);
return Promise.resolve({
status: 200,
statusText: "OK",
headers: new Map([["content-type", "application/json"]]),
text: () => Promise.resolve("{}"),
});
}));

const spec = await loadSpec(FIXTURE);
const program = new Command();
program.exitOverride();
buildCommands(program, [{ tag: "Items", description: "", operations: [op] }], config, spec);

await program.parseAsync(["node", "test", "Items", "create", "--tags", '["a","b"]']);

expect(capturedBody).toEqual({ tags: ["a", "b"] });

vi.unstubAllGlobals();
});

it("falls back to string when JSON parse fails", async () => {
const op: Operation = {
id: "createThings",
method: "POST",
path: "/things",
summary: "Create thing",
description: "",
params: [
{ name: "data", in: "body", type: "object", required: true, description: "" },
],
bodyRequired: true,
security: [],
};

let capturedBody: unknown;
vi.stubGlobal("fetch", vi.fn().mockImplementation((_url: string, init: RequestInit) => {
capturedBody = JSON.parse(init.body as string);
return Promise.resolve({
status: 200,
statusText: "OK",
headers: new Map([["content-type", "application/json"]]),
text: () => Promise.resolve("{}"),
});
}));

const spec = await loadSpec(FIXTURE);
const program = new Command();
program.exitOverride();
buildCommands(program, [{ tag: "Things", description: "", operations: [op] }], config, spec);

await program.parseAsync(["node", "test", "Things", "create", "--data", "not-json"]);

expect(capturedBody).toEqual({ data: "not-json" });

vi.unstubAllGlobals();
});
});
12 changes: 12 additions & 0 deletions src/executor/commander-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ function collectParams(
case "boolean":
params[p.name] = value === true || value === "true";
break;
case "object":
case "array":
if (typeof value === "string") {
try {
params[p.name] = JSON.parse(value);
} catch {
params[p.name] = value;
}
} else {
params[p.name] = value;
}
break;
default:
params[p.name] = value;
}
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ function buildDynamicCommands(
params[p.name] = Number(opts[p.name]);
} else if (p.type === "boolean") {
params[p.name] = opts[p.name] === true || opts[p.name] === "true";
} else if ((p.type === "object" || p.type === "array") && typeof opts[p.name] === "string") {
try {
params[p.name] = JSON.parse(opts[p.name] as string);
} catch {
params[p.name] = opts[p.name];
}
} else {
params[p.name] = opts[p.name];
}
Expand Down
Loading