forked from Azure/azure-functions-dapr-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed property casing issue (Azure#154)
* Fixed property casing issue Signed-off-by: MD Ashique <[email protected]> * Update src/Microsoft.Azure.WebJobs.Extensions.Dapr/DaprExtensionConfigProvider.cs Co-authored-by: Shubham Sharma <[email protected]> * Update src/Microsoft.Azure.WebJobs.Extensions.Dapr/Utils/DictionaryUtils.cs Co-authored-by: Shubham Sharma <[email protected]> * renaming variable Signed-off-by: MD Ashique <[email protected]> --------- Signed-off-by: MD Ashique <[email protected]> Co-authored-by: Shubham Sharma <[email protected]>
- Loading branch information
1 parent
f8975dc
commit 35d6f1a
Showing
5 changed files
with
167 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/Microsoft.Azure.WebJobs.Extensions.Dapr/Utils/DictionaryUtils.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
// ------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.WebJobs.Extensions.Dapr.Utils | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
|
||
/// <summary> | ||
/// Dictionary Utils. | ||
/// </summary> | ||
public static class DictionaryUtils | ||
{ | ||
/// <summary> | ||
/// Convert a JsonElement to a dictionary. | ||
/// </summary> | ||
/// <param name="element">JsonElement.</param> | ||
/// <returns>Dictionary.</returns> | ||
public static Dictionary<string, JsonElement> ToCaseInsensitiveDictionary(this JsonElement element) | ||
{ | ||
var propertyBag = new Dictionary<string, JsonElement>(StringComparer.InvariantCultureIgnoreCase); | ||
|
||
if (element.ValueKind == JsonValueKind.Null || element.ValueKind == JsonValueKind.Undefined) | ||
{ | ||
return propertyBag; | ||
} | ||
|
||
foreach (var prop in element.EnumerateObject()) | ||
{ | ||
if (element.TryGetProperty(prop.Name, out JsonElement value) && value.ValueKind != JsonValueKind.Null) | ||
{ | ||
propertyBag[prop.Name] = value; | ||
} | ||
} | ||
|
||
return propertyBag; | ||
} | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
test/DaprExtensionTests/UnitTests/Utils/DictionaryUtilsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
namespace DaprExtensionTests.UnitTests.Utils | ||
{ | ||
using System.Text.Json; | ||
using Microsoft.Azure.WebJobs.Extensions.Dapr.Utils; | ||
using Xunit; | ||
|
||
public class DictionaryUtilsTests | ||
{ | ||
[Fact] | ||
public void ToCaseInsensitiveDictionary_PositiveCases() | ||
{ | ||
// Arrange | ||
var json = @"{ | ||
""Name"": ""John"", | ||
""Age"": 30, | ||
""City"": ""New York"" | ||
}"; | ||
|
||
var element = JsonDocument.Parse(json).RootElement; | ||
|
||
// Act | ||
var dictionary = element.ToCaseInsensitiveDictionary(); | ||
|
||
// Assert | ||
Assert.Equal(3, dictionary.Count); | ||
Assert.True(dictionary.ContainsKey("name")); | ||
Assert.True(dictionary.ContainsKey("age")); | ||
Assert.True(dictionary.ContainsKey("city")); | ||
Assert.Equal("John", dictionary["name"].GetString()); | ||
Assert.Equal(30, dictionary["age"].GetInt32()); | ||
Assert.Equal("New York", dictionary["city"].GetString()); | ||
} | ||
|
||
[Fact] | ||
public void ToCaseInsensitiveDictionary_EmptyElement() | ||
{ | ||
// Arrange | ||
var json = @"{}"; | ||
|
||
var element = JsonDocument.Parse(json).RootElement; | ||
|
||
// Act | ||
var dictionary = element.ToCaseInsensitiveDictionary(); | ||
|
||
// Assert | ||
Assert.Empty(dictionary); | ||
} | ||
|
||
[Fact] | ||
public void ToCaseInsensitiveDictionary_NullValueProperties() | ||
{ | ||
// Arrange | ||
var json = @"{ | ||
""Name"": null, | ||
""Age"": null | ||
}"; | ||
|
||
var element = JsonDocument.Parse(json).RootElement; | ||
|
||
// Act | ||
var dictionary = element.ToCaseInsensitiveDictionary(); | ||
|
||
// Assert | ||
Assert.Empty(dictionary); | ||
} | ||
|
||
[Fact] | ||
public void ToCaseInsensitiveDictionary_NullElement() | ||
{ | ||
// Arrange | ||
JsonElement element = default; | ||
|
||
// Act | ||
var dictionary = element.ToCaseInsensitiveDictionary(); | ||
|
||
// Assert | ||
Assert.Empty(dictionary); | ||
} | ||
|
||
[Fact] | ||
public void ToCaseInsensitiveDictionary_DuplicateProperties() | ||
{ | ||
// Arrange | ||
var json = @"{ | ||
""Name"": ""John"", | ||
""name"": ""Doe"", | ||
""Age"": 30 | ||
}"; | ||
|
||
var element = JsonDocument.Parse(json).RootElement; | ||
|
||
// Act | ||
var dictionary = element.ToCaseInsensitiveDictionary(); | ||
|
||
// Assert | ||
Assert.Equal(2, dictionary.Count); | ||
Assert.True(dictionary.ContainsKey("name")); | ||
Assert.True(dictionary.ContainsKey("age")); | ||
Assert.Equal("Doe", dictionary["name"].GetString()); // The last occurrence should overwrite the previous one | ||
Assert.Equal(30, dictionary["age"].GetInt32()); | ||
} | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...ts/Services/EnvironmentExtensionsTests.cs → ...Tests/Utils/EnvironmentExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters