I have a generic Result<T>
response type in my controllers, e.g.
public Result<T> GetSomething()
{
...
}
I also have a custom asp.net core filter that returns a Json representation of T
To have swashbuckle generate correct documentation, I have to decorate every method with:
[Produces(typeof(T))]
As this is cumbersome, easily forgotten and error prone, I was looking for a way to automate this.
Now in Swashbuckle you have a MapType
, but I can't get a hold of the T
in those methods:
services.AddSwaggerGen(c =>
{
...
c.MapType(typeof(Result<>), () => /*can't get T here*/);
};
I was looking at the IOperationFilter
but I can't find a way to override the result type in there.
Then there are ISchemaFilter
public class ResultSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (!context.Type.IsGenericType || !context.Type.GetGenericTypeDefinition().IsAssignableFrom(typeof(Result<>)))
{
return;
}
var returnType = context.Type.GetGenericArguments()[0];
//How do I override the schema here ?
var newSchema = context.SchemaGenerator.GenerateSchema(returnType, context.SchemaRepository);
}
}