Skip to content

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

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

Closed
spring-projects-issues opened this issue Jan 2, 2018 · 3 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues 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-projects-issues
Copy link
Collaborator Author

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-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

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

@spring-projects-issues
Copy link
Collaborator Author

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
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants