Skip to content

fix: resolve panic on JSON array comparison in body assertions#48

Merged
danielbowne merged 3 commits into
mainfrom
fix/json-comparison-and-structured-output
Feb 26, 2026
Merged

fix: resolve panic on JSON array comparison in body assertions#48
danielbowne merged 3 commits into
mainfrom
fix/json-comparison-and-structured-output

Conversation

@danielbowne

@danielbowne danielbowne commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Refactors compare() into compare() + compareValues() to handle []interface{} (JSON arrays) which previously caused a runtime panic
  • Adds strict array length matching with element-by-element recursive comparison using indexed error paths (e.g., body.json.data[0].name)
  • Hardens all type assertions with comma-ok pattern to prevent panics on missing keys or type mismatches

Closes #36

Test plan

  • All 25 existing BATS tests pass with no changes
  • New test: SHOULD PASS with matching JSON arrays
  • New test: SHOULD FAIL with mismatched JSON array element
  • New test: SHOULD FAIL with JSON array length mismatch

Refactor compare() into compare() + compareValues() to support
[]interface{} (JSON arrays) with strict length matching and indexed
error paths. Also hardens all type assertions with comma-ok pattern
to prevent panics on missing keys or type mismatches.

Closes #36
…overage

Compare overlapping elements even when array lengths differ so users
see all errors at once. Add tests for primitive arrays and nested
arrays. Document precision caveat on int-to-float64 comparison and
clarify default case type assumptions.

@ARPerry87 ARPerry87 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but probably should also have Sinan or Zach look it over as well!

@danielbowne

danielbowne commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator Author

Tested this branch against the ZTMF project's full E2E suite (73 tests) with real array body assertions that previously panicked on v0.3.1.

Test that was panicking:

  - url: http://localhost:8090/api/v1/cfactssystems
    method: GET
    headers:
      <<: *issoHeaders
    expect:
      status: 200
      body:
        json:
          data:
            - fisma_uuid: "E1D00198-36D4-4EAB-8C00-501E1D000999"
              fisma_acronym: "SLD-GEN"

Built the branch locally, swapped the binary, ran against an isolated test database — 73/73 passed with 0 failures. No panic, array comparison worked as expected.

@cleong14 cleong14 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Logic is sound and confirmed tests are passing.

@danielbowne danielbowne merged commit b8230fb into main Feb 26, 2026
3 checks passed
@danielbowne danielbowne deleted the fix/json-comparison-and-structured-output branch February 26, 2026 00:26
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.

emberfall panics when expecting an array in body.json

3 participants