question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot request match when the request JSON body's element order is different from that specified in the Stub

See original GitHub issue
  • WireMock Version: com.github.tomakehurst:wiremock-jre8:2.24.1
  • Framework: Spring Boot 2.0.3 - Release
  • JUnit Version: org.powermock:powermock-module-junit4:2.0.0-beta.5
  • WireMock Configuration:
    @Rule
    public WireMockRule wireMockRule = new WireMockRule(options().bindAddress(WIREMOCK_HOST).port(WIREMOCK_PORT).notifier(new ConsoleNotifier(true)));
  • Stub:

Stub

stubFor(post(urlMatching(URL))
                    .withHeader("Content-Type", equalTo("application/json"))
                    .withRequestBody(equalToJson(REQUEST_BODY_JSON.toString(), true, true))
                    .willReturn(aResponse()
                            .withHeader("Content-Type", "application/json")
                            .withHeader("Connection", "close")
                            .withBody(RESPONSE_JSON.toString())));

Issue Description

The following JSON object (which consists of a few key-value pairs and an array) is used in the withRequestBody() matcher.

{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3",
  "key4": "value4",
  "arr": [
    "arrValue1",
    "arrValue2",
    "arrValue3",
    "arrValue4"
  ]
}

When the following JSON is sent in the actual request body switching the order of the elements in the JSON object, WireMock fails to match it with the Stub.

{
  "arr": [
    "arrValue1",
    "arrValue2",
    "arrValue3",
    "arrValue4"
  ],
  "key1": "value1",
  "key2": "value2",
  "key3": "value3",
  "key4": "value4"
}

Actual Error

                                               Request was not matched
                                               =======================

-----------------------------------------------------------------------------------------------------------------------
| Closest stub                                             | Request                                                  |
-----------------------------------------------------------------------------------------------------------------------
                                                           |
POST                                                       | POST
/mockStub                                                  | /mockStub
                                                           |
Content-Type: application/json                             | Content-Type: application/json
                                                           |
{                                                          | {                                                   <<<<< Body does not match
  "category" : "060",                                      |   "children" : [ {
  "date" : "20200312",                                     |     "id" : "9010946",
  "isRegistered" : true,                                   |     "age" : 1,
  "id" : "12302067",                                       |     "isRegistered" : false
  "children" : [ {                                         |   }, {
    "id" : "9405762",                                      |     "id" : "9405762",
    "age" : 1,                                             |     "age" : 1,
    "isRegistered" : true                                  |     "isRegistered" : true
  }, {                                                     |   }, {
    "id" : "9010946",                                      |     "id" : "9166586",
    "age": 1,                                              |     "age" : 1,
    "isRegistered" : false                                 |     "isRegistered" : false
  }, {                                                     |   }, {
    "id" : "7537984",                                      |     "id" : "7537984",
    "age" : 1,                                             |     "age" : 1,
    "isRegistered" : true                                  |     "isRegistered" : true
  }, {                                                     |   } ],
    "id" : "9166586",                                      |   "category" : "060",
    "age" : 1,                                             |   "id" : "12302067",
    "isRegistered" : false                                 |   "isRegistered" : true,
  } ]                                                      |   "date" : "20200312"
}                                                          | }
                                                           |
-----------------------------------------------------------------------------------------------------------------------

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
tomakehurstcommented, Mar 12, 2020

The JRE8 version of WireMock 2.26+ has a new underlying implementation for JSON equality that fixes some bugs regarding array order. The JRE7 version still uses the old impl because the library used by the new one is Java8+ only.

1reaction
damianpereracommented, Mar 12, 2020

@aaron-goff my bad, that was a copy-paste error - I edited it with the correct snippet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wiremock: how to match a JSON request that does NOT have ...
In order to match a missing JSON property, you can use the matchingJsonPath operator in combination with the absent() , like this:
Read more >
Request Matching - WireMock
Deems a match if the attribute (most likely the request body in practice) is valid JSON and is a semantic match for the...
Read more >
Matching JSON requests - MockLab
When stubbing API functions that accept JSON request bodies we may want to return different responses based on the JSON sent. MockLab provides...
Read more >
WireMock Tutorial: Request Matching, Part Three
This blog post describes how we can specify expectations for the JSON document that is received by our WireMock server.
Read more >
Cypress cy.intercept Problems - Gleb Bahmutov
The server determines the data "cache key" in this case by looking at the if-none-match request header sent by the web application. We...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found