question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Swashbuckle output different from demo

See original GitHub issue

Dear 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:

image

However, if I create a new ASP.Net 5.0 Web API, then I don’t get that description:

image

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:closed
  • Created 2 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
khellangcommented, Jan 25, 2022

It does three things:

  1. It swaps MVC’s built-in 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.
  2. It turns off MVC’s built-in client error mapping, which also gives you more consistent error responses
  3. It tries to set the produced error response type on API controllers, but apparently that isn’t working too well 😅
0reactions
khellangcommented, Jan 25, 2022

Oh, and it adds a filter that makes returning pre-baked ProblemDetails instances from controllers a bit more consistent, by calling the middleware hooks 😃

Sorry for being slow, but when exactly does nr 1 and 2 occur? I have no idea when invalid state filter or built-in client error mapping is used.

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 of ProblemDetails and return it back, bypassing this middleware completely. So in order to make the responses consistent between MVC and the middleware, I’ve provided the AddProblemDetailsConventions method to smooth things out a bit 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found