New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONPath validator doesn't return array length correctly #277

Closed
gucce opened this Issue Aug 2, 2017 · 0 comments

Comments

Projects
None yet
2 participants
@gucce
Contributor

gucce commented Aug 2, 2017

Problem

See Stackoverflow question

When using the Hamcrest Matcher @assertThat(hasSize(x))@ in combination with a JSONPath expression which matches several nodes whose contents are identical, the length is always 1.

Example

{
  "test_array": [
    {
      "key_with_identical_values": "identical_value",
      "key_with_unique_values": "unique_value"
    },
    {
      "key_with_identical_values": "identical_value",
      "key_with_unique_values": "different_unique_value"
    }
  ]
}

Given the validation as follows:

<receive endpoint="testServer">
  <message type="json">
    <validate>
      <json-path expression="$..key_with_unique_values" value="@assertThat(hasSize(2))@"/>
      <json-path expression="$..key_with_identical_values" value="@assertThat(hasSize(2))@"/>
    </validate>
  </message>
</receive>

Even though the JSONPath array for both expressions is of size 2 (["unique_value", "different_unique_value"] and ["identical_value", "identical_value"]), the validation will fail for the expression $..key_with_identical_values with the message:

Expected: a collection with size <2>
     but: collection size was <1>

Code

The reason for this behavior is the following code fragment:

return StringUtils.commaDelimitedListToSet(arrayString);

which constructs a Set (not a List), which ignores duplicate entries.

There is a method StringUtils.commaDelimitedListToStringArray which would not ignore the duplicate entries. We need to investigate why this is not used and what side-effects it would have if we used it.

@gucce gucce added the Type: Bug label Aug 2, 2017

@gucce gucce self-assigned this Aug 4, 2017

@gucce gucce added this to the v2.7.3 milestone Aug 4, 2017

gucce added a commit that referenced this issue Aug 4, 2017

Fixes #277 JSONPath validator array length
- Use List instead of Set to not ignore duplicate entries
- Add unit and integration tests for this scenario

christophd added a commit that referenced this issue Aug 10, 2017

@christophd christophd removed the IN PROGRESS label Aug 10, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment