spec-kit icon indicating copy to clipboard operation
spec-kit copied to clipboard

feat(workflows): add from_json expression filter

Open doquanghuy opened this issue 5 months ago • 2 comments

Description

Fixes #2960.

Adds one arg-less pipe filter to the sandboxed expression evaluator:

items: "{{ steps.emit.output.stdout | from_json }}"

_filter_from_json parses a JSON string into its typed value (list/dict/scalar). Semantics are parse-or-raise: invalid JSON or non-string input raises a clear ValueError — never a silent passthrough, since a parse failure means the pipeline wiring is wrong and silence would hide it. The module docstring's filter list is updated; no other filter or evaluator behavior is touched.

This is the smallest unblock for feeding fan-out items: (or any condition/arg) from a step that computes a collection at runtime. It composes with — and doesn't preclude — a future declared-outputs: mechanism, which I'm proposing separately.

Testing

  • [x] Ran existing tests with uv sync && uv run pytest — tests/test_workflows.py 210 passed
  • [x] Three new tests in TestExpressions (valid JSON → typed value; invalid JSON raises; non-string raises) — all three are red against current main, green with the filter (verified both directions)
  • [x] uvx ruff check src/ — clean
  • [x] Tested locally with uv run specify --help
  • [ ] Tested with a sample project (covered by the evaluator tests; exercised manually with the #2956 repro workflow rewired through | from_json)

AI Disclosure

  • [ ] I did not use AI assistance for this contribution
  • [x] I did use AI assistance (describe below)

Code, tests, and this description were authored with AI assistance (Claude); verified by running the repo's test suite and ruff locally in both red and green directions.

doquanghuy avatar Jun 12 '26 17:06 doquanghuy

@mnriem when you have a moment, would appreciate a review — happy to adjust anything.

doquanghuy avatar Jun 12 '26 17:06 doquanghuy

Please address Copilot feedback

mnriem avatar Jun 16 '26 20:06 mnriem

@mnriem Thanks for the review — addressed the Copilot feedback:

  • from_json is now strict: any parenthesized form (from_json() or from_json('x')) raises a clear error instead of silently returning the unparsed value.
  • Renamed the mis-named test to test_filter_from_json_parses_object (the JSON under test is an object), and added coverage for the strict no-arguments behavior.

Full suite green; ruff check src/ clean. Ready for another look.

doquanghuy avatar Jun 17 '26 02:06 doquanghuy

@mnriem Pushed 0eba146 addressing the latest Copilot comment:

  • Documented the new from_json filter in the workflows/ARCHITECTURE.md filter table with the suggested example {{ steps.emit.output.stdout | from_json }}.
  • Preempted the same gap in the other two filter enumerations so all user-facing docs match the evaluator: workflows/README.md and docs/reference/workflows.md.

The earlier strictness fix (parenthesized forms like from_json() / from_json('x') raise instead of silently passing through) and the test rename are unchanged.

doquanghuy avatar Jun 17 '26 13:06 doquanghuy

Please address Copilot feedback

mnriem avatar Jun 17 '26 15:06 mnriem

@mnriem Pushed fd896a3 for the latest Copilot round:

  • Strictness now rejects trailing tokens, not just parens. Typos like | from_json) or | from_json extra previously fell through to the unknown-filter path and silently returned the unparsed value. The check now matches the leading filter token and requires the whole filter to be exactly from_json, so every mis-wired form raises. Extended the rejection test to cover the trailing-token cases (7 forms total).
  • Docstring fixed. No file I/O, no imports was misleading after the json import; reworded to state the actual sandbox guarantee (templates cannot do file I/O, import modules, or run arbitrary code).

Full tests/test_workflows.py green (211 passed).

doquanghuy avatar Jun 17 '26 15:06 doquanghuy

Thank you!

mnriem avatar Jun 17 '26 18:06 mnriem