aspnet / JsonPatch

[Archived] JSON PATCH library. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
103 stars 48 forks source link

Support for strongly typed dictionarys #96

Closed patrick-hampton-avalara closed 7 years ago

patrick-hampton-avalara commented 7 years ago

I have the following model (simplified for example)

public class FieldResponseModel
{
        public int Id { get; set; }
    public string Name { get; set; }
        public string Value { get; set; }
        public IDictionary<string, TranslationModel> Translations { get; set; }
}
public class TranslationModel
{
    public string Language {get;set;}
    public string Value {get;set;}
}

I want to perform the following add operation to add a new translation to the Dictionary [{ "op":"add", "path":"/fieldResponseModel/tranlsations/es-ES", "value":{ "language":"es-ES", "Value":"some translated value" } }]

However, when calling patch.ApplyTo() I get the following exception:

The value \"{\r\n \"language\": \"es-ES\",\r\n \"Value\": \"some translated value\",\r\n }\" is not of type "TranslationModel" and cannot be used in this generic collection.\r\nParameter name: value.

Am I missing something my operation or is this type of add operation not supported?

jbagga commented 7 years ago

@patrick-hampton-avalara Can you share how you are adding this operation to your patch document?

Are you creating a JsonPatchDocument with a list of operations? If so, can you show how you new up this Operation?

Also, can you try with the dev bits and share if you see the same or a different error? There have been some changes.

patrick-hampton-avalara commented 7 years ago

@jbagga The JsonPatchDocument document is being created by the Asp.Net Core MVC framework We're sending the Json from above in the request body: [{ "op":"add", "path":"/fieldResponseModel/tranlsations/es-ES", "value":{ "language":"es-ES", "Value":"some translated value" } }]

And it's recieved by the Controller with the following signature public async Task<IActionResult> PatchAsync(int entityId, int id, [FromBody]JsonPatchDocument<FieldResponseModel>> patch)

I then retrieve my existing object by it's id, and call patch.ApplyTo to apply the operations: var fieldResponseSet = await _fieldResponseService.GetAsync(entityId, id); patch.ApplyTo(fieldResponseSet, ModelState);

I'll try the latest dev bits in a few days and let you know what I see.

jbagga commented 7 years ago

@patrick-hampton-avalara I did some investigation.

In 2.0.0 and before, we had the DictionaryAdapter which threw the error you see here.

In a recent change, DictionaryAdapterOfTU replaced the old adapter and converts the value to your C# object here. See 0f51c56

So with the latest bits, this scenario should work.

cc @Eilon

patrick-hampton-avalara commented 7 years ago

@jbagga That's great news. I'll try it out in a few days.

Eilon commented 7 years ago

@jbagga can we close this issue then?

jbagga commented 7 years ago

Closing this as it was addressed as part of https://github.com/aspnet/JsonPatch/pull/106.

@patrick-hampton-avalara Let us know if you run into an issue!