From e9c83802d0bb68149a7551b5137081bce5b2a648 Mon Sep 17 00:00:00 2001 From: Timothy Makkison Date: Mon, 1 Jul 2024 15:24:28 +0100 Subject: [PATCH] feat: fix form url encoded strings #1593 --- Refit.Tests/RequestBuilder.cs | 23 +++++++++++++++++++++++ Refit/FormValueMultimap.cs | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Refit.Tests/RequestBuilder.cs b/Refit.Tests/RequestBuilder.cs index 5b8f300da..f0477c5aa 100644 --- a/Refit.Tests/RequestBuilder.cs +++ b/Refit.Tests/RequestBuilder.cs @@ -3241,6 +3241,29 @@ public void BodyContentGetsUrlEncoded() Assert.Equal("Foo=Something&Bar=100&Baz=", output.SendContent); } + [Fact] + public void BodyContentGetsUrlEncodedWithCollectionFormat() + { + var settings = new RefitSettings() { CollectionFormat = CollectionFormat.Csv }; + var fixture = new RequestBuilderImplementation(settings); + var factory = fixture.RunRequest("PostSomeUrlEncodedStuff"); + var output = factory( + new object[] + { + 6, + new + { + Foo = "Something", + Bar = 100, + FooBar = new [] {5,7}, + Baz = "" // explicitly use blank to preserve value that would be stripped if null + } + } + ); + + Assert.Equal("Foo=Something&Bar=100&FooBar=5%2C7&Baz=", output.SendContent); + } + [Fact] public void FormFieldGetsAliased() { diff --git a/Refit/FormValueMultimap.cs b/Refit/FormValueMultimap.cs index f82114006..291fb3567 100644 --- a/Refit/FormValueMultimap.cs +++ b/Refit/FormValueMultimap.cs @@ -65,7 +65,8 @@ public FormValueMultimap(object source, RefitSettings settings) // see if there's a query attribute var attrib = property.GetCustomAttribute(true); - if (value is not IEnumerable enumerable) + // add strings/non enumerable properties + if (value is not IEnumerable enumerable || value is string) { Add( fieldName,