2

I have a simple .NET 6 API that is using Swashbuckle (6.3.1).

I've noticed that when I do a request from Swagger UI (after hitting F5 in VS2022) and when I return error details they are not displayed in Swagger UI, but when the same request is done in a new chrome tab the error details are returned, same with Postman.

enter image description here

enter image description here

enter image description here

when I do the same request in another tab I get this (correct) response:

enter image description here

enter image description here

I noticed that Swagger UI is doing a fetch type request, but when I do a "normal" request I get a document type request:

Swagger UI: enter image description here

Standard request: enter image description here

My question is how can I configure Swashbuckle so that I get error details when I do a request from Swagger UI.

As requested I'm adding my controller:

[ApiController]
public class LocalizationController : ApiController
{
    /// <summary>
    /// Get supported languages
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpGet("languages")]
    [AllowAnonymous]
    [Cache(CacheDuration.FiveMinutes, 1, CacheTags.Languages)]
    public async Task<IEnumerable<LanguageDto>> GetLanguages([FromRoute] GetLanguages model) => await Process(model);

    
    /// <summary>
    /// Get localized strings in requested language
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpGet("{language}")]
    [ProducesResponseType(typeof(IEnumerable<LocalizationDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [AllowAnonymous]
    [Cache(CacheDuration.FiveMinutes, 1, CacheTags.Languages)]
    public async Task<IEnumerable<LocalizationDto>> GetLocalizations([FromRoute] GetLocalizations model) => await Process(model);
}

and my base ApiController:

public abstract class ApiController : ControllerBase
{
    /// <summary>
    /// MediatR mediator
    /// </summary>
    protected IMediator Mediator => HttpContext.RequestServices.GetRequiredService<IMediator>();

    /// <summary>
    /// Maps the given object to the given type
    /// </summary>
    protected IMapper Mapper => HttpContext.RequestServices.GetRequiredService<IMapper>();

    protected async Task<TResponse> Process<TResponse>(ICommand<TResponse> command)
    {
        return await Mediator.Send(command);
    }

    /// <summary>
    /// Process command - Create, Update, Delete
    /// </summary>
    /// <param name="command"></param>
    /// <typeparam name="TModel"></typeparam>
    /// <typeparam name="TResponse"></typeparam>
    /// <returns></returns>
    protected async Task<TResponse> Process<TModel, TResponse>(ICommand<TModel> command)
    {
        var result = await Mediator.Send(command);

        return Mapper.Map<TResponse>(result);
    }

    /// <summary>
    /// Process query - only Read
    /// </summary>
    /// <param name="query"></param>
    /// <typeparam name="TResponse"></typeparam>
    /// <returns></returns>
    protected async Task<TResponse> Process<TResponse>(IQuery<TResponse> query)
    {
        return await Mediator.Send(query);
    }
}
Misiu
  • 4,738
  • 21
  • 94
  • 198
  • please add your controller method also – CodingMytra Jul 05 '22 at 12:25
  • @CodingMytra question updated – Misiu Jul 05 '22 at 12:48
  • is this custom error message is coming from some middleware ? – CodingMytra Jul 06 '22 at 06:55
  • yes. I did some more testing and push my changes to dev server, there the error is showing correctly in swagger. SO the problem is only when I run this directly from VS2022. – Misiu Jul 06 '22 at 09:30
  • 1
    I am not sure where you custom your error message, But when i use swagger, I meet the same issue, It doesn't show any error message details, But when i use postman it works well, maybe you can refer to this [issue](https://stackoverflow.com/questions/47823228/swagger-does-not-show-real-error-message). – Xinran Shen Jul 06 '22 at 09:57
  • @XinranShen thank you for the link, will try that, if nothing works, I'll create an issue in Swashbuckle repository. – Misiu Jul 06 '22 at 11:23

0 Answers0