Skip to content

fix: prevent matcher crash when jsonpath result is undefined#132

Open
matmar10 wants to merge 2 commits intostepci:mainfrom
matmar10:fix/matcher-undefined-crash
Open

fix: prevent matcher crash when jsonpath result is undefined#132
matmar10 wants to merge 2 commits intostepci:mainfrom
matmar10:fix/matcher-undefined-crash

Conversation

@matmar10
Copy link

Summary

When a JSONPath expression evaluates to undefined (e.g. $.items[*].name on an empty array []), the in, nin, and match matchers throw a TypeError because they call methods like .includes() on undefined.

Because the JSONPath check block in http.ts wraps ALL jsonpath checks in a single try/catch, this one failure causes every jsonpath check in the step to be reported as failed with the raw response body as the given value — even checks that would have passed individually.

Steps to Reproduce

Create a workflow with a step that checks multiple jsonpath expressions, where one resolves to undefined:

version: "1.1"
name: Repro
tests:
  - name: test
    steps:
      - name: check
        http:
          url: https://httpbin.org/anything
          method: POST
          json:
            name: "Widget"
            items: []
            status: "Done"
          check:
            status: 200
            jsonpath:
              $.json.name:
                - eq: "Widget"
              $.json.status:
                - eq: "Done"
              $.json.items[*].category:
                - in: "Electronics"

Run: npx stepci run workflow.yaml

All three jsonpath checks fail, even though $.json.name and $.json.status would pass.

Expected Behavior

  • $.json.name check → pass (value is "Widget")
  • $.json.status check → pass (value is "Done")
  • $.json.items[*].category check → fail (value is undefined, "Electronics" not found)

Actual Behavior

All three checks fail with the raw response body as the given value.

Root Cause

matcher.ts line 53:

if ('in' in test) return given.includes(test.in)

When given is undefined, throws TypeError: Cannot read properties of undefined (reading 'includes').

http.ts lines 531-549: The try/catch wraps all jsonpath checks together, so the TypeError from one check overwrites all previously successful checks with failures.

Fix

  1. matcher.ts: Guard in, nin, and match matchers against undefined/null values — return false instead of crashing
  2. http.ts: Move try/catch inside the per-key loop so one failing check doesn't poison all other checks

When a JSONPath expression like `$.matchedCategories[*].name` evaluates
against an empty array, the result is `undefined`. The `in`, `nin`, and
`match` matchers called methods on `undefined` (e.g. `undefined.includes()`),
throwing a TypeError.

This error was caught by the catch-all block wrapping ALL jsonpath checks
in http.ts, causing every jsonpath check in the step to be marked as failed
with the raw body string — even checks that would have passed.

Fixes:
- matcher.ts: guard `in`, `nin`, and `match` against undefined/null `given`
- http.ts: isolate try/catch per jsonpath key so one failure doesn't
  poison all other checks

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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