Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wiremock: how to match a JSON request that does NOT have a specific property?

I'm trying to mock an API call that accepts a JSON body in a POST and it has two possible responses:

  1. if body contains SearchCenter property, answer with response A
  2. if body does NOT contain SearchCenter, answer with response B

In the Request Matching chapter of Wiremock documentation it only shows how to positively match JSON, it does not show how to match missing properties.

Sample request with SearchCenter:

{
    "GeoCoordinatesResponseFormat": "DecimalDegree",
    "ProviderID": "bla bla",
    "SearchCenter": {
        "GeoCoordinates": {
            "DecimalDegree": {
                "Latitude": "{{search_lat}}",
                "Longitude": "{{search_lon}}"
            }
        },
        "Radius": {{search_radius}}
    }
}

Sample request without SearchCenter:

{
    "GeoCoordinatesResponseFormat": "DecimalDegree",
    "ProviderID": "bla bla"
}
like image 706
Dimitri De Franciscis Avatar asked Sep 01 '25 22:09

Dimitri De Franciscis


1 Answers

In order to match a missing JSON property, you can use the matchingJsonPath operator in combination with the absent(), like this:

.withRequestBody(matchingJsonPath("$.SearchCenter", absent()))
like image 58
Ivan Koryshev Avatar answered Sep 03 '25 13:09

Ivan Koryshev