Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
RobThree opened this issue Nov 10, 2023 · 0 comments
Labels

Comments

@RobThree
Copy link

RobThree commented Nov 10, 2023

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 RobThree added the bug label Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant