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’ll 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

Closed
RobThree opened this issue Nov 10, 2023 · 1 comment · Fixed by #1752
Closed
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

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant