Skip to content

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

Closed

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

Activity

  1. github-actions commented on Jul 16, 2024

    @github-actions

    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.

  2. locked as resolved and limited conversation to collaborators on Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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