waterlink / rspec-json_expectations

Set of matchers and helpers to allow you test your APIs responses like a pro.
https://www.relishapp.com/waterlink/rspec-json-expectations/docs
MIT License
140 stars 23 forks source link

Support for matching unordered arrays in nested jsons #21

Open jeroenj opened 7 years ago

jeroenj commented 7 years ago

We have a quite complex JSON structure in which we have a mix of hashes and arrays. The root node is a Hash causing include_unordered_json to not be supported.

An example of our json (simplified):

{
  "foo" => "bar",
  "baz" => ["one", "three", "two"]
}

In that case we need to know the exact order of the baz array.

A workaround we use now is by explicitly wrapping the array in our expected json in RSpec::JsonExpectations::Matchers::UnorderedArrayMatcher classes:

{
  "foo" => "bar",
  "baz" => RSpec::JsonExpectations::Matchers::UnorderedArrayMatcher.new(["one", "two", "three"])
}

I'd say that include_unordered_json could accept a Hash too and uses the unordered array matcher for arrays it has in its structure.

waterlink commented 7 years ago

@jeroenj As far as I understand, matchers include_unordered_json and match_unordered_json should solve that issue?

jeroenj commented 7 years ago

@waterlink the issue described here indeed is related but it's a bit different. match_unorderd_json can only be used if the root node of the JSON being checked is an Array. In case the root node is a Hash (with an array as one of the keys) match_unordered_json can not be used.

jeroenj commented 7 years ago

I'm looking into refactoring some parts. I think that the following matchers are what we want:

What do you think about it? I think it would cover all possible situates needed but I might be overlooking something. :)

benoittgt commented 7 years ago

Very interested about the match_json 😃

linkyndy commented 7 years ago

What's the status on this issue? I also found myself in the same situation.

XenoPhex commented 5 years ago

We're also very interested in the match_json matcher for the Cloudfoundry's Cloud Controller API tests. We currently have a custom matcher to do JSON validation that errors when the actual JSON has extra fields. Having match_json would allow us to swap this custom matcher for this more generic matcher.

waterlink commented 5 years ago

@XenoPhex I’ll take a look into this. Shall we jump on chat/zoom at some point to discuss how you want to use such a matcher?

tobiasz-p commented 2 years ago

@waterlink @XenoPhex what's the status of this issue?