0

I am using asp.net core 3.1 with Swashbuckle 5.6. I am using a common class ApiResponse to standardize the response structure. So for both http status codes 404 and 500, my response structure will use same class.

But in the generated swagger documentation, I want to provide different examples for different response codes. If I use typeof(ApiResponse) with either ProducesResponseType or SwaggerResponse, it will end up showing same "Example value" for both 404 and 500 status codes. I tried providing sample in the XML documentation. But that does not come with schema.

ApiResponse class structure is same as that used in below link. https://www.devtrends.co.uk/blog/handling-errors-in-asp.net-core-web-api

public class ApiResponse
{
    public int StatusCode { get; }

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public string Message { get; }

    public ApiResponse(int statusCode, string message = null)
    {
        StatusCode = statusCode;
        Message = message ?? GetDefaultMessageForStatusCode(statusCode);
    }

    private static string GetDefaultMessageForStatusCode(int statusCode)
    {
        switch (statusCode)
        {
            ...
            case 404:
                return "Resource not found";
            case 500:
                return "An unhandled error occurred";
            default:
                return null;
        }
    }
}

Both statusCode and Message will be different for 404 and 500.

I have another similar issue with Ok Response also. By using generics, I am able to get proper example for class type. But for status code and Message, I am unable to provide specific values.

    public class ApiResponseOk<T> : ApiResponse
    {
        public T Result { get; }

        public ApiResponseOk()
        {

        }

        public ApiResponseOk(T result, string message = null)
            : base(200, message)
        {
            Result = result;
        }

    }

Please let me know how can I provide separate examples when using same type for response.

Thanks!

askids
  • 1,406
  • 1
  • 15
  • 32
  • Hi @askids, could please provide full class `ApiResponse` and which property will be different for `404` and `500` status codes? – DarkSideMoon Jan 05 '21 at 15:07
  • hi @DarkSideMoon, I have updated the question with class definition. Both StatusCode and Message will change. – askids Jan 05 '21 at 15:35
  • I have played a long time with your code and simple WebAPI example. I modify swagger using `ISchemaFilter` and try to override different error response `404, 500`, but it change response in swagger for both status codes, because you have one class for both response `ApiResponse`. I can add additional description such on the [screen](https://imgur.com/a/lfOIuzH). If it is okay for, I can describe solution. But to solve this situation in better way - It is great to have **different classes** and you can describe **different response** in `swagger`. – DarkSideMoon Jan 05 '21 at 22:29
  • For the 404 and 500 response, I was able to use another Nuget that allows me to provide examples, while continuing to use same class. Feature is called examplefilter. https://github.com/mattfrear/Swashbuckle.AspNetCore.Filters But for ok response, i still have same issue. I even tried adding example in ApiRespose. But that does not reflect in Swagger, when I have return type as typeof(ApiResponseOk). – askids Jan 06 '21 at 04:57
  • I think you need also add `ApiResponseOk` type in your examples filters, and add all types `ApiResponseOk` with concrete `MyClass`, may it work for swagger. – DarkSideMoon Jan 06 '21 at 12:42
  • Adding example will work. But that will be too much work to add every variant :) Moreover, they are recommending not to use it unless its absolutely necessary. Let me see if I can provide example in base class ApiResponse and see if that reflects in example.. – askids Jan 07 '21 at 13:05

0 Answers0