aspnet / JsonPatch

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

Multiple actions matched #25

Closed zygimantas closed 8 years ago

zygimantas commented 8 years ago

Is it possible to have multiple controller actions which has a different signature?

[HttpPatch("items/{id}")]
public IActionResult PatchAsync(string id, [FromBody]JsonPatchDocument<Document1> document1)
{
}

[HttpPatch("items/{id}")]
public IActionResult PatchAsync(string id, [FromBody]JsonPatchDocument<Document2> document2)
{
}

I am getting the "Microsoft.AspNetCore.Mvc.Internal.AmbiguousActionException: Multiple actions matched." exception. Should I use a custom constraint in such case or JsonPatch has something built-in for such scenario?

dougbu commented 8 years ago

@zygimantas might be simpler to change your two route templates so they aren't identical e.g. [HttpPatch("items1/{id}")] and [HttpPatch("items2/{id}")].

zygimantas commented 8 years ago

Well, in my situation I have the endpoint /items/{id} to the document

{
    "a": {
        "b": []
        "c": 1
    }
}

and the first controller action

[HttpPatch("items/{id}")]
public IActionResult PatchAsync(string id, [FromBody]JsonPatchDocument<Document1> document1)
{
}

[{
    "op": "replace",
    "path": "/a",
    "value": {"b": [], "c": 2}
}]

would allow me to replace the "a" property completely, while the second one

[HttpPatch("items/{id}")]
public IActionResult PatchAsync(string id, [FromBody]JsonPatchDocument<Document2> document2)
{
}

[{
    "op": "add",
    "path": "/a/b/-",
    "value": 1
}]

would allow to add new thing into "b" array.

I have asked because I don't see it how a "shared" patch document can be created (because of different paths).

zygimantas commented 8 years ago

The only option I can think of right now is to have an action method

[HttpPatch("items/{id}/a")]
public IActionResult PatchAsync(string id, [FromBody]JsonPatchDocument<Document2> document2)
{
}

[{
    "op": "add",
    "path": "/b/-",
    "value": 1
}]

but that does not look nice personally, to move "path" from the patch document to the route configuration, because it will expand the surface of the API and will require 2 endpoints:

  1. items/{id}
  2. items/{id}/a

I would suggest to have a built-in "path" constraint, if that is possible.

zygimantas commented 8 years ago

My issue "almost" makes no sense. It is possible to create such "shared" patch document. I am closing this issue.