Hi,
I have tried for hours to access this Class attribute but it does not appear to exist. I am unable to figure out how to access it from the OperationFilter.
my goal is to have this schema:
"MyClass": {
"get": {
"x-displayName": "MyClass"
}
}
Can anyone help?
I am using Swashbuckle.AspNetCore and ASP.Net Core mapped endpoint (i.e. options.MapEndpoints(true, true, UseSystemJson.Never))
I created this custom attribute:
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public class OpenApiDisplayNameAttribute : Attribute
{
public string DisplayName { get; set;}
}
Added it to a class:
[OpenApiDisplayName(nameof(MyClass))]
[Route("/MyClass", "GET")]
public class MyClass
{
}
Added it my SwaggerGen:
services.AddSwaggerGen(options =>
{
..
options.OperationFilter<OpenApiDisplayNameOperationFilter>();
..
}
and created a OperationFilter:
public class OpenApiDisplayNameOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
//This does not work--always NULL
var descriptor = context.ApiDescription.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor;
//This doesn't work either--always NULL
var attribute = context.MethodInfo?.GetCustomAttribute<OpenApiDisplayNameAttribute>();
if (attribute != null)
{
operation.AddExtension("x-displayName", new OpenApiString(attribute.DisplayName));
}
}
}