wiremock icon indicating copy to clipboard operation
wiremock copied to clipboard

`ignoreExtraElements` ignores extra array elements

Open kubukoz opened this issue 3 years ago • 1 comments

I believe this behavior is inconsistent with the docs.

In the docs, we see this:

By default different array orderings and additional object attributes will trigger a non-match. However, both of these conditions can be disabled individually.

{
  "request": {
    ...
    "bodyPatterns" : [ {
      "equalToJson" : "{ \"total_results\": 4  }",
      "ignoreArrayOrder" : true,
      "ignoreExtraElements" : true
    } ]
    ...
  },
  ...
}

Which doesn't imply that missing array elements are also affected.

In the OpenAPI:

          ignoreExtraElements:
            type: boolean
            default: true
            description: If equalToJson is used, matcher ignores extra elements in objects
          matcher:

which is very specific about extra elements in objects.

However, the code has this:

https://github.com/wiremock/wiremock/blob/23729a1049765d532682aa9c1b7031086436c594/src/main/java/com/github/tomakehurst/wiremock/matching/EqualToJsonPattern.java#L64-L67

which I believe will not only ignore extra object fields but also extra array elements.


I'm reporting this because I've hit an issue of accidental matcher overlap, which resulted in inconsistent responses between MacOS and Linux machines (believe it or not, it was pretty consistent! Everyone running a Mac had one failing test at all times with no good explanation).

Hopefully this can be fixed (at this point likely by amending the documentation, since people are likely to rely on the present behavior) and this issue helps someone looking for an explanation of a similar issue.

kubukoz avatar May 19 '22 21:05 kubukoz

I'd be reluctant to change the semantics of the existing flag as that would be a breaking change for a lot of projects. The documentation is clearly wrong, so that should be fixed.

Perhaps adding separate flags for these while keeping the original one would give you the level of control you need?

tomakehurst avatar May 20 '22 09:05 tomakehurst

I also had the same issue. I wrongly assumed that it's only for extra elements in objects

minaamir26 avatar Aug 08 '23 19:08 minaamir26