Skip to content

JsonPathExpectationsHelper doesn't offer a way to distinguish between field not existing and an explicit null [SPR-16339] #20886

@spring-projects-issues

Description

@spring-projects-issues

John Ryan Bard opened SPR-16339 and commented

JsonPathExpectationsHelper's doesNotExist method does not distinguish between the value being null because the jsonPath didn't find a property or because there was an explicit null in the json.

In doesNotExist, if the field/property in the jsonPath isn't there, it eats the AssertionError and returns (as I would expect). If, however, the json has an explicit null for the jsonPath, the doesNotExist falls through to the else statement on line 207 and will fall through without throwing an AssertionError (because the value is null).

If this is a bug in the doesNotExist method, change this ticket to bug/defect instead of feature. If, however, that is not the intent of the doesNotExist method, I would like to have a method that allows me to not only check that there isn't a value in the supplied jsonPath, but also that the field doesn't exist (for jsonPath("$.abc").doesNotExist(), I would want "{\"abc\": null}" to throw an AssertionError and "{}" to pass).

If implementing this feature, be careful not to break empty array behavior mentioned in #17935.

Jayway JsonPath has the JsonPathMatchers.hasNoJsonPath / WithoutJsonPath matcher for this.

Test case:

@Test
public void doesNotExistForAnExplicitNull() throws Exception {
        String expression = "$.none";
        exception.expect(AssertionError.class);
        new JsonPathExpectationsHelper(expression).doesNotExist("{\"none\": null}");
}

No further details from SPR-16339

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions