Skip to content

Commit

Permalink
Add json serializer settings fallback, #1961
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Aug 22, 2019
1 parent 43e4934 commit 44ead05
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/NSwag.AspNetCore/JsonExceptionFilterAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override void OnActionExecuted(ActionExecutedContext context)
if (context.Exception != null && (_exceptionTypes.Count == 0 || _exceptionTypes.Exists(t => t.IsInstanceOfType(context.Exception))))
{
var settings = AspNetCoreOpenApiDocumentGenerator.GetJsonSerializerSettings(context.HttpContext?.RequestServices);
settings = settings != null ? CopySettings(settings) : new JsonSerializerSettings();
settings = settings != null ? CopySettings(settings) : (JsonConvert.DefaultSettings?.Invoke() ?? new JsonSerializerSettings());
settings.Converters.Add(new JsonExceptionConverter(_hideStackTrace, _searchedNamespaces));

var json = JsonConvert.SerializeObject(context.Exception, settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public async Task<OpenApiDocument> GenerateAsync(object serviceProvider)
var typedServiceProvider = (IServiceProvider)serviceProvider;

var mvcOptions = typedServiceProvider.GetRequiredService<IOptions<MvcOptions>>();
var settings = GetJsonSerializerSettings(typedServiceProvider);
var settings = GetJsonSerializerSettings(typedServiceProvider) ??
Settings?.ActualSerializerSettings ??
JsonConvert.DefaultSettings?.Invoke();

Settings.ApplySettings(settings, mvcOptions.Value);

Expand All @@ -69,11 +71,10 @@ public static JsonSerializerSettings GetJsonSerializerSettings(IServiceProvider
// Try load ASP.NET Core 3 options
var optionsAssembly = Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Mvc.NewtonsoftJson"));
var optionsType = typeof(IOptions<>).MakeGenericType(optionsAssembly.GetType("Microsoft.AspNetCore.Mvc.MvcNewtonsoftJsonOptions", true));
options = serviceProvider?.GetRequiredService(optionsType) as dynamic;
options = serviceProvider?.GetService(optionsType) as dynamic;
}

var settings = (JsonSerializerSettings)options?.Value?.SerializerSettings ?? JsonConvert.DefaultSettings?.Invoke();
return settings;
return (JsonSerializerSettings)options?.Value?.SerializerSettings;
}

/// <summary>Generates a Swagger specification for the given <see cref="ApiDescriptionGroupCollection"/>.</summary>
Expand Down

0 comments on commit 44ead05

Please sign in to comment.