Skip to content

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

Closed
@RobThree

Description

@RobThree

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions