From b3615b4c4fc23e4b2dc69b8d7d6a420c4931e141 Mon Sep 17 00:00:00 2001 From: Rico Suter Date: Tue, 26 Mar 2024 16:15:41 +0100 Subject: [PATCH] Fix loading of correct JSON serializer options, fixes #4834, regression of #4733, v14.0.7 --- Directory.Build.props | 2 +- .../NSwagServiceCollectionExtensions.cs | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) 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 {