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

Empty array query parameter bug fix #21

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 16 additions & 12 deletions src/Atc.Rest.Client/Builder/MessageRequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,29 +140,33 @@ public IMessageRequestBuilder WithQueryParameter(string name, object? value)
throw new ArgumentException($"'{nameof(name)}' cannot be null or whitespace", nameof(name));
}

if (value is null)
if (value is not null)
{
return this;
queryMapper[name] = value.ToString();
}

var valueType = value.GetType();
if (valueType.IsArray || valueType.IsGenericType)
return this;
}

public IMessageRequestBuilder WithQueryParameter(string name, IList? value)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException($"'{nameof(name)}' cannot be null or whitespace", nameof(name));
}

if (value is { Count: not 0 })
{
var objects = ((IEnumerable)value).Cast<object>().ToArray();
var sb = new StringBuilder();
for (int i = 0; i < objects.Length; i++)
for (var i = 0; i < value.Count; i++)
{
sb.Append(i == 0
? Uri.EscapeDataString(objects[i].ToString())
: $"&{name}={Uri.EscapeDataString(objects[i].ToString())}");
? Uri.EscapeDataString(value[i].ToString())
: $"&{name}={Uri.EscapeDataString(value[i].ToString())}");
}

queryMapper["#" + name] = sb.ToString();
}
else
{
queryMapper[name] = value.ToString();
}

return this;
}
Expand Down
58 changes: 58 additions & 0 deletions test/Atc.Rest.Client.Tests/Builder/MessageRequestBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Numerics;
using System.Threading.Tasks;
using System.Web;
using Atc.Rest.Client.Builder;
using Atc.Rest.Client.Serialization;
using Atc.Test;
Expand Down Expand Up @@ -163,6 +165,62 @@ public void Should_Replace_Query_Parameters_WithNull(
.Be($"/api?foo={fooValue}&bar={barValue}");
}

[Theory]
[InlineAutoNSubstituteData("/api")]
public void Should_Omit_Query_Parameters_With_DateTime_List(
string template,
IEnumerable<Vector2> values)
{
var sut = CreateSut(template);

sut.WithQueryParameter("foo", values);
var message = sut.Build(HttpMethod.Get);

message!
.RequestUri!
.ToString()
.Should()
.BeEquivalentTo($"/api?foo={HttpUtility.UrlEncode(values.ToString())}");
}

[Theory]
[InlineAutoNSubstituteData("/api")]
public void Should_Omit_Query_Parameters_With_Empty_List(
string template)
{
var sut = CreateSut(template);

var values = new List<string>();

sut.WithQueryParameter("foo", values);
var message = sut.Build(HttpMethod.Get);

message!
.RequestUri!
.ToString()
.Should()
.Be($"/api");
}

[Theory]
[InlineAutoNSubstituteData("/api")]
public void Should_Omit_Query_Parameters_With_Empty_Array(
string template)
{
var sut = CreateSut(template);

var values = Array.Empty<string>();

sut.WithQueryParameter("foo", values);
var message = sut.Build(HttpMethod.Get);

message!
.RequestUri!
.ToString()
.Should()
.Be($"/api");
}

[Theory]
[InlineAutoNSubstituteData("/api")]
public void Should_Replace_Query_Parameters_With_ArrayOfItems1(
Expand Down