Hi Team, I have set up a custom transformer to fil...
# help
j
Hi Team, I have set up a custom transformer to filter a json file. I can get the resolved request url and the path template, but how can I resolve the path params and their values?
Copy code
wireMockServer.stubFor(
        WireMock.get(WireMock.urlPathTemplate("/organisations/v1/register/organisations/{id}"))
            .willReturn(
                WireMock.aResponse()
                    .withTransformers("filter-single-organisation")
//                    .withTransformerParameter("id", "{{ request.path.id }}")
            ))
Copy code
class SingleOrganisationTransformer : ResponseDefinitionTransformerV2 {
    override fun transform(serveEvent: ServeEvent): ResponseDefinition {
        println(serveEvent.request.url)
        println(serveEvent.stubMapping.request.urlPathTemplate)

        val orgId = serveEvent.transformerParameters.getString("id") ?: 0
        val organisation = jacksonObjectMapper().writeValueAsString(WireMockAdminStubs.ORGANISATIONS_LIST.find { it.id == orgId })
        return ResponseDefinitionBuilder()
            .withHeader("Content-Type", "application/json")
            .withBody(organisation)
            .build()
    }

    override fun getName(): String {
        return "filter-single-organisation"
    }
}
t
I think what you’re looking for is something like this:
Copy code
PathTemplate pathTemplate =
          serveEvent.getStubMapping().getRequest().getUrlMatcher().getPathTemplate();
PathParams requestPathParams = pathTemplate.parse(Urls.getPath(request.getUrl()));
The
PathParams
object contains the variable values.
j
Thank you 🙌 Worked like a charm :-)
👍 1