public void ApplyTo_JsonPatchDocument_ModelState() { // Arrange var operation = new Operation<Customer>("add", "Customer/CustomerId", from: null, value: "TestName"); var patchDoc = new JsonPatchDocument<Customer>(); patchDoc.Operations.Add(operation); var modelState = new ModelStateDictionary(); // Act patchDoc.ApplyTo(new Customer(), modelState); // Assert var error = Assert.Single(modelState["Customer"].Errors); Assert.Equal("The property at path 'Customer/CustomerId' could not be added.", error.ErrorMessage); }
public void ApplyTo_JsonPatchDocument_PrefixModelState() { // Arrange var operation = new Operation<Customer>("add", "Customer/CustomerId", from: null, value: "TestName"); var patchDoc = new JsonPatchDocument<Customer>(); patchDoc.Operations.Add(operation); var modelState = new ModelStateDictionary(); // Act patchDoc.ApplyTo(new Customer(), modelState, "jsonpatch"); // Assert var error = Assert.Single(modelState["jsonpatch.Customer"].Errors); Assert.Equal("Property does not exist at path 'Customer/CustomerId'.", error.ErrorMessage); }
public async Task<JsonResult> Patch(string claimNumber, [FromBody]MitchellClaimType claimToModify) { try { if (ModelState.IsValid) { var client = ClaimHttpClient.GetClient(); JsonPatchDocument<MitchellClaimType> patchDoc = new JsonPatchDocument<MitchellClaimType>(); patchDoc.Replace(pt => pt.AssignedAdjusterID, claimToModify.AssignedAdjusterID); patchDoc.Replace(pt => pt.Vehicles.First().ExteriorColor, claimToModify.Vehicles.First().ExteriorColor); var serializedClaimToModify = JsonConvert.SerializeObject(patchDoc); var response = await client.PatchAsync("api/claims/" + claimNumber, new StringContent( serializedClaimToModify, System.Text.Encoding.Unicode, "application/json")); if(response.IsSuccessStatusCode) { Response.StatusCode = (int)HttpStatusCode.OK; return Json(claimToModify); } } } catch (Exception ex) { _logger.LogError("error", ex); } Response.StatusCode = (int)HttpStatusCode.BadRequest; return Json(new { Message = "Failed", ModelState = ModelState }); }