question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SwaggerToCSharpClientGenerator: JSON output of JsonPatchDocument (HttpPatch) includes the operations property wrapper, causing model binding issues in the API

See original GitHub issue

I’ve had some issues with a JsonPatchDocument serialisation.

The serialization of the JsonPatchDocument from the generated client sends the following:

{
	"operations: [
		 { "op": "add", "path": "/name", "value": "Carly" }
	]
}

However, Web API 2 is expecting:

[
	{ "op": "add", "path": "/name", "value": "Carly" }
]

As a result the request cannot be successfully bound to the controller method parameter.

A hacky fix is to take a copy the PatchAsync method and include it in the partial class/interface. All that method does is that it serializes the object.Operations rather than the object itself. I.e. Changing from this:

var content_ = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(customer, _settings.Value));

to this:

var content_ = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(customer.Operations, _settings.Value));

Is this a bug, or is there some other way to work around this, since this workaround feels rather ugly?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
rebeccapowellcommented, Nov 26, 2018

@G4rce The following works:

public IActionResult PatchCustomer(string customerId, [JsonSchemaType(typeof(List<Operation>))] [FromBody]JsonPatchDocument<CustomerDto> customer)
{
    // do stuff with customer
    var customerDto = new CustomerDto();
    customer.ApplyTo(customerDto);
}
0reactions
G4rcecommented, Sep 25, 2018

@rebeccapowell @RSuter has there been any progress on this issue. Writing our own PatchAsync overload like Rebecca suggested would beat the whole point of code generation. Pls advise, regards.

Read more comments on GitHub >

github_iconTop Results From Across the Web

asp.net core - JsonPatchDocument not binding correctly
I'm finding that patch comes through as NULL , indicating there's an issue with binding. To check there wasn't issues with the request,...
Read more >
JsonPatch in ASP.NET Core web API
Learn how to handle JSON Patch requests in an ASP.NET Core web API. ... The path property of an operation object has slashes...
Read more >
When (not) use JSON Patch in ASP.NET Core. A real-life ...
Let's say we have the business object of the Rectangle which has two properties: length and width. When using PUT operation if you...
Read more >
How to perform partial resource updates with JSON Patch and ...
Wrapping up​​ Use the ASP.NET Core Json Patch library to support partial updates (patches) in your APIs using JSON Patch operations.
Read more >
patch - Command | Vault
The patch command updates data in Vault at the given path (wrapper command for HTTP PATCH using the JSON Patch format). The data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found