Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spring-issuemaster opened this issue Jan 2, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Jan 2, 2018

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

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 3, 2018

Rossen Stoyanchev commented

It's certainly not a bug since the code explicitly deals with null, and also the Javadoc talks about "non-null" values (on the exists method). I can see the ambiguity the method name can cause. It's really referring to the existence of a (non-null) value, so it could have been named assertValueExists/assertValueDoesNotExist.

What you're describing sounds more like a check for the existence of a field. We can add an extra pair of methods along the lines of hasJsonPath/hasNoJsonPath.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 4, 2018

Rossen Stoyanchev commented

I've added options for hasJsonPath and doesNotHaveJsonPath. See commit 0f1f95.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 4, 2018

John Ryan Bard commented

Looks awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.