Havunen / SystemTextJsonPatch

SystemTextJsonPatch is a JSON Patch (JsonPatchDocument) RFC 6902 implementation for .NET using System.Text.Json
MIT License
102 stars 12 forks source link

It seems it doesnt work with lists #23

Closed mhosman closed 1 year ago

mhosman commented 1 year ago

If I'm trying to update an entire collection inside an object:

[ { "op": "replace", "path": "mylist", "value": [ { "languageId": "es", "name": "albert" } ] } ]

Model:

public class MyModel
{
    public int Id { get; set; }
    public List<Whatever> myList { get; set; }
}

I get:

"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'SystemTextJsonPatch.JsonPatchDocument`1[MyApi.Models.v1.MyModel]' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\nPath '', line 1, position 1."

This is working fine using Microsoft Json Patch.

Havunen commented 1 year ago

Hi, thanks for reporting this issue. It sounds like a bug

Havunen commented 1 year ago

I added a test case for this, but it is working as expected. Are you having JsonSerializerOptions.PropertyNameCaseInsensitive value as false it is the default if not set: https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions.propertynamecaseinsensitive?view=net-7.0 ?

In your provided sample the value in path is writted in lower case. If you are using camel case property naming it should be myList instead of mylist ?

Havunen commented 1 year ago

Can you provide the actual JSON payload and actual models please?

mhosman commented 1 year ago

Nevermind, is working fine. I just forgot to remove Newtonsoft from controller actions. Thanks guys! Great library!