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]: Refit's settings with contentSerializer and urlParameterKeyFormatter parameters, when using at the same time doesn't work #1794

Open
navernoe opened this issue Aug 28, 2024 · 0 comments
Labels

Comments

@navernoe
Copy link

Describe the bug 🐞

I was need to send post request with body, serialized with some json options, so when I was adding refit-client I've added this RefitSettings in it:

        var jsonOptions = new JsonSerializerOptions()
        {
            Converters = { new JsonStringEnumMemberConverter() },
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase
        };
        var refitSettings = new RefitSettings(
              new SystemTextJsonContentSerializer(jsonOptions)
            );
        services
            .AddRefitClient<IMyClient>(refitSettings)
            .ConfigureHttpClient(c =>
            {
                c.BaseAddress = new Uri("http://myclienturl");
            });

And all was good, request via refit-client worked as it was expected.

But a few time later, there was a need to add settings for url parameter's keys, then I've updated refit settings, adding urlParameterKeyFormatter to it like this:

        var refitSettings = new RefitSettings(
              new SystemTextJsonContentSerializer(jsonOptions),
            urlParameterKeyFormatter: new CamelCaseUrlParameterKeyFormatter());

And formatting for url parameter's keys is working now as well, BUT - the first setting for content serialization was broken, and it doesn't work when urlParameterKeyFormatter is added to refit settings.
I checked it once again - when I remove urlParameterKeyFormatter setting, then setting for content serialization starts to work, and its stops to work when I turn back urlParameterKeyFormatter setting.

Step to reproduce

  1. Create refit-client with settings
 var jsonOptions = new JsonSerializerOptions()
        {
            Converters = { new JsonStringEnumMemberConverter() },
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase
        };
        var refitSettings = new RefitSettings(
              new SystemTextJsonContentSerializer(jsonOptions),
              urlParameterKeyFormatter: new CamelCaseUrlParameterKeyFormatter()
            );
        services
            .AddRefitClient<IMyClient>(refitSettings)
            .ConfigureHttpClient(c =>
            {
                c.BaseAddress = new Uri("http://myclienturl");
            });  

public interface IMyClient
{
    [Post("/Actions/Test")]
    Task<TestResponse> TestMethod(TestRequest request);
}

public record TestRequest 
{
    public string TestRequestProp { get; set; }
}

public record TestResponse
{
    public string TestResponseProp { get; set; }
}

2 Send POST request to client, that receives body's content only in lowerCamelCase casing.
4. See that client will return error

Reproduction repository

https://github.com/reactiveui/refit

Expected behavior

This should happen...
The next Refit's settings should work for both - url parameter's keys and content serialization

        var jsonOptions = new JsonSerializerOptions()
        {
            Converters = { new JsonStringEnumMemberConverter() },
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase
        };
        var refitSettings = new RefitSettings(
              new SystemTextJsonContentSerializer(jsonOptions),
            urlParameterKeyFormatter: new CamelCaseUrlParameterKeyFormatter());
            
        services
            .AddRefitClient<IMyClient>(refitSettings)
            .ConfigureHttpClient(c =>
            {
                c.BaseAddress = new Uri("http://myclienturl");
            });

Screenshots 🖼️

No response

IDE

Rider Windows

Operating system

No response

Version

No response

Device

No response

Refit Version

7.1.2

Additional information ℹ️

No response

@navernoe navernoe added the bug label Aug 28, 2024
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