Skip to content

Add --diff to compare a response against a JSON baseline#273

Open
jongio wants to merge 1 commit into
mainfrom
idea/response-diff
Open

Add --diff to compare a response against a JSON baseline#273
jongio wants to merge 1 commit into
mainfrom
idea/response-diff

Conversation

@jongio

@jongio jongio commented Jul 12, 2026

Copy link
Copy Markdown
Owner

What

Adds a --diff <file> flag that compares the JSON response against a saved baseline and prints a unified diff, exiting non-zero when they differ. This turns azd rest into a snapshot-testing tool for APIs in CI.

Both the response and the baseline are canonicalized (parsed as JSON, re-encoded with sorted object keys and normalized whitespace) before comparison, so a change in key order or formatting does not produce a false diff. Only real value changes show up.

Behavior

  • Match: prints nothing, exits 0.
  • Drift: prints a unified diff (baseline vs response, 3 lines of context) to stdout and exits 1.
  • Missing or unreadable baseline, a non-JSON baseline, or a non-JSON response: exits 2 (usage/config), consistent with the existing exit-code contract.

--diff is terminal: it replaces the normal body output so the diff is the only thing on stdout, which keeps it easy to capture or pipe.

Example

# Save a known-good response once
azd rest get https://management.azure.com/subscriptions/{sub}/resourceGroups/{rg}?api-version=2021-04-01 > baseline.json

# Later, in CI, fail the job if the response drifts
azd rest get https://management.azure.com/subscriptions/{sub}/resourceGroups/{rg}?api-version=2021-04-01 --diff baseline.json

Notes

  • Reuses client.IsJSON for the response-type guard, matching how service.go and color.go already gate JSON handling.
  • Promotes github.com/pmezard/go-difflib from an indirect to a direct dependency (it was already in the module graph via testify). No new third-party code is pulled in.
  • Docs updated: CLI reference flag table, exit-code table, and a README example.

Tests cover key-order insensitivity (top level and nested), drift output, and the three exit-code-2 cases, plus end-to-end match and drift through Execute.

Closes #266

--diff <file> canonicalizes the JSON response and a saved baseline (sorted
keys, normalized whitespace) and prints a unified diff when they differ,
exiting non-zero so snapshot checks in CI can catch drift. Object key order
no longer produces false diffs, so only real value changes show up.

Exit codes follow the existing contract: a clean match exits 0, drift exits
1, and a missing or non-JSON baseline (or a non-JSON response) exits 2.

Closes #266

Co-authored-by: Copilot App <[email protected]>
@jongio jongio added the idea Feature idea from the idea pipeline label Jul 12, 2026
@jongio jongio self-assigned this Jul 12, 2026
github-actions Bot added a commit that referenced this pull request Jul 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Website Preview

Your PR preview is ready!

📎 Preview URL: https://jongio.github.io/azd-rest/pr/273/

This preview will be automatically cleaned up when the PR is closed.

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Test This PR

A preview build (0.5.0-pr273) is ready for testing!

One-Line Install (Recommended)

PowerShell (Windows):

iex "& { $(irm https://raw.githubusercontent.com/jongio/azd-rest/main/scripts/install-pr.ps1) } -PrNumber 273 -Version 0.5.0-pr273"

Bash (macOS/Linux):

curl -fsSL https://raw.githubusercontent.com/jongio/azd-rest/main/scripts/install-pr.sh | bash -s 273 0.5.0-pr273

Uninstall

PowerShell (Windows):

iex "& { $(irm https://raw.githubusercontent.com/jongio/azd-rest/main/scripts/uninstall-pr.ps1) } -PrNumber 273"

Bash (macOS/Linux):

curl -fsSL https://raw.githubusercontent.com/jongio/azd-rest/main/scripts/uninstall-pr.sh | bash -s 273

Build Info:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

idea Feature idea from the idea pipeline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add --diff to compare a response against a baseline file

1 participant