Closed eliasespinosa closed 2 months ago
Hey @eliasespinosa, thanks, I will have a look very soon. I’ll try and reproduce the behaviour myself first (I’m assuming you’re getting some kind of error message) and if I don’t see it I’ll ask you for an example.
@eliasespinosa just so you know, I haven’t had the time to look into this yet but I will do so this coming week. Will keep you posted.
OK, I can reproduce this.
If this is the response body:
[
{
"id": 1,
"text": "Do the dishes"
},
{
"id": 2,
"text": "Clean out the trash"
},
{
"id": 3,
"text": "Read the newspaper"
}
]
And here's the test:
[Test]
public void JsonArrayResponseBodyElementCanBeVerifiedUsingNHamcrestMatcher()
{
Given()
.When()
.Get($"{MOCK_SERVER_BASE_URL}/json-array-response-body")
.Then()
.StatusCode(200)
.Body("$[1].text", NHamcrest.Is.EqualTo("Clean out the trash"));
}
I expect the test to pass, but instead it throws an exception:
Message:
Newtonsoft.Json.JsonReaderException : Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
Stack Trace:
JObject.Load(JsonReader reader, JsonLoadSettings settings)
JObject.Parse(String json, JsonLoadSettings settings)
JObject.Parse(String json)
@eliasespinosa is this what you were referencing?
Fixed in https://github.com/basdijkstra/rest-assured-net/commit/00055e342e7e348528bded6e43f2d3c94d5d6b2d.
@eliasespinosa could you have a look and let me know if this is what you expected? Also have a look at the tests please.
If so, I'll add the same logic to extracting response body values, and corresponding tests, and release a new version.
If you want to beta test it first, I'm happy to publish a beta version for you.
Pushed a couple more commits that addresses the problem in a slightly more generic way. Less code, more elegance.
Releasing a new version in 10 minutes or so, please retest with RestAssured .Net version 4.2.2 once it's available, @eliasespinosa. Should be available in an hour or so.
Closing this for the moment, @eliasespinosa, please feel free to reopen if you don’t think this was adequately resolved in 4.2.2
Json body is parsed by default using JObject, but when is in the form of an array it needs to be parsed with JArray instead VerifiableResponse.Body JObject.Parse(result).SelectTokens(path) JArray.Parse(result).SelectTokens(path)