Workaround for using type parameter in attribute for using ProducesResponseType with a generic type argument?

Althrough I found no way to use generic type parameter in ProducesResponseTypeAttribute, I found another way to make swagger work:

Use IApplicationModelConvention to update ApplicationModel, which is used by swagger.

public class EntityControllerConversion : IApplicationModelConvention
{
    public void Apply(ApplicationModel application)
    {
        ActionModel action = ... // finds the controller action 
        Type viewModelType = ... // get the view type by reflection from the controller
        SetResponseUsingHack(action, viewModelType, HttpStatusCode.OK);
    }

    private void SetResponseUsingHack(ActionModel actionModel, Type responseType, HttpStatusCode statusCode)
    {
        if (actionModel == null) throw new ArgumentNullException(nameof(actionModel));
        if (responseType == null) throw new ArgumentNullException(nameof(responseType));

        var writable = (IList<object>)(actionModel.Attributes);
        var attribute = FindResponseAttributeUsingHack(writable, statusCode);
        if (attribute != null)
        {
            attribute.Type = responseType;
        }
    }

    private ProducesResponseTypeAttribute FindResponseAttributeUsingHack(IList<object> attributes, HttpStatusCode statusCode)
    {
        if (attributes == null) return null;
        var result = attributes.OfType<ProducesResponseTypeAttribute>()
            .Where(x => x.Type == typeof(ProducesResponseStub))
            .FirstOrDefault(x => x.StatusCode == (int) statusCode);
        return result;
    }
}

public abstract class EntityController<TEntity> 
{
    [HttpGet]
    [ProducesResponseType(typeof(ProducesResponseStub), 200)]
    public IActionResult Get(string id)
    {
    }
}

public static class ProducesResponseStub
{
}

NOTE: Swagger won't work correctly if you just add a new ProducesResponseTypeAttribute instance to ActionModel.Attributes, may be it's a bug in swagger or in asp.net core. That why I use ProducesResponseStub in decorating action methods in EntityController and replace them with correct types in EntityControllerConversion.


Since .NET Core 2.1 instead of using IActionResult, you can use ActionResult<TEntity> as returntype (or Task<ActionResult<TEntity>>) and then swagger will also know the returntype for 200 calls!

Tags:

C#

Asp.Net