Skip to content

fix(gh): passthrough output when --jq flag is present#319

Closed
jackfreem wants to merge 1 commit intortk-ai:masterfrom
jackfreem:fix/gh-api-jq-passthrough
Closed

fix(gh): passthrough output when --jq flag is present#319
jackfreem wants to merge 1 commit intortk-ai:masterfrom
jackfreem:fix/gh-api-jq-passthrough

Conversation

@jackfreem
Copy link

Summary

Fixes #311.

rtk gh api with --jq was returning type placeholders (int, string, { tag_name: string }) instead of real values.

Root cause

run_api() unconditionally passed all gh api output through json_cmd::filter_json_string(), which is a schema extractor — it replaces every JSON value with its type descriptor. When --jq is present, gh has already applied the user's filter and returned targeted output (e.g. a scalar 104 or a small object). Running that through the schema extractor destroys the data the user asked for.

Fix

Detect --jq/--jq=<expr> in args and print the output as-is. Adds a has_jq_flag() helper following the existing should_passthrough_run_view() pattern.

Before / After

# Before
rtk gh api search/issues --method GET -f q='repo:rtk-ai/rtk is:issue' -f per_page=1 --jq '.total_count'
# → int   ❌

rtk gh api "/repos/rtk-ai/rtk/releases?per_page=1" --jq '.[0] | {tag_name,published_at}'
# → { published_at: string, tag_name: string }   ❌

# After
rtk gh api search/issues --method GET -f q='repo:rtk-ai/rtk is:issue' -f per_page=1 --jq '.total_count'
# → 104   ✅

rtk gh api "/repos/rtk-ai/rtk/releases?per_page=1" --jq '.[0] | {tag_name,published_at}'
# → {"published_at":"2026-02-28T21:10:20Z","tag_name":"v0.23.0"}   ✅

Tests

5 new unit tests added to gh_cmd::tests:

  • test_has_jq_flag_long_form--jq .filter style
  • test_has_jq_flag_equals_form--jq=.filter style
  • test_has_jq_flag_absent — no flag present
  • test_has_jq_flag_empty — empty args
  • test_api_jq_output_not_schema_filtered — demonstrates filter_json_string("104") returns "int", confirming why the passthrough is necessary
  • test_api_jq_object_would_be_schema_mangled — demonstrates object projections lose real values without the fix

All 28 gh_cmd tests pass.

When `gh api` is called with `--jq`, gh applies the filter and returns
already-user-filtered output (e.g. a scalar "104" or a compact object).
RTK was then passing this through `filter_json_string()`, which replaces
all JSON values with type placeholders — turning "104" into "int" and
{"tag_name":"v0.23.0"} into {tag_name: string}.

Fix: detect `--jq`/`--jq=<expr>` in args and print the output as-is.
The user has already expressed exactly what data they want; schema
extraction on top of that is wrong.

Adds `has_jq_flag()` helper (testable, follows existing repo pattern)
and five unit tests covering the flag detection and the breakage case.

Fixes rtk-ai#311
@FlorianBruniaux
Copy link
Collaborator

Hi @jackfreem, @rursache! We have several PRs tackling gh flag passthrough (#196, #217, #319, #325, #328) — they all touch gh_cmd.rs and will conflict with each other. Before we review, can you coordinate? We're leaning toward consolidating into #328 (most complete). @jackfreem, would your --jq fix (#319) fit as an addition to #328?

@pszymkowiak
Copy link
Collaborator

Fixed in v0.27.0 — gh with --jq now skips rewrite.

@pszymkowiak pszymkowiak closed this Mar 5, 2026
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.

bug: rtk gh api --jq returns type placeholders (int/string) instead of real values

3 participants