Skip to content

Commit

Permalink
Trimming quotes from single enum args in request URLs
Browse files Browse the repository at this point in the history
Resolves #135
  • Loading branch information
soxtoby committed Dec 24, 2022
1 parent 5cf0af8 commit 2e2ce44
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
7 changes: 7 additions & 0 deletions SlackNet.Tests/SlackUrlBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public void StringListArgs_CommaSeparatedAndEscaped()
.ShouldBe(BaseUrl + "method?foo=%3Fbar%2Cbaz");
}

[Test]
public void EnumArg_NoQuotes()
{
_sut.Url("method", new Args { { "foo", SortDirection.Descending } })
.ShouldBe(BaseUrl + "method?foo=desc");
}

[Test]
public void EnumListArgs_CommaSeparatedEscaped()
{
Expand Down
19 changes: 11 additions & 8 deletions SlackNet/SlackUrlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ private IEnumerable<string> ArgValues(Args args) =>
args.Where(a => a.Value != null)
.Select(a => $"{a.Key}={Uri.EscapeDataString(ArgValue(a.Value))}");

private string ArgValue(object value) =>
value is string stringValue ? stringValue
: value is IDictionary dictionary ? JsonConvert.SerializeObject(dictionary, _jsonSettings.SerializerSettings)
: value is IEnumerable enumerable ? SerializeEnumerable(enumerable)
: JsonConvert.SerializeObject(value, _jsonSettings.SerializerSettings);
private string ArgValue(object value) => value switch
{
string stringValue => stringValue,
IDictionary dictionary => SerializeObject(dictionary),
IEnumerable enumerable => SerializeEnumerable(enumerable),
_ => SerializeObject(value)
};

private string SerializeEnumerable(IEnumerable enumerable) =>
string.Join(",", enumerable.Cast<object>()
.Select(o => JsonConvert.SerializeObject(o, _jsonSettings.SerializerSettings))
.Select(val => val.Trim('"')));
string.Join(",", enumerable.Cast<object>().Select(SerializeObject));

private string SerializeObject(object value) =>
JsonConvert.SerializeObject(value, _jsonSettings.SerializerSettings).Trim('"');
}

0 comments on commit 2e2ce44

Please sign in to comment.