Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.0.0] Custom ResponseTemplateTransformer gets overwritten by default implementation #2349

Closed
manofthepeace opened this issue Aug 31, 2023 · 6 comments
Labels

Comments

@manofthepeace
Copy link

Proposal

In Wiremock 2.35 I have the following external file;

file.json

[
    {
        "id": "theID",
        "time": {{now offset='2 hours' format='epoch'}}
    }
]

with the following code;

        WireMockConfiguration config = new WireMockConfiguration().extensions(new ResponseTemplateTransformer(true))
                .port(9090).fileSource(new ClasspathFileSource("src/test/resources/jsonfiles/"));
        wireMockServer = new WireMockServer(config);

in 3.0 the code change to this;

        WireMockConfiguration config = new WireMockConfiguration();
        FileSource fs = new ClasspathFileSource("src/test/resources/jsonfiles/");
        config = config.port(9090).fileSource(fs)
                .extensions(new ResponseTemplateTransformer(TemplateEngine.defaultTemplateEngine(), true, fs,
                        Collections.emptyList()));
        wireMockServer = new WireMockServer(config);

in the json returned by wiremock, the {{now offset='2 hours' format='epoch'}} is not replaced thus leading to wrong json format + data. I must say I also find it odd api wise to pass the FileSource to both the config and the ResponseTemplateEngine.

Reproduction steps

have wiremock returning a classpathfilesource like the above and see the template not being replaced.

References

Relates to; wiremock/wiremock.org#157

@manofthepeace
Copy link
Author

A bit more notes, the "config" object above does contain the right true value for global, but when adding a breakpoint in
ResponseTemplateTransformer::applyGlobally the value is false

@manofthepeace manofthepeace changed the title [3.0.0] ResponseTemplateTransformer does not seem to be working with ClasspathFileSource [3.0.0] Custom ResponseTemplateTransformer gets overwritten by default implementation Sep 1, 2023
@achimgrimm
Copy link

Hi @manofthepeace,
not sure if it helps with your problem, but if you only want to activate the global templating, then you can use an option now to do that.

        FileSource fs = new ClasspathFileSource("src/test/resources/jsonfiles/");
        WireMockConfiguration config = new WireMockConfiguration().port(9090)
                                                                  .fileSource(fs)
                                                                  .globalTemplating(true);
        wireMockServer = new WireMockServer(config);

@achimgrimm
Copy link

I also added wiremock/wiremock.org#164 to update the documentation.

@tomakehurst
Copy link
Member

This is a documentation issue - being addressed shortly.

Local templating is enabled by default in WireMock 3+ (you can disable it with wireMockConfig().templatingEnabled(false)) and per @achimgrimm's comment above you can set it global with .globalTemplating().

This will do all the heavy lifting regarding construction of the transformer.

@manofthepeace
Copy link
Author

I still do not believe this is a only doc issue. globalTempating is one thing, but please see; #2350 (comment)

@tomakehurst
Copy link
Member

I've pushed a further doc fix that should address your other concern:
https://wiremock.org/docs/response-templating/#customising-and-extending-the-template-engine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

Successfully merging a pull request may close this issue.

4 participants