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

Handle empty array in AddQueryStringParameter. #7309

Merged
merged 2 commits into from
Jan 19, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Volo.Abp.Http.Modeling;
using Volo.Abp.Http.ProxyScripting.Generators;
using Volo.Abp.Localization;
using Volo.Abp.Reflection;

namespace Volo.Abp.Http.Client.DynamicProxying
{
Expand Down Expand Up @@ -82,9 +81,10 @@ private static void AddQueryStringParameters(StringBuilder urlBuilder, IList<Par
continue;
}

AddQueryStringParameter(urlBuilder, isFirstParam, queryStringParameter.Name, value);

isFirstParam = false;
if (AddQueryStringParameter(urlBuilder, isFirstParam, queryStringParameter.Name, value))
{
isFirstParam = false;
}
}

if (apiVersion.ShouldSendInQueryString())
Expand All @@ -93,28 +93,37 @@ private static void AddQueryStringParameters(StringBuilder urlBuilder, IList<Par
}
}

private static void AddQueryStringParameter(
private static bool AddQueryStringParameter(
StringBuilder urlBuilder,
bool isFirstParam,
string name,
[NotNull] object value)
{
urlBuilder.Append(isFirstParam ? "?" : "&");

if (value.GetType().IsArray || (value.GetType().IsGenericType && value is IEnumerable))
{
var index = 0;
foreach (var item in (IEnumerable) value)
{
if (index == 0)
{
urlBuilder.Append(isFirstParam ? "?" : "&");
}
urlBuilder.Append(name + $"[{index++}]=" + System.Net.WebUtility.UrlEncode(ConvertValueToString(item)) + "&");
}
//remove & at the end of the urlBuilder.
urlBuilder.Remove(urlBuilder.Length - 1, 1);
}
else
{
urlBuilder.Append(name + "=" + System.Net.WebUtility.UrlEncode(ConvertValueToString(value)));

if (index > 0)
{
//remove & at the end of the urlBuilder.
urlBuilder.Remove(urlBuilder.Length - 1, 1);
return true;
}

return false;
}

urlBuilder.Append(isFirstParam ? "?" : "&");
urlBuilder.Append(name + "=" + System.Net.WebUtility.UrlEncode(ConvertValueToString(value)));
return true;
}

private static string ConvertValueToString([NotNull] object value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -140,5 +141,13 @@ public class Car

[FromQuery]
public DateTime FirstReleaseDate { get; set; }

[FromQuery]
public List<string> Colors { get; set; }

public Car()
{
Colors = new List<string>();
}
}
}