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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -444,6 +444,15 @@ public AzureAppConfigurationOptions ConfigureStartupOptions(Action<StartupOption
return this;
}

/// <summary>
/// Configure the provider behavior when parsing invalid JSON values. When strict JSON parsing is enabled, the provider will throw <see cref="FormatException"/>, if a key-value with JSON content type has invalid JSON value.
/// </summary>
public AzureAppConfigurationOptions EnableStrictJsonParsing()
{
JsonKeyValueAdapter.ThrowOnInvalidJson = true;
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; } = false;

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.");
Copy link
Member

@amerjusupovic amerjusupovic Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can mention the new API in this warning log? If we do change the default though, it would probably be mentioned in the exception instead.


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

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

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

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

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

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