fix: emit valid JSON when --output json falls through to raw#128
fix: emit valid JSON when --output json falls through to raw#128steipete merged 1 commit intosteipete:mainfrom
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2ec8beb463
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // which breaks downstream parsers (e.g. OpenClaw's mcporter bridge). | ||
| if (requestedFormat === 'json') { | ||
| try { | ||
| console.log(JSON.stringify(raw, null, 2)); |
There was a problem hiding this comment.
Handle undefined stringify result in JSON raw fallback
When requestedFormat === 'json', printRaw assumes JSON.stringify(raw, null, 2) either returns JSON text or throws, but for top-level undefined, function, or symbol it returns undefined without throwing. This then logs the literal undefined, which is not valid JSON and still breaks downstream parsers in the exact fallback path this patch is trying to harden.
Useful? React with 👍 / 👎.
|
Addressed Codex P2 in 730d93c:
Fix: check 420/420 tests pass. |
730d93c to
b744c44
Compare
Problem
When
--output jsonis requested andwrapped.json()returnsnull(e.g. an MCP tool returns content without ajson-typed entry),printRaw()is called as the last-resort fallback. This usesutil.inspect(depth: 8), which produces output like:This looks like JS object notation but isn't valid JSON — single quotes, no quoting rules, truncation markers. Downstream JSON parsers (e.g. OpenClaw's mcporter bridge) then fail with:
Fix
When the requested format is
json,printRaw()now usesJSON.stringify()instead ofutil.inspect(), guaranteeing the output is always valid JSON. Falls back toJSON.stringify(String(raw))for non-serializable values (circular references, etc.).Changes
printRaw()accepts an optionalrequestedFormatparameterrequestedFormat === 'json', usesJSON.stringifyinstead ofinspect"raw-only-string"instead of unquoted)util.inspectTesting
All 419 tests pass (103 test files).
Companion to openclaw/openclaw#54728 (QMD 1.1+ mcporter compatibility).
AI-assisted: Built with Claude (Opus 4.6) via OpenClaw. Fully tested.