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

JsonPatchDocument doesn't respect JsonSerializerOptions.PropertyNamingPolicy #19

Closed
Gigas002 opened this issue Jun 12, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@Gigas002
Copy link

AFAIK, the only way to feed JsonSerializerOptions to JsonPatchDocument is to pass them through constructor. However, when running methods of this object, e.g. Replace, the resulting Path property is written as it was passed, not converting it's string to a needed NamingPolicy:

var serializerOptions = new JsonSerializerOptions
{
    // this requires targeting net8.0
    PropertyNamingPolicy = JsonNamingPolicy.KebabCaseLower
};

var patch = new JsonPatchDocument<User>(new(), serializerOptions);
patch.Replace((v) => v.Name, "Bob");
var patchStr = JsonSerializer.Serialize(patch, options: serializerOptions);
Console.WriteLine($"patch: {patchStr}");

The patch.Operations[0].Paths value is going to be /Name in this example, as a property name, while expected to be /name , as should be forced by JsonSerializerOptions. The patch is applied anyways (due to #10 I guess?), but still would be nice to see appropriate values in requests

I understand, that json values usually don't need to be affected by PropertyNamingPolicy, but I think it should be respected in this case, since Path values are related to property names, when it comes to original json

@Havunen Havunen added the bug Something isn't working label Jun 12, 2023
@Havunen
Copy link
Owner

Havunen commented Jul 16, 2023

Fixed in 2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants