Codeception / module-rest

REST module for Codeception
MIT License
53 stars 27 forks source link

Add a failing test for JsonType assert #61

Open Kolyunya opened 3 years ago

Kolyunya commented 3 years ago

I believe the seeResponseMatchesJsonType() assert is broken and I've added a failing test to illustrate this.

We are going to test different JSON documents against the same assertion. The assertion is:

seeResponseMatchesJsonType(['name' => 'string'], '$.users.*.roles.*');

It means: "Each of the users (if any) and each of his roles(is any) must have a string property name. The if any part is crucial here.

The first document has non-empty roles for all users and the assertion passes:

{
  "users": [
    {
      "id": 1,
      "roles": [
        {
          "name": "admin"
        }
      ]
    }
  ]
}

The second document has non-empty roles for some of the users and the assertion passes:

{
  "users": [
    {
      "id": 1,
      "roles": [
        {
          "name": "admin"
        }
      ]
    },
    {
      "id": 2,
      "roles": []
    }
  ]
}

The last document has all users with an empty roles array and the assertion fails when it should pass:

{
  "users": [
    {
      "id": 1,
      "roles": []
    }
  ]
}

The second example is supplied to justify the fact the last assertion must pass. It shows that the assertion passes for empty arrays.

This issue arose in a real project of mine where I had a paginated resource. On some pages I had some users with roles and the assertion passed. On the other pages, all the users had empty roles array and the assertion unexpectedly failed. The problem here is that I can not see any elegant ways to work around the issue.

DavertMik commented 3 years ago

Thanks! Currently we don't have an opportunity to dive into context to figure out the issue. Maybe you could help us? Seems like the problem somewhere in Util/JsonType file? Probably you could figure it out?