The object is not primitive for response message models

I decorated the action as follows

[SwaggerResponse(HttpStatusCode.OK, "List of customers", typeof(List<CustomerDto>))] [SwaggerResponse(HttpStatusCode.NotFound, Type = typeof(NotFoundException))] 

The OK model is shown correctly.

However, in Repsonse posts, I get “Object Not Primitive” for NotFound . A custom exception comes from Exception , implements ISerializable , and also has [Serializable] and [DataContract()]

How can I show the actual data type instead of a message?

Also, if I use WebApi normally, if I decorate all actions with such attributes?

+5
source share
1 answer

How about something like:

  [SwaggerResponse(HttpStatusCode.OK, "List of customers", typeof(IEnumerable<int>))] [SwaggerResponse(HttpStatusCode.BadRequest, Type = typeof(BadRequestErrorMessageResult))] [SwaggerResponse(HttpStatusCode.NotFound, Type = typeof(NotFoundResult))] public IHttpActionResult GetById(int id) { if (id > 0) return Ok(new int[] { 1, 2 }); else if (id == 0) return NotFound(); else return BadRequest("id must be greater than 0"); } 

http://swashbuckletest.azurewebsites.net/swagger/ui/index#!/IHttpActionResult/IHttpActionResult_GetById

-1
source

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


All Articles