Swashbuckle.aspnetcore: ProducesResponseType with ModelStateDictionary

Created on 19 Apr 2017  路  3Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

I'm trying to use multiple ProducesResponseTypeAttribute on my action, but if I use built in types like ModelStateDictionary the swagger page doesn't contain this class' model. If I use custom type it works like charm.

Not working:
```C#
[HttpGet("{id}")]
[ProducesResponseType(typeof(Product), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(ModelStateDictionary), (int)HttpStatusCode.BadRequest)]
public IActionResult Get(int id)
{
return Ok(_mapper.Map(_productService.GetProduct(id)));
}

![image](https://cloud.githubusercontent.com/assets/8333960/25176288/f4c23bb6-24fd-11e7-8744-fe3916e9a7d3.png)


**Working but I don't want this:**
```C#
[HttpGet("{id}")]
[ProducesResponseType(typeof(Product), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(Order), (int)HttpStatusCode.BadRequest)]
public IActionResult Get(int id)
{
    return Ok(_mapper.Map<Product>(_productService.GetProduct(id)));
}

image

All 3 comments

ping

Okay I dig into this a little bit and I found that the asp.net core pipeline does not use ModelStateDictionary as a return value for BadRequest rather than a SerializableError class. This is formally a Dictionary<string, object> but technically a Dictionary<string, string[]> see.

So we need to use [ProduceResponseType(typeof(Dictionary<string, string[]>)] attribute to achive the same functionality as the MVC pipeline works. However after this the swagger ui still cannot visualize the response's model, because the Dictionary<string, whatever> is a simple json object with unspecified property names. Besides that in the swagger schema is correct so the client side code generation should work.
image

Could we find out some way to visualize this model in the swagger UI?

Essentially a dup of #337

Was this page helpful?
0 / 5 - 0 ratings