lukas-krecan / JsonUnit

Compare JSON in your Unit Tests
Apache License 2.0
894 stars 115 forks source link

Allow wildcard path ignore #807

Open Ragin-LundF opened 3 months ago

Ragin-LundF commented 3 months ago

We like to ignore a field, independent of its structural situation. E.g.:

{
  "store": {
      "book": "HDR",
       "creationDate": "2020-12-12"
   }
}

or

{
  "creationDate": "2020-12-12",
  "store": {
      "book": "HDR"
   }
}

(just that simple).

When I look into the https://github.com/json-path/JsonPath/ library, they are using the pattern $..<fieldName>, so in our concrete case it should be $..creationDate.

Now I looked into the code and I would expect, that if I change the PathMatchers function matches() to this:

        @Override
        boolean matches(String pathToMatch) {
            return path.equals(pathToMatch)
                     || (path.startsWith("$..") && pathToMatch.endsWith(path.substring(2)))
                    || (path.startsWith("$.") && path.substring(2).equals(pathToMatch))
                    || (path.startsWith("$[") && path.substring(1).equals(pathToMatch));
        }

it should work.

Is it possible to implement this?


I found something strange here...

I have no idea how the Diff class works. My test always fails and when I debug this project, it does some magical things.

When I call the method Diff.similar(), the property compared is false. Now similar() calls compare(). Immediately after calling this method, the status of compared has been changed to true and the whole method is skipped.

If I set compared = true within this method to compared = false, the state does not change.

Maybe something is wrong with my debugger in IntelliJ, but I cannot pass the if (!compared) { section in the compare() method, the matcher is not executed properly, but the state changes and I can just avoid this by changing a part of the code, which is never executed....

So I would create a PR, but I'm unable to write a valid test for this change 😔.

lukas-krecan commented 3 months ago

Hi, thanks for reporting. Can you please shere the failing tests? I can somehow reconstruct from the description, but I want to be sure I understand.