From 2e2ce44aa20997bba7eaf950b721e9a0dc6be744 Mon Sep 17 00:00:00 2001 From: Simon Oxtoby Date: Sat, 24 Dec 2022 13:13:13 +1000 Subject: [PATCH] Trimming quotes from single enum args in request URLs Resolves #135 --- SlackNet.Tests/SlackUrlBuilderTests.cs | 7 +++++++ SlackNet/SlackUrlBuilder.cs | 19 +++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/SlackNet.Tests/SlackUrlBuilderTests.cs b/SlackNet.Tests/SlackUrlBuilderTests.cs index 9dfab5e..bda412e 100644 --- a/SlackNet.Tests/SlackUrlBuilderTests.cs +++ b/SlackNet.Tests/SlackUrlBuilderTests.cs @@ -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() { diff --git a/SlackNet/SlackUrlBuilder.cs b/SlackNet/SlackUrlBuilder.cs index 02d814c..d20a533 100644 --- a/SlackNet/SlackUrlBuilder.cs +++ b/SlackNet/SlackUrlBuilder.cs @@ -29,14 +29,17 @@ private IEnumerable 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() - .Select(o => JsonConvert.SerializeObject(o, _jsonSettings.SerializerSettings)) - .Select(val => val.Trim('"'))); + string.Join(",", enumerable.Cast().Select(SerializeObject)); + + private string SerializeObject(object value) => + JsonConvert.SerializeObject(value, _jsonSettings.SerializerSettings).Trim('"'); } \ No newline at end of file