Using WireMock in integration tests does not pick up the SerializerSettings defined in the StartUp #428

Closed
opened 2025-12-29 08:28:02 +01:00 by adam · 9 comments
Owner

Originally created by @ghost on GitHub (Jul 27, 2022).

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
        };
Originally created by @ghost on GitHub (Jul 27, 2022). In the Startup: ``` c# .AddNewtonsoftJson(x => x.SerializerSettings.NullValueHandling = NullValueHandling.Ignore) ``` The integration test custom WebApplicationFactory (which is then used with the Startup of course): ``` c# 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: ``` c# JsonConvert.DefaultSettings = () => new JsonSerializerSettings { // settings must be aligned on the settings defined in startup. Annoying! Is there a better way? NullValueHandling = NullValueHandling.Ignore }; ```
adam added the question label 2025-12-29 08:28:02 +01:00
adam closed this issue 2025-12-29 08:28:02 +01:00
Author
Owner

@StefH commented on GitHub (Jul 28, 2022):

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.

@StefH commented on GitHub (Jul 28, 2022): 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.
Author
Owner

@StefH commented on GitHub (Aug 1, 2022):

Hello @FredCni,

Did you have time to reflect on this idea?

@StefH commented on GitHub (Aug 1, 2022): Hello @FredCni, Did you have time to reflect on this idea?
Author
Owner

@ghost commented on GitHub (Aug 1, 2022):

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"

@ghost commented on GitHub (Aug 1, 2022): 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"
Author
Owner

@StefH commented on GitHub (Aug 1, 2022):

Can you elaborate what you mean by this parameter in the init settings? Which init-settings you mean?

@StefH commented on GitHub (Aug 1, 2022): Can you elaborate what you mean by `this parameter in the init settings`? Which **init-settings** you mean?
Author
Owner

@ghost commented on GitHub (Aug 2, 2022):

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.

@ghost commented on GitHub (Aug 2, 2022): 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.
Author
Owner

@StefH commented on GitHub (Aug 9, 2022):

@FredCni
Just to clarify : when do you want that your custom JsonSerializerSettings are used?

@StefH commented on GitHub (Aug 9, 2022): @FredCni Just to clarify : when do you want that your custom JsonSerializerSettings are used?
Author
Owner

@StefH commented on GitHub (Aug 15, 2022):

@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)

@StefH commented on GitHub (Aug 15, 2022): @FredCni See this preview version (`1.5.3-ci-16370`) where you can provide a different JsonConverter when using `WithBody`: ``` c# 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)
Author
Owner

@StefH commented on GitHub (Aug 24, 2022):

Solved with
https://github.com/WireMock-Net/WireMock.Net/issues/786

A new NuGet will be released soon.

@StefH commented on GitHub (Aug 24, 2022): Solved with https://github.com/WireMock-Net/WireMock.Net/issues/786 A new NuGet will be released soon.
Author
Owner

@ghost commented on GitHub (Aug 24, 2022):

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.

@ghost commented on GitHub (Aug 24, 2022): 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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#428