smocker icon indicating copy to clipboard operation
smocker copied to clipboard

Ignore order of elements in list in JSON

Open Majestry opened this issue 4 years ago • 1 comments

Let's assume, we expect the next payload to be sent in POST request, that should be caught by smocker: { "tags": ["hello1", "hello2", "hello3"] } If i create the new mock and specify the shown json as body with matcher ShouldEqualJSON, it will work only if we receive the exactly same list in the same order.

If, for example, received body is { "tags": ["hello2", "hello1", "hello3"] } then Smocker will not match it with configured mock and will return 666.

I propose to ignore the order of entries inside the list, since logically this is still the same list in both cases.

Majestry avatar Jul 19 '21 12:07 Majestry

Arrays are ordered so my opinion on this is that this is the correct behavior for JSON array equality.

However I see the need for this, maybe a new ShouldEqualJSONLoose or ShouldEqualJSONNonStrict operator would be a good idea? In the wild I found this lib which has a strict/non strict mode: https://github.com/skyscreamer/JSONassert

When strict is set to false (recommended), it forgives reordering data and extending results (as long as all the expected elements are there), making tests less brittle.

This seems like sane behaviours to implement in a Loose operator.

This is not trivial to implement though

Thiht avatar Jul 19 '21 20:07 Thiht