diff --git a/src/NSwag.Generation.AspNetCore/AspNetCoreOpenApiDocumentGenerator.cs b/src/NSwag.Generation.AspNetCore/AspNetCoreOpenApiDocumentGenerator.cs index 94cb4b5d3c..257e3b982d 100644 --- a/src/NSwag.Generation.AspNetCore/AspNetCoreOpenApiDocumentGenerator.cs +++ b/src/NSwag.Generation.AspNetCore/AspNetCoreOpenApiDocumentGenerator.cs @@ -81,12 +81,20 @@ public static JsonSerializerSettings GetJsonSerializerSettings(IServiceProvider } catch { - // Newtonsoft.JSON not available, see GetSystemTextJsonSettings() + // Newtonsoft.JSON not available, use GetSystemTextJsonSettings() return null; } } - return (JsonSerializerSettings)options?.Value?.SerializerSettings; + try + { + return (JsonSerializerSettings)options?.Value?.SerializerSettings; + } + catch + { + // Newtonsoft.JSON not available, use GetSystemTextJsonSettings() + return null; + } } /// Generates a Swagger specification for the given . @@ -140,7 +148,7 @@ public static JsonSerializerSettings GetSystemTextJsonSettings(IServiceProvider { var optionsAssembly = Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Mvc.Core")); var optionsType = typeof(IOptions<>).MakeGenericType(optionsAssembly.GetType("Microsoft.AspNetCore.Mvc.JsonOptions", true)); - + var options = serviceProvider?.GetService(optionsType) as dynamic; var jsonOptions = (object)options?.Value?.JsonSerializerOptions; if (jsonOptions != null && jsonOptions.GetType().FullName == "System.Text.Json.JsonSerializerOptions")