Search code examples
.netnswag

Set SerializerSettings in NSwag.AspNetCore Version=14.1


I need to set CamelCase configuration for the JsonSerializer used in NSwag, in older version i create an instance of JsonSerializerSettings, set the property ContractResolver and override the SerializerSettings in method AddOpenApiDocument, now i can't find SerializerSettings

This is the code used before:

private void AddSwaggerEndpoints(IServiceCollection services, UnoSwaggerEndpoint[] endpoints)
{
    var nswagDefaultSerializerSettings = new JsonSerializerSettings()
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver(),
    };

    foreach (var endpoint in endpoints.OrderBy(e => e.DocumentName))
    {
        services.AddOpenApiDocument(o =>
        {
            o.DocumentName = endpoint.DocumentName;
            o.Title = endpoint.Title;
            o.ApiGroupNames = endpoint.ApiGroupName;
            o.SerializerSettings = nswagDefaultSerializerSettings;
            o.SchemaSettings.SchemaNameGenerator = new NestedClassNameSchemaNameGenerator();
            o.Version = endpoint.Version;
            o.SchemaSettings.AllowReferencesWithProperties = true;
            o.SchemaSettings.GenerateEnumMappingDescription = true;
            o.DocumentProcessors.Add(new SignalRTypescriptDocumentProcessor(endpoint));
            o.DocumentProcessors.Add(new SchemaSorterDocumentProcessor());
        });
    }
}

Solution

  • AspNetCoreOpenApiDocumentGeneratorSettings does not have a SerializerSettings property. Please look here, how its implemented now.

    https://github.com/RicoSuter/NSwag/blob/6f5e5f9006a34b22d8755428026ff6395d8d8631/src/NSwag.Generation.AspNetCore/AspNetCoreOpenApiDocumentGeneratorSettings.cs

    To handle JSON serialization with camel case in NSwag, you typically configure the serializer in ASP.NET Core's default MvcJsonOptions or JsonOptions rather than directly in NSwag.

    builder.Services.AddControllers()
        .AddNewtonsoftJson(options =>
        {
            options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        });
    

    or

    builder.Services.AddControllers()
        .AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
        });
    

    EDIT 1: After further investigation I found this:

    builder.Services
        .AddOpenApiDocument(options =>
        {
            options.DocumentName = "v2";
            options.Version = "v2";
            options.SchemaSettings = new NewtonsoftJsonSchemaGeneratorSettings
            {
                SchemaType = NJsonSchema.SchemaType.OpenApi3,
                SchemaNameGenerator = new NestedClassNameSchemaNameGenerator(),
                SerializerSettings  = new JsonSerializerSettings()
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                }
            };
    
        });
    

    enter image description here