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 9e93d76 commit 0bfb665
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 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
12 changes: 5 additions & 7 deletions src/Framework/Framework/Routing/UrlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ public static string BuildUrlSuffix(string? urlSuffix, object? query)
private static string AppendQueryParam(ref string urlSuffix, string name, string? value)
{
urlSuffix += (urlSuffix.LastIndexOf('?') < 0 ? "?" : "&");
var hasValue = !string.IsNullOrWhiteSpace(value);

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

/// <summary>
Expand Down Expand Up @@ -137,16 +136,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 0bfb665

Please sign in to comment.