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

Formatting of Extra Property #218

Merged
merged 1 commit into from
Feb 7, 2024
Merged
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
21 changes: 21 additions & 0 deletions Deepgram.Tests/UnitTests/UtilitiesTests/QueryParameterUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ public void GetParameters_Should_Return_String_When_Passing_Array_Parameter()
result.Should().Contain(expected);
}

public void GetParameters_Should_Return_String_When_Passing_Dictonary_Parameter()
{
//Arrange
var prerecordedOptions = new PrerecordedSchema()
{
Extra = new Dictionary<string, string>
{
{"KeyOne","ValueOne" },
{"KeyTwo","ValueTwo" }
}
};
var expected = $"extra={HttpUtility.UrlEncode("KeyOne:ValueOne")}&extra={HttpUtility.UrlEncode("KeyTwo:ValueTwo")}";

//Act
var result = QueryParameterUtil.GetParameters(prerecordedOptions);

//Assert
result.Should().NotBeNull();
result.Should().Contain(expected);
}
Comment on lines +84 to +103
Copy link
Contributor

Choose a reason for hiding this comment

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

The test GetParameters_Should_Return_String_When_Passing_Dictonary_Parameter correctly asserts the functionality for handling dictionary parameters. However, the expected string construction does not account for the order in which dictionary entries might be processed, which could lead to flaky tests if the dictionary iteration order is not consistent. Consider using a more flexible assertion that checks for the presence of each key-value pair in the result, regardless of order.

- var expected = $"extra={HttpUtility.UrlEncode("KeyOne:ValueOne")}&extra={HttpUtility.UrlEncode("KeyTwo:ValueTwo")}";
+ // Assert each key-value pair separately to avoid dependency on dictionary iteration order
+ Assert.IsTrue(result.Contains(HttpUtility.UrlEncode("KeyOne:ValueOne")));
+ Assert.IsTrue(result.Contains(HttpUtility.UrlEncode("KeyTwo:ValueTwo")));

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
public void GetParameters_Should_Return_String_When_Passing_Dictonary_Parameter()
{
//Arrange
var prerecordedOptions = new PrerecordedSchema()
{
Extra = new Dictionary<string, string>
{
{"KeyOne","ValueOne" },
{"KeyTwo","ValueTwo" }
}
};
var expected = $"extra={HttpUtility.UrlEncode("KeyOne:ValueOne")}&extra={HttpUtility.UrlEncode("KeyTwo:ValueTwo")}";
//Act
var result = QueryParameterUtil.GetParameters(prerecordedOptions);
//Assert
result.Should().NotBeNull();
result.Should().Contain(expected);
}
public void GetParameters_Should_Return_String_When_Passing_Dictonary_Parameter()
{
//Arrange
var prerecordedOptions = new PrerecordedSchema()
{
Extra = new Dictionary<string, string>
{
{"KeyOne","ValueOne" },
{"KeyTwo","ValueTwo" }
}
};
// Assert each key-value pair separately to avoid dependency on dictionary iteration order
Assert.IsTrue(result.Contains(HttpUtility.UrlEncode("KeyOne:ValueOne")));
Assert.IsTrue(result.Contains(HttpUtility.UrlEncode("KeyTwo:ValueTwo")));
//Act
var result = QueryParameterUtil.GetParameters(prerecordedOptions);
//Assert
result.Should().NotBeNull();
result.Should().Contain(expected);
}


[Test]
public void GetParameters_Should_Return_String_When_Passing_Decimal_Parameter()
{
Expand Down
15 changes: 13 additions & 2 deletions Deepgram/Utilities/QueryParameterUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections;
using System.Collections;
using Deepgram.Models.PreRecorded.v1;

namespace Deepgram.Utilities;
Expand Down Expand Up @@ -39,7 +39,18 @@ private static string UrlEncode<T>(T parameters, IEnumerable<PropertyInfo> prope
case DateTime time:
sb.Append($"{name}={HttpUtility.UrlEncode(time.ToString("yyyy-MM-dd"))}&");
break;

//specific case for the Extra Parameter dictionary to format the querystring correctly
//no case changing of the key or values as theses are unknowns and the casing may have
//significance to the user
case Dictionary<string, string> dict:
if (name == "extra")
{
foreach (var kvp in dict)
{
sb.Append($"{name}={HttpUtility.UrlEncode($"{kvp.Key}:{kvp.Value}")}&");
}
}
break;
default:
Comment on lines +42 to +53
Copy link
Contributor

Choose a reason for hiding this comment

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

The implementation for handling Dictionary<string, string> in the UrlEncode method correctly preserves the case of keys and values, aligning with the PR objectives. However, consider using Uri.EscapeDataString instead of HttpUtility.UrlEncode for encoding keys and values. Uri.EscapeDataString provides a more RFC 3986 compliant way to encode URI components, which might be more appropriate for query string parameters.

- sb.Append($"{name}={HttpUtility.UrlEncode($"{kvp.Key}:{kvp.Value}")}&");
+ sb.Append($"{name}={Uri.EscapeDataString($"{kvp.Key}:{kvp.Value}")}&");

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
//specific case for the Extra Parameter dictionary to format the querystring correctly
//no case changing of the key or values as theses are unknowns and the casing may have
//significance to the user
case Dictionary<string, string> dict:
if (name == "extra")
{
foreach (var kvp in dict)
{
sb.Append($"{name}={HttpUtility.UrlEncode($"{kvp.Key}:{kvp.Value}")}&");
}
}
break;
//specific case for the Extra Parameter dictionary to format the querystring correctly
//no case changing of the key or values as theses are unknowns and the casing may have
//significance to the user
case Dictionary<string, string> dict:
if (name == "extra")
{
foreach (var kvp in dict)
{
sb.Append($"{name}={Uri.EscapeDataString($"{kvp.Key}:{kvp.Value}")}&");
}
}
break;

sb.Append($"{name}={HttpUtility.UrlEncode(pValue.ToString().ToLower())}&");
break;
Expand Down
Loading