Skip to content

Commit

Permalink
Fix building of query string parameters in UrlHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Dec 15, 2023
1 parent 13f3cf4 commit 5ee1620
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Framework/Framework/Routing/DotvvmRoute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected override string BuildUrlCore(Dictionary<string, object?> values)
var convertedValues =
values.ToDictionary(
v => v.Key,
v => UrlHelper.ParameterToString(v.Value),
v => UrlHelper.ParameterToString(v.Value) is string x ? Uri.EscapeDataString(x) : null,
StringComparer.OrdinalIgnoreCase
);
try
Expand Down
9 changes: 4 additions & 5 deletions src/Framework/Framework/Routing/UrlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private static string AppendQueryParam(ref string urlSuffix, string name, string

return (!hasValue) ?
urlSuffix += Uri.EscapeDataString(name) :
urlSuffix += $"{Uri.EscapeDataString(name)}={value}";
urlSuffix += $"{Uri.EscapeDataString(name)}={Uri.EscapeDataString(value)}";

Check failure on line 58 in src/Framework/Framework/Routing/UrlHelper.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Debug)

Possible null reference argument for parameter 'stringToEscape' in 'string Uri.EscapeDataString(string stringToEscape)'.

Check failure on line 58 in src/Framework/Framework/Routing/UrlHelper.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Debug)

Possible null reference argument for parameter 'stringToEscape' in 'string Uri.EscapeDataString(string stringToEscape)'.

Check failure on line 58 in src/Framework/Framework/Routing/UrlHelper.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Release)

Possible null reference argument for parameter 'stringToEscape' in 'string Uri.EscapeDataString(string stringToEscape)'.

Check failure on line 58 in src/Framework/Framework/Routing/UrlHelper.cs

View workflow job for this annotation

GitHub Actions / Build published projects without warnings (Release)

Possible null reference argument for parameter 'stringToEscape' in 'string Uri.EscapeDataString(string stringToEscape)'.
}

/// <summary>
Expand Down Expand Up @@ -137,16 +137,15 @@ private static bool ContainsOnlyValidUrlChars(string url)
}
else if (ReflectionUtils.TryGetCustomPrimitiveTypeRegistration(value.GetType()) is { } registration)
{
return Uri.EscapeDataString(registration.ToStringMethod(value));
return registration.ToStringMethod(value);
}
else if (value is IConvertible convertible)
{
return Uri.EscapeDataString(convertible.ToString(CultureInfo.InvariantCulture));
return convertible.ToString(CultureInfo.InvariantCulture);
}
else
{
var strVal = value.ToString();
return strVal == null ? null : Uri.EscapeDataString(strVal);
return value.ToString();
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Tests/Routing/UrlHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public void UrlHelper_BuildUrlSuffix_EnumerableStringString()
var suffix = "suffix";
var query = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("key1", "value1"),
new KeyValuePair<string, string>("key1", "value1+x"),
new KeyValuePair<string, string>("key2", null!),
new KeyValuePair<string, string>("key3", string.Empty),
new KeyValuePair<string, string>("key4", "value4")
};
var result = UrlHelper.BuildUrlSuffix(suffix, query);
Assert.AreEqual("suffix?key1=value1&key3&key4=value4", result);
Assert.AreEqual("suffix?key1=value1%2Bx&key3&key4=value4", result);
}

[TestMethod]
Expand All @@ -52,13 +52,13 @@ public void UrlHelper_BuildUrlSuffix_EnumerableStringObject()
var suffix = "suffix";
var query = new List<KeyValuePair<string, object>>()
{
new KeyValuePair<string, object>("key1", "value1"),
new KeyValuePair<string, object>("key1", "value1+x"),
new KeyValuePair<string, object>("key2", null!),
new KeyValuePair<string, object>("key3", string.Empty),
new KeyValuePair<string, object>("key4", "value4")
};
var result = UrlHelper.BuildUrlSuffix(suffix, query);
Assert.AreEqual("suffix?key1=value1&key3&key4=value4", result);
Assert.AreEqual("suffix?key1=value1%2Bx&key3&key4=value4", result);
}

[TestMethod]
Expand All @@ -67,12 +67,12 @@ public void UrlHelper_BuildUrlSuffix_Object()
var suffix = "suffix";
var query = new TestUrlSuffixDescriptor();
var result = UrlHelper.BuildUrlSuffix(suffix, query);
Assert.AreEqual("suffix?key1=value1&key3&key4=value4", result);
Assert.AreEqual("suffix?key1=value1%2Bx&key3&key4=value4", result);
}

private class TestUrlSuffixDescriptor
{
public string key1 { get; set; } = "value1";
public string key1 { get; set; } = "value1+x";
public object key2 { get; set; } = null;
public object key3 { get; set; } = string.Empty;
public string key4 { get; set; } = "value4";
Expand Down

0 comments on commit 5ee1620

Please sign in to comment.