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

Fixed building query parameters in URL suffixes #1502

Merged
merged 3 commits into from
Nov 2, 2022
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
16 changes: 12 additions & 4 deletions src/Framework/Framework/Routing/UrlHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using DotVVM.Framework.Utils;
Expand All @@ -24,19 +25,19 @@ public static string BuildUrlSuffix(string? urlSuffix, object? query)
case null:
break;
case IEnumerable<KeyValuePair<string, string>> keyValueCollection:
foreach (var item in keyValueCollection)
foreach (var item in keyValueCollection.Where(i => i.Value != null))
{
AppendQueryParam(ref resultSuffix, item.Key, item.Value);
}
break;
case IEnumerable<KeyValuePair<string, object>> keyValueCollection:
foreach (var item in keyValueCollection)
foreach (var item in keyValueCollection.Where(i => i.Value != null))
{
AppendQueryParam(ref resultSuffix, item.Key, item.Value.ToString().NotNull());
}
break;
default:
foreach (var prop in query.GetType().GetProperties())
foreach (var prop in query.GetType().GetProperties().Where(p => p.GetValue(query) != null))
{
AppendQueryParam(ref resultSuffix, prop.Name, prop.GetValue(query)!.ToString().NotNull());
}
Expand All @@ -47,7 +48,14 @@ public static string BuildUrlSuffix(string? urlSuffix, object? query)
}

private static string AppendQueryParam(ref string urlSuffix, string name, string value)
=> urlSuffix += (urlSuffix.LastIndexOf('?') < 0 ? "?" : "&") + $"{Uri.EscapeDataString(name)}={Uri.EscapeDataString(value)}";
{
urlSuffix += (urlSuffix.LastIndexOf('?') < 0 ? "?" : "&");
var hasValue = value.Trim() != string.Empty;

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

/// <summary>
/// Checks whether the URL is local.
Expand Down
46 changes: 46 additions & 0 deletions src/Tests/Routing/UrlHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,51 @@ public void UrlHelper_IsLocalUrl(string url, bool expectedResult)
Assert.AreEqual(expectedResult, result);
}

[TestMethod]
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>("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);
}

[TestMethod]
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>("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);
}

[TestMethod]
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);
}

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