Skip to content

[Bug]: JsonPropertyName attribute is ignored (does not rename properties as expected) #1541

Closed

Description

@7amou3

Describe the bug 🐞

Hello
the [JsonPropertyName("mycustomePropName")] doesn't seems to work, the serializer always output the property name,
for example: in the DummyRequest class below, the url is:

http://localhost/myendpoint/json?All=True&Limit=10
instead of
http://localhost/myendpoint/json?all=True&otherText=10

the [AliasAs("othertext")] works fine, but I don't want to use it
Any Idea?
Thanks

Step to reproduce

create a new .Net 7 project and paste the following code in program.cs

using System.ComponentModel;
using System.Text.Json.Serialization;
using Refit;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

// Configure Refit
builder.Services
               .AddRefitClient<IGithubApi>()
               .ConfigureHttpClient(c =>
               {
                   c.BaseAddress = new Uri("http://localhost/");
               });

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

// Test client
var api = app.Services.GetService<IGithubApi>();
var res = api.GetContainers(new DummyRequest() { All = true, Limit = 10 });

app.Run();


public class DummyRequest
{
    [JsonPropertyName("all")]
    public bool All { get; set; } = true;

    
    [JsonPropertyName("otherText")]
    public int Limit { get; set; }
}

interface IGithubApi
{
    [Get("/myendpoint/json")]
    Task<Container> GetContainers(DummyRequest request);
}

Reproduction repository

https://github.com/reactiveui/refit

Expected behavior

Serialized properties that have the [JsonPropertyName("myPropName")] should be renamed accordingly

Screenshots 🖼️

bellow the sent request
https://i.imgur.com/hJkiH3a.png

IDE

Visual Studio 2022

Operating system

Windows

Version

11

Device

No response

Refit Version

7.0.0

Additional information ℹ️

No response

Activity

  1. 7amou3 commented on Jul 23, 2023

    @7amou3
    Author

    Hello, any help?

  2. sergeivalko commented on Jul 26, 2023

    @sergeivalko

    @7amou3 it's not a bug, it's by design, i've prepared pr, but i think it's odd to use JsonPropertyName for query params, do you have any reasons why you do not want to use AliasAs

  3. 7amou3 commented on Jul 26, 2023

    @7amou3
    Author

    I don't want to use AliasAs Because I share a Nuget package that host the models, and I don't want the nuget to have a dependancy on refit

  4. damidhagor commented on Jul 30, 2023

    @damidhagor

    I'm running into the same issue where I want to remove Refit from my core package and reuse the models between my library which I want to support trimming and AOT and a child library which adds a Refit client separately.
    Supporting the JsonPropertyNameAttribute (or an alternative BCL included attribute) as a replacement for the AliasAsAttribute for query parameters would be helpful to not have my shared models depend on Refit references.

  5. sergeivalko commented on Jul 31, 2023

    @sergeivalko

    @7amou3 @damidhagor oh i get it, i forgot about that case, PR still on review so we have to wait 😄

  6. 7amou3 commented on Aug 13, 2023

    @7amou3
    Author

    @clairernovotny can you check and merge this fix please?

  7. tcortega commented on Sep 27, 2023

    @tcortega
    Contributor

    The issue arises because query strings aren't JSON and thus aren't processed in the same way as JSON serialization. While JSON serialization has its own conventions and rules, query strings follow a different set of standards and encoding mechanisms. The two are distinct and require different handling approaches.

  8. Thepriestdude commented on May 29, 2024

    @Thepriestdude

    Any updates on this?

  9. tcortega commented on May 29, 2024

    @tcortega
    Contributor

    Any updates on this?

    You have to use the AliasAs attribute. Sadly the refit team has not reviewed my PR.
    https://github.com/reactiveui/refit?tab=readme-ov-file#api-attributes

    GitHub
    The automatic type-safe REST library for .NET Core, Xamarin and .NET. Heavily inspired by Square's Retrofit library, Refit turns your REST API into a live interface. - reactiveui/refit
  10. Thepriestdude commented on May 30, 2024

    @Thepriestdude

    @tcortega, Question: Would it be possible to also add in support for using the formatters for bodies since they also ignore the [JsonPropertyName] attribute? Having one interface for both would cut down on the clutter.

    Edit: There was an error with my code, please ignore

  11. tcortega commented on Jun 4, 2024

    @tcortega
    Contributor

    @ChrisPulman this should be closed

  12. github-actions commented on Jul 5, 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.

  13. locked as resolved and limited conversation to collaborators on Jul 5, 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