proudmonkey / AutoWrapper

A simple, yet customizable global exception handler and Http response wrapper for ASP.NET Core APIs.
MIT License
679 stars 82 forks source link

Typed response for a smoother swagger implementation #107

Closed PBonvang closed 3 years ago

PBonvang commented 3 years ago

Hi :)

After implementing AutoWrapper I still wish to show the result type in the Swagger doc, so my consumers know what to expect. Currently it's only showing the ApiResponse model. image

I might have overlooked something, is there an easy way to do so? Else my suggestion is to create a ApiResponse with a type input, my current workaround:

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

    public ApiResponse(string message, T result, int statusCode, string apiVersion) : base(message, result, statusCode, apiVersion)
    {
        Result = result;
    }

    public ApiResponse(T result, int statusCode = 200) : base(result, statusCode)
    {
        Result = result;
    }
}

Then in my controller I can use: [ProducesResponseType(typeof(ApiResponse<MyModel>), StatusCodes.Status200OK)]

Resulting in: image

As this seems super easy to implement from my very inexperienced POV, I thought why not have it in the package it self instead of having to share this between my services.

Please let me know if I'm wrong. I'm here to learn :)

proudmonkey commented 3 years ago

Hello, Thank you for your comment :)

Actually, AutoWrapper provides a few Models for you to use as the response shape. You can see them here: https://github.com/proudmonkey/AutoWrapper/tree/master/src/AutoWrapper/Models

In your example, you could do:

[ProducesResponseType(typeof(ApiResultResponse<MyModel>), StatusCodes.Status200OK)]

Note that it was my intent to just expose the Message and the Result metadata for the response as others are kind of redundant to show.

PBonvang commented 3 years ago

Hi again :)

Thank you for the fast response. That seems like what I was looking for :) 👍 It might have been nice to have the status code as well, but as it can be gotten from context I don't see it as a necessity.

lydonchandra commented 3 years ago

What is the easiest way to tell swagger that ALL responses are wrapped by ResponseWrapper ?

Currently ALL Api endpoints have to be annotated with

[ProducesResponseType(typeof(ApiResultResponse<MyModel>), StatusCodes.Status200OK)]