Hi, I am having some trouble using the `absent` fe...
# help
j
Hi, I am having some trouble using the
absent
feature with
matchesJsonPath
when attempting to filter request logs. I'll explain below 👍 if I check equality of the
$.entitlementId
alone returns 1 request. Request:
Copy code
POST /requests/count
{
    "method": "POST",
    "urlPathPattern": "/merchant-entitlement/ns/804c2d9f-3689-4095-8403-fd0525fb834b/entitlements",
    "bodyPatterns": [
        {
            "matchesJsonPath": {
                "expression": "$.entitlementId",
                "equalTo": "e331678d-160a-4530-a966-57341666b0b2"
            }
        }
    ]
}
Response:
200 OK
Copy code
{
  "count": 1,
  "requestJournalDisabled": false
}
So this is evidence that the property appears in the log and is equal. if I switch it to use
absent
My expectation is that this will check if the field itself exists - so I perform the following request:
Copy code
POST /requests/count
{
    "method": "POST",
    "urlPathPattern": "/merchant-entitlement/ns/804c2d9f-3689-4095-8403-fd0525fb834b/entitlements",
    "bodyPatterns": [
        {
            "matchesJsonPath": {
                "expression": "$.entitlementId",
                "absent": false
            }
        }
    ]
}
Response:
200 OK
Copy code
{
  "count": 0,
  "requestJournalDisabled": false
}
My thinking here that I am checking to ensure the property exists - which it does since the request first request for field equality returns a count of 1. If I change the above request to
"absent":"true"
I would expect it to return the above response like it does e.g
"count":0
. I've tried looking for more information about how
absent
works but haven't been able to locate it so I'm hoping someone here can support 🙏 thank you Further note: before I went down a bit of a rabbit hole here - my core requirement was to use this endpoint to ensure fields in requests do not exist e.g.
Copy code
entitlementId == xyz && otherFields does not exist
I'm unsure if this is possible, and if not - is there another way? wiremock server - docker - wiremock:3.4.1-2
t
I think you’ve tripped over a quirk of how `absent`works (I agree what you’re doing should work as you’ve described). Try this instead:
Copy code
{
  "method": "POST",
  "urlPathPattern": "/merchant-entitlement/ns/804c2d9f-3689-4095-8403-fd0525fb834b/entitlements",
  "bodyPatterns": [
    {
      "matchesJsonPath": {
        "expression": "$.entitlementId",
        "not": {
          "absent": true
        }
      }
    }
  ]
}
j
🤯 🙂 - thanks for you reply. I'll try this is the morning. Is the "not" property documented anywhere?
t
I’ve just realised it isn’t…
But it works the same way as
and
and
or
, both of which are documented: https://wiremock.org/docs/request-matching/#logical-and-and-or
j
thanks for the info will try it tomorrow and feedback
t
No probs 👍
j
So toggling
Copy code
"bodyPatterns": [
            {
                "matchesJsonPath": {
                    "expression": "$.entitlementId",
                    "not": {
                        "absent": false
                    }
                }
            }
        ]
absent to
true
and
false
continues to return
Copy code
{
  "count": 1,
  "requestJournalDisabled": false
}
in both cases 🤷 I'd expect: •
not absent: true
would ensure it exists •
not absent: false
would ensure it doesn't exist. But I'm not seeing any differentiation on the response
t
@Lee Turner perhaps since we’re making some breaking changes in 4.x this would be a good candidate to clean up a bit. WDYT?
l
Seems like a good idea to me
j
So are you lining up a feature in a future server release? Are you aware of any other way I can check to ensure values on requests that hit a mock do not exist? Thank you
t
Not sure I understand the problem now - you can achieve both cases with what's there now, it's just a bit ugly
j
Apologies its quite difficult to explain. When i get a request that matches for
$.entitlementId
using
equalTo
- the count returns as
1
so i can confirm it exists. I have tried using:
Copy code
{
            "matchesJsonPath": {
                "expression": "$.entitlementId",
                "absent": false/true
            }
}
And both return a count of
0
I would expect the request to
/requests/count
to return 1 in this case:
Copy code
{
  "matchesJsonPath": {
    "expression": "$.entitlementId",
    "not": {
      "absent": true
    }
  }
}
But it returns a count of
0
t
Can you share examples of the request body you want to match, and one you don’t want to match?
j
Sure see the insomnia export attached - quickly threw together expectations with some tests.
Copy code
docker run -it --rm \
  -p 8080:8080 \
  --name wiremock \
  wiremock/wiremock:3.4.1-2
t
Could you export that to Postman? My Insomnia installation is crashing.
j
Had to export everything - found this guide
Hello again, did you manage to get the above working?
t
I’m still not sure what the issue is. When I call /find with the following I get 0:
Copy code
"matchesJsonPath": {
									"expression": "$.entitlementId",
									"absent": true
                                    
							}
And with this I get 1:
Copy code
"matchesJsonPath": {
									"expression": "$.entitlementId",
									"not": {
                                        "absent": true
                                    }
							}
j
I'm using
__admin/requests/count
And i'm expecting toggling absent to true and false to yield different results, however they are the same.
t
As I mentioned, just toggling it false doesn’t work at the moment, but wrapping it with
not
does the equivalent of
"absent": false
. Is there any reason you can’t do that?
j
I don't think it fits what i'm trying to achieve in this scenario. but thanks for confirming and i appreciate the support - i'll take another look through the documentation and see if theres another way to achieve it.
👍 1