Skip to content

match $.field == '#notpresent' throws error when field doesn't exist #2892

@rc2201

Description

@rc2201

Description

In Karate v2, match $.field == '#notpresent' throws No results for path: $['field'] when the field doesn't exist in the response. In v1, this correctly passed — asserting the field is absent.

It seems like the issue is specifically with $ JsonPath syntax. Dot-notation (response.field) works correctly with #notpresent.

Steps to Reproduce

  1. Create a feature file using the snippets below.
Feature: match $.field == '#notpresent' throws error when field doesn't exist (v2 regression)

  # Bug: $ JsonPath syntax throws "No results for path" on missing fields,
  # breaking #notpresent assertions. Dot-notation works as workaround.

  Scenario: $ JsonPath - existing field works, missing field breaks #notpresent
    Given url 'https://httpbin.org'
    And path '/anything'
    And request { name: 'test', value: 123 }
    When method post
    Then status 200
    # PASSES: existing field with $ syntax
    * match $.method == 'POST'
    # FAILS in v2: "No results for path: $['errors']"
    # PASSES in v1: correctly asserts field is absent
    * match $.errors == '#notpresent'

  Scenario: response dot-notation - workaround that works in both v1 and v2
    Given url 'https://httpbin.org'
    And path '/anything'
    And request { name: 'test', value: 123 }
    When method post
    Then status 200
    # PASSES in both v1 and v2
    * match response.method == 'POST'
    * match response.errors == '#notpresent'

Expected Behavior

  1. The scenario should pass, matching the v1 behavior.
Image

Actual Behavior

  1. Scenario fails with $ JsonPath syntax when the field is not present.
Image

Karate Version

2.0.9

Java Version

25.0.2

Operating System

macOS

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions