0

My SwaggerUI is showing the wrong parameter for the Delete Endpoint. I'm passing an object to delete but in SwaggerUI the parameter showed is the Id of the object. There is a way to correct this?

Api Method:

[SwaggerResponse(HttpStatusCode.OK)] public IHttpActionResult Delete([FromBody]CargoApiDTO cargoDto)

enter image description here

haldo
  • 14,512
  • 5
  • 46
  • 52
  • what is the [Route] attribute parameter of your Controller? if the Action method does not have any [Route] or [HttpX] verb that overwrites the controller's path, it will use that one. – Radu Ungureanu May 29 '19 at 12:55
  • Post your controller for help. I suspect you are missing http verb attribute. – cl0ud May 29 '19 at 13:01
  • `[SwaggerResponse(HttpStatusCode.OK)] [HttpDelete, Route("{cargoDto}")] public IHttpActionResult Delete([FromBody]CargoApiDTO cargoDto) { ... }` Just added the Http Attribute but it didn't make any change – Matheus Daumas May 29 '19 at 13:16
  • i've read this post [link](https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request/299696#299696) explaining that the HTTP Delete request ignore the request body. I think that is the reason swagger ui is not showing the object but the id (codigo) property, but i'm not sure – Matheus Daumas May 29 '19 at 13:23
  • why are you passing an object to a Delete method? Pass the ID and be done with it – Andrei Dragotoniu May 29 '19 at 14:07
  • what @AndreiDragotoniu wants to say, and I agree with him, is that the REST Specification indicates that the DELETE operations should not receive a body. `DELETE APIs are used to delete resources (identified by the Request-URI)`, so it would be a better design of your API to send the id as part of the URL – Radu Ungureanu May 29 '19 at 14:16

1 Answers1

0

I did think whether I should post this as the answer as it doesn't directly answer the question.

This being said, I think you're going about this the wrong way. A DELETE request obviously passes a delete request to an endpoint. As such it doesn't need the whole object.

Think what you need to delete something, all you need is the unique identifier of the resource you want to delete. I guess Swagger is trying to point you in the right direction.

Even if you pass the whole object, in the end you'll only extract the ID from your entire DTO, which makes it pointless to transfer potentially a lot of data over the wire for literally nothing.

So, change your endpoint, only have the id as a parameter and Swagger won't be confused any more.

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32