Implementing PATCH with Delta <T> using JSON Formatter

I installed the OData library, but only to access Delta to use PATCH for WebAPI. This is kind of work, but not for types like decimal and int. I understand that this is due to the JSON Formatter format, not Delta, but is there a workaround or fix (or workaround) that can be applied to this to make it work.

I know that Delta was created to work with OData (and formatting), but without something like Delta using PATCH without Delta, it becomes difficult when you allow partial updating of a resource and do not limit the fields that can be passed.

Is there an alternative?

A related question is to have the same problem, int / decimal, etc. do not update

Delta <T> in PATCH actions not tracking primitive types

UPDATED INCLUDE SOME CODE FOR CONTEXT

The POST / PATCH / PUT types will be simplified for specific DTOs so that I can reduce the noise of objects, but for now I use Model objects directly, as I was just trying to get it working.

PATCH Route

public HttpResponseMessage Patch(int id, Delta<Measurement> measurement) { var resp = new HttpResponseMessage(HttpStatusCode.OK); var dbMeasurement = (from n in _repo.Include("SurveyItem") where n.Id == id select n).SingleOrDefault(); measurement.Patch(dbMeasurement); _uow.Commit(); var measurementRep = dbMeasurement.ToRepresentation<Measurement, MeasurementRepresentation>(); resp.Content = new ObjectContent<MeasurementRepresentation>(measurementRep, new JsonHalMediaTypeFormatter()); return resp; } 

FIDDLER FUNCTIONAL PARAMETERS

Headings

 User-Agent: Fiddler Host: localhost:1996 Content-Length: 25 Content-Type: application/json 

Body

 { "Value":101.00 } 

200 is returned, but nothing is updated. But more important is the following: the value is 0:

Delta

I can save strings, just no other types.

+4
source share
2 answers

Since commenting is a little out of control, I begin this answer.

I tried my code and see some strange things:

First of all, you do not use overriding the Patch method. Without this, the request should not appear, because it cannot be resolved.

Secondly, assuming that your β€œValue” field is of type decimal, I cannot send a decimal number like you. That is, in JSON with only a value of 101.00. This is what I also noticed in my application, and my decision is to send it as a string, encapsulating it in quotation marks ("Value": "101.00"). It is still parsed as a decimal so, and I guess the same thing applies to doubles and floats.

Finally, to really exhaust all the options, can you include this line of code at the beginning of your patch method?

 if (!ModelState.IsValid) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } 

Thus, you will receive the correct BadRequest 400 answer with the answer to what is wrong.

However, if all of the above is included, I can still correct non-line. Visual Studio is already starting to numb about my first point, unless you override the method that mentions hidden methods. Hope one of these questions solves your problem. If not, I will update my answer, hopefully more solutions.

+2
source

Is there an alternative?

Yes, using the Asp.Net JsonPatchDocument (using Microsoft.AspNetCore.JsonPatch)

Here's how to use it.

0
source

Source: https://habr.com/ru/post/1497421/


All Articles