Skip to content

Fix optional CLI JSON object flags#1496

Merged
ChiragAgg5k merged 2 commits intomasterfrom
codex/fix-cli-optional-json-flags
May 4, 2026
Merged

Fix optional CLI JSON object flags#1496
ChiragAgg5k merged 2 commits intomasterfrom
codex/fix-cli-optional-json-flags

Conversation

@ChiragAgg5k
Copy link
Copy Markdown
Member

What

  • Replaces generated CLI service JSON.parse(...) object argument handling with parseJsonObject(...).
  • Allows omitted optional JSON object flags, including functions create-execution --headers, to pass through as undefined instead of crashing on JSON.parse(undefined).
  • Converts malformed JSON object flag values into clear InvalidArgumentError messages like --headers must be a valid JSON object.
  • Adds a mock functions create-execution fixture and CLI regression coverage for invoking it without --headers.

Testing

  • docker run --rm -v $(pwd):/app -w /app php:8.3-cli php example.php cli
  • composer lint-twig
  • npm run build:types in examples/cli
  • npm run build:runtime in examples/cli
  • vendor/bin/phpunit tests/CLIBun13Test.php
  • Manual malformed flag check in the generated CLI fixture: functions create-execution --function-id fn --headers invalid exits with --headers must be a valid JSON object.

Note: the generated CLI fixture still prints an existing bun run build:types failure for "sites" resource typing during CLIBun13Test; the PHPUnit test harness continues and the test passes.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 4, 2026

Greptile Summary

Replaces inline JSON.parse(varName) calls for object-typed CLI flags with a new parseJsonObject(value, optionName) helper that gracefully returns undefined for omitted optional flags and surfaces a clear InvalidArgumentError for malformed or non-object JSON values. The accompanying Twig template change also applies | raw to all argExpressions | join(', ') output sites, which is necessary to prevent Twig HTML-escaping the double-quoted option-name string literal introduced by the new helper signature.

Confidence Score: 5/5

Safe to merge — the fix is targeted, the helper logic is correct, and regression coverage is added across all three Bun test suites.

No P0 or P1 findings. The parseJsonObject function correctly handles all edge cases (undefined, malformed JSON, valid non-object JSON such as arrays and primitives, and valid objects including empty {}). The | raw filter addition is necessary and applied consistently across all three argExpressions | join branches. Test fixture and mock responses are properly wired in all affected test classes.

No files require special attention.

Important Files Changed

Filename Overview
templates/cli/lib/parser.ts Adds parseJsonObject helper that safely handles undefined (returns undefined), invalid JSON (throws InvalidArgumentError), and valid non-object JSON (throws InvalidArgumentError); logic is sound.
src/SDK/Language/CLI.php Replaces JSON.parse(varName) with parseJsonObject(varName, "--optionName") for object-typed parameters; correctly threads the kebab-case option name through to the error message.
templates/cli/lib/commands/services/services.ts.twig Adds hasObjectParam flag for conditional parseJsonObject import, and adds `
tests/resources/spec.json Adds a functions createExecution fixture endpoint with optional object-typed headers parameter, following the existing test-spec conventions.
tests/languages/cli/test.js Adds regression test that calls functions create-execution --function-id sample-function without --headers to verify the optional object flag no longer crashes.
tests/Base.php Adds CLI_FUNCTION_RESPONSES mock-server constant for the new POST execution endpoint and updates the completion-list assertion to include the new functions service.
tests/CLIBun10Test.php Adds CLI_FUNCTION_RESPONSES to the response set so the mock server handles the new execution endpoint during Bun10 test runs.
tests/CLIBun11Test.php Same CLI_FUNCTION_RESPONSES addition as CLIBun10Test, keeping the three Bun test fixtures in sync.
tests/CLIBun13Test.php Same CLI_FUNCTION_RESPONSES addition; primary test target for this PR's regression coverage.

Reviews (2): Last reviewed commit: "Align CLI JSON object error wording" | Re-trigger Greptile

Comment thread templates/cli/lib/parser.ts Outdated
@ChiragAgg5k ChiragAgg5k merged commit f8a1082 into master May 4, 2026
57 checks passed
@ChiragAgg5k ChiragAgg5k deleted the codex/fix-cli-optional-json-flags branch May 4, 2026 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant