Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow strict mode while parsing JSON content type #579

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,15 @@ public AzureAppConfigurationOptions ConfigureStartupOptions(Action<StartupOption
return this;
}

/// <summary>
/// By default, the provider will throw a <see cref="FormatException"/> if a key-value with JSON content type has an invalid JSON value. This method silences the behavior and interprets the value as a string instead.
/// </summary>
public AzureAppConfigurationOptions DisableStrictJsonParsing()
{
JsonKeyValueAdapter.ThrowOnInvalidJson = false;
return this;
}

private static ConfigurationClientOptions GetDefaultClientOptions()
{
var clientOptions = new ConfigurationClientOptions(ConfigurationClientOptions.ServiceVersion.V2023_10_01);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace Microsoft.Extensions.Configuration.AzureAppConfiguration
{
internal class JsonKeyValueAdapter : IKeyValueAdapter
{
public static bool ThrowOnInvalidJson { get; set; } = true;

private static readonly IEnumerable<string> ExcludedJsonContentTypes = new[]
{
FeatureManagementConstants.ContentType,
Expand All @@ -42,7 +44,13 @@ public Task<IEnumerable<KeyValuePair<string, string>>> ProcessKeyValue(Configura
}
catch (JsonException)
{
// If the value is not a valid JSON, treat it like regular string value
if (ThrowOnInvalidJson)
{
throw new FormatException($"Invalid JSON value for key '{setting.Key}' with content type '{setting.ContentType}'. Please ensure the value is properly formatted JSON.");
}

logger.LogWarning($"Invalid JSON value for key '{setting.Key}' with content type '{setting.ContentType}'. Treated as a string value.");

return Task.FromResult<IEnumerable<KeyValuePair<string, string>>>(new[] { new KeyValuePair<string, string>(setting.Key, setting.Value) });
}

Expand Down
43 changes: 43 additions & 0 deletions tests/Tests.AzureAppConfiguration/JsonContentTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,49 @@ public void JsonContentTypeTests_LoadSettingsWithInvalidJsonContentTypeAsString(
Assert.Equal("{}", config["TestKey7"]);
}

[Fact]
public void JsonContentTypeTests_StrictJsonParsingEnabled()
{
List<ConfigurationSetting> _kvCollection = new List<ConfigurationSetting>
{
ConfigurationModelFactory.ConfigurationSetting(
key: "TestKey1",
value: "}{",
contentType: "application/json")
};
var mockClientManager = GetMockConfigurationClientManager(_kvCollection);

var builder = new ConfigurationBuilder()
.AddAzureAppConfiguration(options =>
{
options.ClientManager = mockClientManager;
});

Assert.Throws<FormatException>(() => builder.Build());
}

[Fact]
public void JsonContentTypeTests_StrictJsonParsingDisabled()
{
List<ConfigurationSetting> _kvCollection = new List<ConfigurationSetting>
{
ConfigurationModelFactory.ConfigurationSetting(
key: "TestKey1",
value: "}{",
contentType: "application/json")
};
var mockClientManager = GetMockConfigurationClientManager(_kvCollection);

var builder = new ConfigurationBuilder()
.AddAzureAppConfiguration(options =>
{
options.ClientManager = mockClientManager;
options.DisableStrictJsonParsing();
});

Assert.NotNull(builder.Build());
}

[Fact]
public void JsonContentTypeTests_OverwriteValuesForDuplicateKeysAfterFlatteningJson()
{
Expand Down
Loading