Swashbuckle.aspnetcore: BadRequest, ModelState and response model

Created on 11 Mar 2017  路  7Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

If I want to return model state errors they are transformed to an array of

[ { code: "code", description: "description" } ]

``` C#
return BadRequest(ModelState);

But `ModelState` is just a dictionary.

The closest type which describes this response is
``` C#
[ProducesResponseType(typeof(IdentityError[]), (int)HttpStatusCode.BadRequest)]

But it looks ugly in Swagger because of type name IdentityError.

What can you suggest for this case?

Most helpful comment

ModelStateDictionary is ultimately serialized to Dictionary<string, string[]> and so that's what should be passed to ProducesResponseType. On model validation, the keys and values of this are populated at _runtime_ by the framework. So, there's absolutely no way for Swashbuckle to describe anything more specific because it generates it's descriptions from static code. In the latest version of the UI (see SB 2.1.0), the generic dictionary description is now visualized correctly.

All 7 comments

What type are using to serialize into the error response?

I don't aware of any such type in ASP.NET Core. Errors from model are converted into SerializableError, see this line

Since I (think I) know what the original poster means and I suffer the same problem I allow myself to hijack this issue:

The default way (in .NET Core 1.* & 2.*) is to handle bad model states as in the code below:

[HttpGet(Name = "DoSomething")] [ProducesResponseType(typeof(SomeResponseModel), (int)HttpStatuscode.OK] [ProducesResponseType(typeof(???), (int)HttpStatusCode.BadRequest] [SwaggerOperation("DoSomething")] public IActionResult DoSomething([FromBody] SomeRequestModel request) { if (!ModelState.IsValid) { return BadRequest(ModelState); } //do something return new SomeResponseModel(); }

(sorry, can't get the code to be displayed pretty)

in short: one just uses return BadRequest(ModelState);
which then will be converted into a "dictionary" (like IDictionary) which is displayed in json as given below:

{ "field1": "is null", "field2": "is not a integer", "field3[0]": "whatever reason why this is not valid", "field3[1]": "is null" }

The problem is
[ProducesResponseType(typeof(???), (int)HttpStatusCode.BadRequest]

whatever type one uses in the ProducesResponseType (replace ??? with a model class you like) there is never an Model displayed in the ui that even comes close to the json/model above.

So what type could one use to get a correct representation of this model in the UI?

Hi Guys,
I came across this issue as well.
Are we going to be able to add ModelStateDictionary soon?
This would be a great addition.

I tried With [SwaggerResponse(400, typeof(ModelStateDictionary))] But got Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'API.Models.ModelStateEntry'

ModelStateDictionary is ultimately serialized to Dictionary<string, string[]> and so that's what should be passed to ProducesResponseType. On model validation, the keys and values of this are populated at _runtime_ by the framework. So, there's absolutely no way for Swashbuckle to describe anything more specific because it generates it's descriptions from static code. In the latest version of the UI (see SB 2.1.0), the generic dictionary description is now visualized correctly.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

flipchart picture flipchart  路  4Comments

m-demydiuk picture m-demydiuk  路  3Comments

mrmartan picture mrmartan  路  3Comments

govin picture govin  路  3Comments

voroninp picture voroninp  路  3Comments