From d4f78cb5922d9fd83222514dda85db8bf5287f1b Mon Sep 17 00:00:00 2001 From: RRFZ <247457381+Plastic-time@users.noreply.github.com> Date: Sun, 22 Nov 2026 03:53:17 -0500 Subject: [PATCH] fix(cli): preserve explicit null output (#13342) --- src/cli/cliOutputFormatter.ts | 4 ++-- tests/boundary/cli/cliOutputFormatter.test.ts | 7 ++++--- tests/boundary/cli/filteredArrayOutput.test.ts | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/cli/cliOutputFormatter.ts b/src/cli/cliOutputFormatter.ts index d78e17eb04..5906f611d0 100644 --- a/src/cli/cliOutputFormatter.ts +++ b/src/cli/cliOutputFormatter.ts @@ -157,7 +157,7 @@ const formatDocument = ( value: unknown, format: "toon" | "json" | "yaml" | "jsonl", ): string => { - if (value === null || value === undefined) return ""; + if (value === undefined) return ""; switch (format) { case "json": return formatJson(value); @@ -486,7 +486,7 @@ const formatMarkdown = ( value: unknown, path: readonly string[] = [], ): string => { - if (value == null && path.length === 0) return ""; + if (value === undefined) return ""; if (isScalar(value)) { if (path.length === 0) return escapeMarkdownText(String(value)); return `${markdownHeading(path)}${escapeMarkdownText(String(value))}`; diff --git a/tests/boundary/cli/cliOutputFormatter.test.ts b/tests/boundary/cli/cliOutputFormatter.test.ts index 20fc47f4f7..7b2765cbcc 100644 --- a/tests/boundary/cli/cliOutputFormatter.test.ts +++ b/tests/boundary/cli/cliOutputFormatter.test.ts @@ -26,10 +26,11 @@ it.each([ }, ); -it.each<[string, unknown, "toon" | "json" | "yaml" | "jsonl", string]>([ - ["no value as TOON", null, "toon", ""], +it.each<[string, unknown, "toon" | "json" | "yaml" | "jsonl" | "md", string]>([ + ["no value as TOON", undefined, "toon", ""], ["no value as JSON", undefined, "json", ""], - ["no value as YAML", null, "yaml", ""], + ["no value as YAML", undefined, "yaml", ""], + ["no value as Markdown", undefined, "md", ""], ["a scalar as TOON", 12, "toon", "12"], ["text as TOON", "text", "toon", "text"], [ diff --git a/tests/boundary/cli/filteredArrayOutput.test.ts b/tests/boundary/cli/filteredArrayOutput.test.ts index ebc91a5770..d6f59fb717 100644 --- a/tests/boundary/cli/filteredArrayOutput.test.ts +++ b/tests/boundary/cli/filteredArrayOutput.test.ts @@ -4,6 +4,7 @@ import { Writable } from "node:stream"; import { estimateTokenCount } from "tokenx"; import { expect, it } from "vitest"; +import { parseAllDocuments } from "yaml"; import { formatCliOutput } from "../../../src/cli/cliOutputFormatter.js"; import { @@ -63,6 +64,20 @@ const runFilteredCli = async ( return Buffer.concat(chunks).toString("utf8"); }; +it.each(["yaml", "toon", "md"] as const)( + "emits a selected null as %s", + async (format) => { + const stdout = await runFilteredCli({ binding: null }, format, "binding"); + if (format === "yaml") { + const documents = parseAllDocuments(stdout); + expect(documents).toHaveLength(1); + expect(documents[0]?.toJSON()).toBeNull(); + } else { + expect(stdout).toBe("null\n"); + } + }, +); + it.each(["csv", "tsv"] as const)( "exports only explicitly selected %s fields and retains page metadata", async (format) => {