RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.61k stars 1.22k forks source link

Make it possible to override CreateSerializerSettings #3673

Open peterbozso opened 2 years ago

peterbozso commented 2 years ago

Hi,

I am using NSwag in a Blazor WebAssembly application to generate the client classes to it's backend. To play nicely with ASP.NET Core MVC's default settings, I'd like to use new JsonSerializerOptions(JsonSerializerDefaults.Web) in my NSwag generated client classes' CreateSerializerSettings method.

Currently, as a workaround, I generate my client classes inheriting from this base class:

public class ApiClientBase
{
    protected void UpdateJsonSerializerSettings(JsonSerializerOptions settings)
    {
        var webSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);

        settings.PropertyNameCaseInsensitive = webSerializerOptions.PropertyNameCaseInsensitive;
        settings.PropertyNamingPolicy = webSerializerOptions.PropertyNamingPolicy;
        settings.NumberHandling = webSerializerOptions.NumberHandling;
    }
}

The three settings I change above are coming from this documentation. This of course works, but it's easy to see that a new version of ASP.NET Core can break this code quietly by changing the settings for the JsonSerializerDefaults.Web configuration.

I think a more robust approach to this would be an option in NSwag's code generator that would make it possible to provide my own CreateSerializerSettings method in a base class, just like I do currently with UpdateJsonSerializerSettings above. Then I could just write:

public class ApiClientBase
{
    protected JsonSerializerOptions CreateSerializerSettings() =>
        new JsonSerializerOptions(JsonSerializerDefaults.Web);
}

And I can rest assured that as long as the ASP.NET Core team doesn't decide to use something other than new JsonSerializerOptions(JsonSerializerDefaults.Web) by default, then my NSwag generated client classes will be rock solid.

If this idea makes sense to people other than me, and somebody could point me at some docs where to start with the implementation, then I am more than happy to help with a pull request!

Or maybe there's a setting for this already in the code generator which I simply missed by accident. I'd be the happiest with this option. :)

Thanks, Péter

AlbertoSadoc commented 1 year ago

Hi, any update on this? It would be great if this was added for the Newtonsoft JsonSerializerSettings as well, the auto-generated client just create a new instance and passes it to the UpdateJsonSerializerSettings

        private Newtonsoft.Json.JsonSerializerSettings CreateSerializerSettings()
        {
            var settings = new Newtonsoft.Json.JsonSerializerSettings();
            UpdateJsonSerializerSettings(settings);
            return settings;
        }

        protected Newtonsoft.Json.JsonSerializerSettings JsonSerializerSettings { get { return _settings.Value; } }

        partial void UpdateJsonSerializerSettings(Newtonsoft.Json.JsonSerializerSettings settings);
JarFrank commented 4 months ago

it is possible, I wrote solution to override serialization this is a link to comment