Click here to Skip to main content
16,017,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am returning a 407 http status code from one endpoint, in postman I can see the
407Proxy Authentication Required
response, but in my api swaggerui instead of 407 it is showing as
Code	Details
Undocumented
Failed to fetch.


To cross check this I have created a new api project & directly return the 407 status code, in new project also it is returning the same.

what am I missing here?

What I have tried:

public IActionResult Get()
        {
            return StatusCode((int)HttpStatusCode.ProxyAuthenticationRequired);

        }
Posted
Updated 19-Oct-22 1:41am

1 solution

Swagger does not attempt to analyze your code to determine every possible response code. You need to tell it which response codes your action can return:

Get started with Swashbuckle and ASP.NET Core | Microsoft Learn[^]

Eg:
C#
[HttpGet]
[ProducesResponseType(StatusCodes.Status407ProxyAuthenticationRequired)]
public IActionResult Get()

You can edit your project file to get Visual Studio to analyze your code and issue a warning if your action returns a response code which you haven't documented:
Use web API analyzers | Microsoft Learn[^]
XML
<PropertyGroup>
    <IncludeOpenAPIAnalyzers>true</IncludeOpenAPIAnalyzers>
</PropertyGroup>

You can also use API conventions if your actions almost always return the same set of responses:
Use web API conventions | Microsoft Learn[^]
 
Share this answer
 
Comments
Virendra S from Bangalore, Karnataka 19-Oct-22 8:14am    
I have already added the [ProducesResponseType(StatusCodes.Status407ProxyAuthenticationRequired)] to my controller.
Richard Deeming 19-Oct-22 8:15am    
And you didn't think that was worth mentioning in your question?
Virendra S from Bangalore, Karnataka 19-Oct-22 8:59am    
Sorry!! Forgot to mention it.
Richard Deeming 19-Oct-22 8:16am    
And I suspect you need to add the attribute to your action, not your controller.
Virendra S from Bangalore, Karnataka 19-Oct-22 9:01am    
yes, at controller action it is already decorated.
For other status code, swagger ui showing correct response. only in case of 407 not showing.



[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ApiResponse))]
[ProducesResponseType(StatusCodes.Status500InternalServerError, Type = typeof(ApiResponse))]
[ProducesResponseType(StatusCodes.Status401Unauthorized, Type = typeof(ApiResponse))]
[ProducesResponseType(StatusCodes.Status407ProxyAuthenticationRequired, Type = typeof(ApiResponse))]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900