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

JsonNumEquals fails when MissingNode is passed #37

Open
grimsa opened this issue Nov 21, 2019 · 2 comments
Open

JsonNumEquals fails when MissingNode is passed #37

grimsa opened this issue Nov 21, 2019 · 2 comments

Comments

@grimsa
Copy link

grimsa commented Nov 21, 2019

Problem
The following code:

JsonNode original = originalJson.at("/some/path");    // produces MissingNode instance
JsonNode updated = updatedJson.at("/some/path");
boolean valid = JsonNumEquals.getInstance().equivalent(original, updated);

results in

java.lang.NullPointerException: unhandled token type NOT_AVAILABLE
	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:897)
	at com.github.fge.jackson.NodeType.getNodeType(NodeType.java:144)
	at com.github.fge.jackson.JsonNumEquals.doEquivalent(JsonNumEquals.java:68)
	at com.github.fge.jackson.JsonNumEquals.doEquivalent(JsonNumEquals.java:43)
	at com.google.common.base.Equivalence.equivalent(Equivalence.java:65)

The cause seems to be that there is no enum value in com.github.fge.jackson.NodeType that corresponds to MissingNode.

Workaround:

private boolean nodesEqual(JsonNode original, JsonNode updated) {
    return !oneMissing(original, updated)
            && (bothMissing(original, updated) || JsonNumEquals.getInstance().equivalent(original, updated));
}

private boolean bothMissing(JsonNode original, JsonNode updated) {
    return original.isMissingNode() && updated.isMissingNode();
}

private boolean oneMissing(JsonNode original, JsonNode updated) {
    return original.isMissingNode() ^ updated.isMissingNode();
}
@Capstan
Copy link

Capstan commented Nov 26, 2019

From the commentary in NodeType, it was intended that it be a short-lived replacement until Databind's JsonNodeType could be used, and that has an entry for MISSING.

I would be happy to review contributions of either a NodeType entry addition of MISSING, or a larger refactor to use JsonNodeType from Databind, or both, serially.

@grimsa
Copy link
Author

grimsa commented May 19, 2022

For the record, we have since switched to a more generic workaround presented here: #45 (comment)

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

No branches or pull requests

2 participants