Swashbuckle output different from demo
See original GitHub issueDear Kristian,
I’ve noticed that in the demo at https://problem-details-demo.azurewebsites.net/swagger/index.html#/ApiMvc/ApiMvc_ErrorDetails the error has a description:
However, if I create a new ASP.Net 5.0 Web API, then I don’t get that description:
Everything else is the same as you can see. I’ve looked to the example source code, but couldn’t figure out what causes this. Do you have any idea?
The steps to reproduce:
Create a new Web API in Visual Studio 2019, version 16.10.0 Preview 1.0
Add services.AddProblemDetails(ConfigureProblemDetails);
to Startup.ConfigureServices(...)
Add the following method to Startup:
private void ConfigureProblemDetails(ProblemDetailsOptions o)
{
o.ValidationProblemStatusCode = StatusCodes.Status400BadRequest;
}
Add app.UseProblemDetails();
as first line to Startup.Configure(...)
Change the Get method in the WeaterForecastController to
[HttpGet]
[ProducesResponseType(StatusCodes.Status422UnprocessableEntity)]
public IEnumerable<WeatherForecast> Get()
{
ModelState.AddModelError("someProperty", "This property failed validation.");
var validation = new ValidationProblemDetails(ModelState)
{
Status = StatusCodes.Status422UnprocessableEntity
};
throw new ProblemDetailsException(validation);
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (10 by maintainers)
Top Results From Across the Web
Default model example in Swashbuckle (Swagger)
I'm running ASP WebAPI 2 and successfully installed Swashbuckle. I am trying to figure out how one defines what the default schema values...
Read more >Swagger tools for documenting API's built on ASP.NET Core
Swagger tooling for APIs built with ASP.NET Core. Generate beautiful API documentation, including a UI to explore and test operations, directly from your...
Read more >Multiple Request/Response examples for Swagger UI in ...
In this short tutorial, we are going to explore how can we add multiple examples for request and response in SwaggerUI.
Read more >Implement authorization for Swagger in ASP.NET Core 6
The output of the HttpGet action method in Swagger UI. We'll learn how to implement authentication in Swagger shortly. Let's first create a...
Read more >Swagger in ASP.Net Core (Using Swashbuckle.AspNetCore ...
Which will use the Swagger NuGet package ( Swashbuckle.AspNetCore). ... Source code location: https://github.com/choudhurynirjhar/ swagger - demo.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It does three things:
ProblemDetailsFactory
to use the middleware’s configuration instead. This will give you more consistent errors, no matter where they are produced. I.e. from MVC’s invalid state filter etc.Oh, and it adds a filter that makes returning pre-baked
ProblemDetails
instances from controllers a bit more consistent, by calling the middleware hooks 😃See the documentation here. Basically if you have an API controller that returns back a 4xx status code (with no body) or model validation fails, MVC will use the
ProblemDetailsFactory
to create an instance ofProblemDetails
and return it back, bypassing this middleware completely. So in order to make the responses consistent between MVC and the middleware, I’ve provided theAddProblemDetailsConventions
method to smooth things out a bit 😃