jsonhandlebars.jswiremockwiremock-standalone

Response Template in Wiremock Standalone is not working with json string


I have the following response configuration that I'm testing with wiremock standalone jar v3.4.0.

{
  "id": "e6e2b5cf-52c1-3b0a-b368-3ea71e444fbf",
  "request": {
    "urlPathPattern": "/test",
    "method": "GET"
  },
  "response": {
    "status": 200,
    "jsonBody": {
      "data": [
        {
          "key1": "value",
          "nestedKey1": {
            "key2": "value2"
          },
          "jsonStringKey1": "{\"key\": \"value\", \"nestedKey\": {\"key2\": \"value2\"},}"
        }
      ]
    },
    "transformers": [
      "response-template"
    ]
  },
  "uuid": "e6e2b5cf-52c1-3b0a-b368-3ea71e444fbf"
}

When I tried hitting this endpoint, I'm getting the following error, where handlebars is trying to see if there are any expressions in the value of jsonStringKey1 in the above response. Here's the error I'm getting:

HTTP ERROR 500 com.github.tomakehurst.wiremock.common.JsonException: { "errors" : [ { "code" : 10, "source" : { }, "title" : "Error parsing JSON", "detail" : "Unexpected character ('k' (code 107)): was expecting comma to separate Object entries\n at [Source: (String)\"{\"data\":[{\"key1\":\"value\",\"nestedKey1\":{\"key2\":\"value2\"},\"jsonStringKey1\":\"{\"key\": \"value\", \"nestedKey\": {\"key2\": \"value2\"},}\"}]}\"; line: 1, column: 78]" } ] }
URI:    /test
STATUS: 500
MESSAGE:    com.github.tomakehurst.wiremock.common.JsonException: { "errors" : [ { "code" : 10, "source" : { }, "title" : "Error parsing JSON", "detail" : "Unexpected character ('k' (code 107)): was expecting comma to separate Object entries\n at [Source: (String)\"{\"data\":[{\"key1\":\"value\",\"nestedKey1\":{\"key2\":\"value2\"},\"jsonStringKey1\":\"{\"key\": \"value\", \"nestedKey\": {\"key2\": \"value2\"},}\"}]}\"; line: 1, column: 78]" } ] }
SERVLET:    com.github.tomakehurst.wiremock.servlet.WireMockHandlerDispatchingServlet-37eeec90
CAUSED BY:  com.github.tomakehurst.wiremock.common.JsonException: { "errors" : [ { "code" : 10, "source" : { }, "title" : "Error parsing JSON", "detail" : "Unexpected character ('k' (code 107)): was expecting comma to separate Object entries\n at [Source: (String)\"{\"data\":[{\"key1\":\"value\",\"nestedKey1\":{\"key2\":\"value2\"},\"jsonStringKey1\":\"{\"key\": \"value\", \"nestedKey\": {\"key2\": \"value2\"},}\"}]}\"; line: 1, column: 78]" } ] }
Caused by:
com.github.tomakehurst.wiremock.common.JsonException: {
  "errors" : [ {
    "code" : 10,
    "source" : { },
    "title" : "Error parsing JSON",
    "detail" : "Unexpected character ('k' (code 107)): was expecting comma to separate Object entries\n at [Source: (String)\"{\"data\":[{\"key1\":\"value\",\"nestedKey1\":{\"key2\":\"value2\"},\"jsonStringKey1\":\"{\"key\": \"value\", \"nestedKey\": {\"key2\": \"value2\"},}\"}]}\"; line: 1, column: 78]"
  } ]
}
    at com.github.tomakehurst.wiremock.common.JsonException.fromJackson(JsonException.java:52)
    at com.github.tomakehurst.wiremock.common.Json.read(Json.java:78)
    at com.github.tomakehurst.wiremock.common.Json.node(Json.java:152)
    at com.github.tomakehurst.wiremock.http.Body.<init>(Body.java:57)
    at com.github.tomakehurst.wiremock.http.Body.fromJsonBytes(Body.java:82)
    at com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer.applyTemplatedResponseBody(ResponseTemplateTransformer.java:210)
    at com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer.transform(ResponseTemplateTransformer.java:99)
    at com.github.tomakehurst.wiremock.stubbing.AbstractStubMappings.applyV2Transformations(AbstractStubMappings.java:142)
    at com.github.tomakehurst.wiremock.stubbing.AbstractStubMappings.serveFor(AbstractStubMappings.java:100)
    at com.github.tomakehurst.wiremock.core.WireMockApp.serveStubFor(WireMockApp.java:270)
    at com.github.tomakehurst.wiremock.http.StubRequestHandler.handleRequest(StubRequestHandler.java:72)
    at com.github.tomakehurst.wiremock.http.AbstractRequestHandler.handle(AbstractRequestHandler.java:72)
    at com.github.tomakehurst.wiremock.servlet.WireMockHandlerDispatchingServlet.service(WireMockHandlerDispatchingServlet.java:157)
    at wiremock.jakarta.servlet.http.HttpServlet.service(HttpServlet.java:587)
    at wiremock.org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:764)
    at wiremock.org.eclipse.jetty.servlet.ServletHandler$ChainEnd.doFilter(ServletHandler.java:1665)
    at wiremock.org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:527)
    at wiremock.org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:221)
    at wiremock.org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1381)
    at wiremock.org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:176)
    at wiremock.org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:484)
    at wiremock.org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:174)
    at wiremock.org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1303)
    at wiremock.org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:129)
    at wiremock.org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:122)
    at wiremock.org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:822)
    at wiremock.org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:141)
    at wiremock.org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:122)
    at wiremock.org.eclipse.jetty.server.Server.handle(Server.java:563)
    at wiremock.org.eclipse.jetty.server.HttpChannel$RequestDispatchable.dispatch(HttpChannel.java:1598)
    at wiremock.org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:753)
    at wiremock.org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:501)
    at wiremock.org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:287)
    at wiremock.org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:314)
    at wiremock.org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:100)
    at wiremock.org.eclipse.jetty.io.SelectableChannelEndPoint$1.run(SelectableChannelEndPoint.java:53)
    at wiremock.org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:969)
    at wiremock.org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.doRunJob(QueuedThreadPool.java:1194)
    at wiremock.org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1149)
    at java.base/java.lang.Thread.run(Thread.java:840)

I tried the following:

  1. enclose the value in {{{ }}} - didn't work
  2. tried various escaping - didn't work
  3. tried to skip the processing by enclosing it with {{!-- --}}, but the response was empty string for jsonStringKey1.

Has anyone tried this?


Solution

  • I believe this is an issue specific to WireMock 3.4.0. I tested this with v3.3.1, 3.4.1, and 3.9.1, and all of those returned the response as expected. Only v3.4.0 gives the error you received.

    I searched the issues in WireMock's github and I'm unsure if this issue was explicitly addressed, but it does appear to be resolved in versions of WireMock above and below 3.4.0. This looks like the closest error, but the exception is different. That issue was fixed in 3.4.1, which would align with what I'm seeing.