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

System.Text.Json serialization settings not picked up if MVC not in use #4670

Closed
mdsharpe opened this issue Jan 3, 2024 · 5 comments
Closed

Comments

@mdsharpe
Copy link
Contributor

mdsharpe commented Jan 3, 2024

I have a project that is just using Minimal APIs (no MVC). The APIs use camel case serialization but NSwag is outputting pascal case.

I have had to add the following in order for System.Text.Json settings to be used by NSwag:

builder.Services.Configure<Microsoft.AspNetCore.Mvc.MvcOptions>(options =>
{
    options.OutputFormatters.Add(
        new Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter(
            System.Text.Json.JsonSerializerOptions.Default));
});

The relevant area of NSwag code is NSwagServiceCollectionExtensions.AddSwaggerDocument, where it calls services.GetRequiredService<IOptions<MvcOptions>>().

It'd be great if NSwag could use the existing default serializer options, rather than instantiating a new SystemTextJsonSchemaGeneratorSettings which in turn uses new JsonSerializerOptions() which doesn't specify camel case.

I appreciate STJ support may be a work-in-progress at the moment!

@cliedeman
Copy link

Encountered the same issue and can confirm the workaround works.

Thanks @mdsharpe

@schnerring
Copy link

You can also do this:

builder
    .Services
    .ConfigureHttpJsonOptions(options =>
    {
        options.SerializerOptions.PropertyNamingPolicy = null;
    });

By default, ASP.NET uses the JsonSerializerDefaults.Web defaults:

PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString

@JarFrank
Copy link

You can customize and override the generated client. My client, generated by NSwag, is named 'Client'. To modify its behavior, I simply need to create an additional partial class and define a method within it. This method will then be recognized and utilized during the loading of the JSON settings

public partial class Client
{
    static partial void UpdateJsonSerializerSettings(System.Text.Json.JsonSerializerOptions settings)
    {
        settings.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
    }
}

@RicoSuter
Copy link
Owner

Is this still an issue with v14.0.7?
I think this PR might have fixed it:
#4733

@egresspav
Copy link

For me v14.0.7 works again as expected . Previous working version was v14.0.3. Thanks @RicoSuter
I generate openApi.json from ASP.NET Core project with AddNewtonsoftJson using "$(NSwagExe_Net80) run nswag.json.

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

No branches or pull requests

6 participants