diff --git a/Directory.Build.props b/Directory.Build.props index f606c36a80..e78e0bf6c1 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 14.0.6 + 14.0.7 Rico Suter Copyright © Rico Suter, 2023 diff --git a/src/NSwag.AspNetCore/Extensions/NSwagServiceCollectionExtensions.cs b/src/NSwag.AspNetCore/Extensions/NSwagServiceCollectionExtensions.cs index 8a1e1484d7..f827cff744 100644 --- a/src/NSwag.AspNetCore/Extensions/NSwagServiceCollectionExtensions.cs +++ b/src/NSwag.AspNetCore/Extensions/NSwagServiceCollectionExtensions.cs @@ -69,23 +69,25 @@ public static IServiceCollection AddSwaggerDocument(this IServiceCollection serv var settings = new AspNetCoreOpenApiDocumentGeneratorSettings(); var mvcOptions = services.GetRequiredService>(); + var hasSystemTextJsonOutputFormatter = mvcOptions.Value.OutputFormatters + .Any(f => f.GetType().Name == "SystemTextJsonOutputFormatter"); + var newtonsoftSettings = AspNetCoreOpenApiDocumentGenerator.GetJsonSerializerSettings(services); - var systemTextJsonOptions = mvcOptions.Value.OutputFormatters - .Any(f => f.GetType().Name == "SystemTextJsonOutputFormatter") + var systemTextJsonOptions = hasSystemTextJsonOutputFormatter ? AspNetCoreOpenApiDocumentGenerator.GetSystemTextJsonSettings(services) #if NET6_0_OR_GREATER - : services.GetService>()?.Value.SerializerOptions; + : services.GetService>()?.Value.SerializerOptions; #else : null; #endif - if (systemTextJsonOptions != null) - { - settings.ApplySettings(new SystemTextJsonSchemaGeneratorSettings { SerializerOptions = systemTextJsonOptions }, mvcOptions.Value); - } - else if (newtonsoftSettings != null) + if (newtonsoftSettings != null && !hasSystemTextJsonOutputFormatter) { settings.ApplySettings(new NewtonsoftJsonSchemaGeneratorSettings { SerializerSettings = newtonsoftSettings }, mvcOptions.Value); + } + else if (systemTextJsonOptions != null) + { + settings.ApplySettings(new SystemTextJsonSchemaGeneratorSettings { SerializerOptions = systemTextJsonOptions }, mvcOptions.Value); } else {