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

Ignore difference between null or empty array #17

Open
paulux84 opened this issue Nov 9, 2023 · 2 comments
Open

Ignore difference between null or empty array #17

paulux84 opened this issue Nov 9, 2023 · 2 comments

Comments

@paulux84
Copy link

paulux84 commented Nov 9, 2023

Can be usefull to add a the possibility in LenientJsonArrayPartialMatcher to ignore difference between null array, array field not exist or empty array.
So this three example become the same

{
    name: "example"
    array : null
}
{
    name: "example"
    array : []
}
{
    name: "example"
}
@deblockt
Copy link
Owner

Hi @paulux84,

I need a little bit refactoring on the library to be able to implement this feature easily,
But you can already do something to manage the comparison between [] and null.
You can implement your own JsonMatcher to replace the default CompositeJsonMatcher, you can write something like that:

public class AdvancedJsonArrayMatcher implements JsonMatcher {
    private final PartialJsonMatcher<ArrayNode> jsonArrayPartialMatcher;
    private final PartialJsonMatcher<ObjectNode> jsonObjectPartialMatcher;
    private final PartialJsonMatcher<ValueNode> primitivePartialMatcher;

    public AdvancedJsonArrayMatcher(
            PartialJsonMatcher<ArrayNode> jsonArrayPartialMatcher,
            PartialJsonMatcher<ObjectNode> jsonObjectPartialMatcher,
            PartialJsonMatcher<ValueNode> primitivePartialMatcher
    ) {
        this.jsonArrayPartialMatcher = jsonArrayPartialMatcher;
        this.jsonObjectPartialMatcher = jsonObjectPartialMatcher;
        this.primitivePartialMatcher = primitivePartialMatcher;
    }

    @Override
    public JsonDiff diff(Path path, JsonNode expected, JsonNode received) {
        // check comparison between array and null values
        if (
                (expected.isEmpty() && received.isNull())
                || (received.isEmpty() && expected.isNull())
        ) {
            return new MatchedPrimaryDiff(path, expected);
        }

        // default JsonMatcher code
        if (expected instanceof ObjectNode  && received instanceof ObjectNode) {
            return this.jsonObjectPartialMatcher.jsonDiff(path, (ObjectNode) expected, (ObjectNode) received, this);
        } else if (expected instanceof ArrayNode && received instanceof ArrayNode) {
            return this.jsonArrayPartialMatcher.jsonDiff(path, (ArrayNode) expected, (ArrayNode) received, this);
        } else if (expected instanceof ValueNode && received instanceof ValueNode){
            return this.primitivePartialMatcher.jsonDiff(path, (ValueNode) expected, (ValueNode) received, this);
        } else {
            return new UnMatchedPrimaryDiff(path, expected, received);
        }
    }
}

And you this class like you use the CompositeJsonMatcher.
For the case:
actual

{
    name: "example"
    array : []
}

and
expected

{
    name: "example"
}

This case il already covered by the LenientJsonObjectPartialMatcher, since it ignore extra values.

To avoid to be able to implement custom JsonMatcher I need to rewrite the PartialJsonMatcher since for now this interface allow to compare two values with the same type, but null and [] don't have the same type.

@paulux84 paulux84 reopened this Nov 16, 2023
@paulux84
Copy link
Author

Seems not working...i have nested arrays and are not working

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