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

Ignoring non-existing properties #2143

Closed
AhmetTanakol opened this issue Mar 11, 2021 · 2 comments
Closed

Ignoring non-existing properties #2143

AhmetTanakol opened this issue Mar 11, 2021 · 2 comments

Comments

@AhmetTanakol
Copy link

AhmetTanakol commented Mar 11, 2021

Summary

I couldn't find a way to ignore properties that don't exist in the actual object during recursive comparison. Adding the ignored fields with dummy values to the actual object and setting ignoreFields works. Is there a way to skip non-existing properties?

Example

        String actualJson = "{ \"id\": null, \"name\": \"Foo\",\"updatedAt\": null }";
        String expectedJson = "{ \"name\": \"Foo\" }";
        String[] ignoreFields = {"id","updatedAt"};
        JsonNode actualResponse = OBJECT_MAPPER.readTree(actualJson);
        JsonNode expectedResponse = OBJECT_MAPPER.readTree(expectedJson);

        RecursiveComparisonConfiguration config = new RecursiveComparisonConfiguration();
        config.ignoreFields(ignoreFields);
        assertThat(actualResponse).usingRecursiveComparison(config).isEqualTo(expectedResponse);
@joel-costigliola
Copy link
Member

for testing json, I recommend JSONUnit, it turns out you can ignore fields in JSON Unit https://github.com/lukas-krecan/JsonUnit#ignoreelements - give it a try and see if it works for you.

@joel-costigliola
Copy link
Member

This can't be supported as JsonNode is an iterable and the recursive comparison compares iterables by looking at their content since the 2 json nodes have different size (3 vs 1) the comparison fails.

JsonNode and all subtype nodes don't work well with the recursive comparison as everything is an iterable, the recursive comparison won't never compare the node values (for example IntNode has a _value field but it is not compared as the recursive comparison treats it as an iterable which is empty is this case).

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