Skip to content

JsonNumEquals fails when MissingNode is passed #37

@grimsa

Description

@grimsa

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();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions