refit icon indicating copy to clipboard operation
refit copied to clipboard

[Bug]: CollectionFormat.Csv causes strings to be serialized as comma separated chars

Open RobThree opened this issue 7 months ago • 0 comments

Describe the bug 🐞

When using CollectionFormat.Csv for the CollectionFormat in RefitSettings strings get serialized as comma separated values (e.g. Test becomes T,e,s,t).

Step to reproduce

The below code demonstrates the issue:

using Refit;
using System.Diagnostics;
using System.Net;

var refitsettings = new RefitSettings()
{
    CollectionFormat = CollectionFormat.Csv,
};
var service = RestService.For<ITest>(new HttpClient(new DebugHttpHandler()) { BaseAddress = new Uri("https://localhost") }, refitsettings);

await service.Foo(new TestObject("Test", "12345")).ConfigureAwait(false);

public interface ITest
{
    [Post("/foo")]
    Task<string> Foo([Body(BodySerializationMethod.UrlEncoded)] TestObject request);
}

public record TestObject(string Foo, string Bar);

public class DebugHttpHandler : HttpClientHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        Debug.WriteLine(await request.Content!.ReadAsStringAsync(cancellationToken).ConfigureAwait(false));

        return new HttpResponseMessage(HttpStatusCode.NoContent);
    }
}

Run the program and observe the output in the output window:

Foo=T%2Ce%2Cs%2Ct&Bar=1%2C2%2C3%2C4%2C5

Which, after url-decoding, is:

Foo=T,e,s,t&Bar=1,2,3,4,5

Expected behavior

Only collections should be CSV serialized, not strings as individual chars

IDE

Visual Studio 2022

Refit Version

7.0.0

RobThree avatar Nov 10 '23 13:11 RobThree