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
9 changes: 9 additions & 0 deletions .changeset/fix-query-array-flags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"cf": patch
---

Preserve arrays in generated query parameter flags

Accept multiple values after a query array flag, such as `--ids first second`,
while retaining support for repeated flags. Parse single values as arrays and
preserve each operation's repeated-key or comma-separated query serialization.
6 changes: 3 additions & 3 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,9 @@ to know before editing `generator/plugin/`.
- Per-field-flag `demandOption: true` is suppressed when `--body` is
also supported (so `--body @file` isn't rejected by yargs for
missing required fields — handler-time enforcement instead).
- Array-typed body params (e.g. KV bulk-get's `--keys`) emit
`array: true, string: true` so users can do `--keys k1 k2` instead
of fighting JSON-string-as-array.
- Array-typed body and query params (e.g. KV bulk-get's `--keys`) emit
`type: "string", array: true` so users can do `--keys k1 k2` or repeat
the flag. Query serialization follows the operation's `explode` setting.

### `@file` token (universal file ingestion)

Expand Down
8 changes: 3 additions & 5 deletions packages/cli/generator/plugin/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,9 @@ function queryParams(
const isEnum = p.enumValues !== undefined && p.enumValues.length > 0;
const type: ArgType = isEnum
? "enum"
: p.type === "number"
? "number"
: p.type === "boolean"
? "boolean"
: "string";
: p.type === "number" || p.type === "boolean" || p.type === "array"
? p.type
: "string";
const a = arg(
name,
description,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { stripTypeScriptTypes } from "node:module";
import { Script } from "node:vm";
import { describe, expect, it } from "vite-plus/test";
import yargs, { type CommandModule } from "yargs";
import { Client, type Operation } from "../../../api/client.js";
import { path } from "../../../api/path.js";
import { generateFiles } from "../../helpers/cli-plugin.js";

describe.each([undefined, false])("query arrays (explode: %s)", (explode) => {
const files = generateFiles({
openapi: "3.0.3",
info: { title: "Query arrays", version: "1" },
paths: {
"/things": {
get: {
"x-fern-sdk-group-name": "things",
"x-fern-sdk-method-name": "list",
summary: "List things",
parameters: [
{
name: "indicatorIds",
in: "query",
required: true,
...(explode === undefined ? {} : { explode }),
schema: { type: "array", items: { type: "string" } },
},
],
responses: {
"200": {
description: "OK",
content: { "application/json": { schema: { type: "object" } } },
},
},
},
},
},
});
const operationSource = stripTypeScriptTypes(
files.get("_operations/things/list.ts") ?? ""
)
.replace(/^import[\s\S]*?;\n/gm, "")
.replace("export default", "const operation =");
const operation = new Script(
`${operationSource}\noperation;`
).runInNewContext({
path,
}) as Operation;
const source = stripTypeScriptTypes(files.get("things/list.ts") ?? "")
.replace(/^import[\s\S]*?;\n/gm, "")
.replace("export default command;", "command;");

async function run(flags: string[]) {
const urls: URL[] = [];
const client = new Client({
baseUrl: "https://api.example.test",
fetch: async (input) => {
urls.push(new URL(input instanceof Request ? input.url : input));
return Response.json({ success: true, result: [] });
},
});
const command = new Script(source).runInNewContext({
operation,
createCommandClient: async () => client,
formatOutput: () => {},
withProgress: async (_label: string, task: () => Promise<unknown>) =>
task(),
runWithTelemetry: (
_info: unknown,
_argv: unknown,
task: () => Promise<void>
) => task(),
}) as CommandModule;
const argv = await yargs(["list", ...flags])
.exitProcess(false)
.command(command)
.strict()
.fail((message, error) => {
throw error ?? new Error(message);
})
.parseAsync();
return { argv, urls };
}

it.each([
{ flags: ["--indicator-ids", "001"], expected: ["001"] },
{
flags: ["--indicator-ids", "001", "a/b &c"],
expected: ["001", "a/b &c"],
},
{
flags: ["--indicator-ids", "001", "--indicator-ids", "a/b &c"],
expected: ["001", "a/b &c"],
},
])("parses and serializes $flags", async ({ flags, expected }) => {
const { argv, urls } = await run(flags);
expect(argv["indicator-ids"]).toEqual(expected);
expect(urls).toHaveLength(1);
expect(urls[0]?.searchParams.getAll("indicatorIds")).toEqual(
explode === false ? [expected.join(",")] : expected
);
expect(urls[0]?.searchParams.has("indicator-ids")).toBe(false);
});

it("requires the array flag", async () => {
await expect(run([])).rejects.toThrow(
"Missing required argument: indicator-ids"
);
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading