WireMock.Net icon indicating copy to clipboard operation
WireMock.Net copied to clipboard

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

Open FredCni opened this issue 3 years ago • 6 comments

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
        };

FredCni avatar Jul 27 '22 04:07 FredCni

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 avatar Jul 28 '22 06:07 StefH

Hello @FredCni,

Did you have time to reflect on this idea?

StefH avatar Aug 01 '22 10:08 StefH

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"

FredCni avatar Aug 01 '22 13:08 FredCni

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

StefH avatar Aug 01 '22 16:08 StefH

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 avatar Aug 02 '22 00:08 FredCni

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

StefH avatar Aug 09 '22 20:08 StefH

@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 avatar Aug 15 '22 19:08 StefH

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

A new NuGet will be released soon.

StefH avatar Aug 24 '22 06:08 StefH

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.

FredCni avatar Aug 24 '22 11:08 FredCni