Handle null JsonElement in GetSectionAsJson to return null if section is missing.

This commit is contained in:
Sipke Schoorstra 2026-02-23 14:45:15 +01:00
parent 856f7b40b4
commit 2250e25e5c
No known key found for this signature in database
GPG key ID: 5C10502B28A4268F

View file

@ -6,11 +6,11 @@ namespace Elsa.Extensions;
public static class ConfigurationExtensions
{
public static string GetSectionAsJson(this IConfiguration configuration, string sectionKey, JsonSerializerOptions? options = null)
public static string? GetSectionAsJson(this IConfiguration configuration, string sectionKey, JsonSerializerOptions? options = null)
{
var section = configuration.GetSection(sectionKey);
var element = ConvertToJsonElement(section);
return JsonSerializer.Serialize(element, options);
return element.ValueKind == JsonValueKind.Null ? null : JsonSerializer.Serialize(element, options);
}
private static JsonElement ConvertToJsonElement(IConfigurationSection section)