Using WireMock in integration tests does not pick up the SerializerSettings defined in the StartUp
In the Startup:
.AddNewtonsoftJson(x => x.SerializerSettings.NullValueHandling = NullValueHandling.Ignore)
The integration test custom WebApplicationFactory (which is then used with the Startup of course):
public class IntegrationTestWebApiFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
const string url = "http://localhost:60007";
builder.ConfigureTestServices(
services =>
{
// other stuff
services.AddSingleton(_ => WireMockServer.Start(url)); // hoped it would pick up the json settings... nope!
});
The issue is then that the WireMockServer does not pick up the json settings (null ignore) set in the startup (I see null fields in the wireMockServer.MappingModels, whereas my API does not serialize nulls, as expected from startup settings)
I've found a way around it but I would like to know if it's possible for WireMockServer to pick up the json settings defined in startup
I did this just after the const string url line:
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
// settings must be aligned on the settings defined in startup. Annoying! Is there a better way?
NullValueHandling = NullValueHandling.Ignore
};
Hello @FredCni.
Please note that WireMockServer.Start does not pick up any dependency injection defined like AddNewtonsoftJson.
The only possible solution for you issue is that I update the settings to include a new setting JsonSerializerSettings which you can override as you like.
Hello @FredCni,
Did you have time to reflect on this idea?
Thanks for your reply, much appreciated.
It would be nicer to have this parameter in the init settings, because it would be more explicit than the current way
(JsonConvert.DefaultSettings)
Whether you add it or not, the doc should stress that WireMock serializing settings should be aligned on the serializer settings of the mocked system.
If you don't add it: the doc should point to JsonConvert.DefaultSettings as the "how to"
Can you elaborate what you mean by this parameter in the init settings? Which init-settings you mean?
Oh, I meant please do as you suggested:
... I update the settings to include a new setting JsonSerializerSettings which you can override as you like.
Pls also update the doc as I suggested.
@FredCni Just to clarify : when do you want that your custom JsonSerializerSettings are used?
@FredCni
See this preview version (1.5.3-ci-16370) where you can provide a different JsonConverter when using WithBody:
public IResponseBuilder WithBody(object body, IJsonConverter converter, IJsonConverterOptions? options = null)
For implementations for IJsonConverter, see https://github.com/StefH/JsonConverter.
(how to install preview version: see https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)
Solved with https://github.com/WireMock-Net/WireMock.Net/issues/786
A new NuGet will be released soon.
Was caught with things, so did not take the time to follow up, but you did 👍 Thank you very much. I'll try the new nuget as soon as released.