Skip to content

Commit

Permalink
Fix System.Text.Json options fallback, #2856
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Jan 18, 2021
1 parent 2d620e5 commit d71fc5e
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/// <summary>Generates a Swagger specification for the given <see cref="ApiDescriptionGroupCollection"/>.</summary>
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit d71fc5e

Please sign in to comment.