diff --git a/src/Tingle.AspNetCore.Swagger/Filters/Operations/ErrorCodesOperationFilter.cs b/src/Tingle.AspNetCore.Swagger/Filters/Operations/ErrorCodesOperationFilter.cs index a6beed6..fff33a6 100644 --- a/src/Tingle.AspNetCore.Swagger/Filters/Operations/ErrorCodesOperationFilter.cs +++ b/src/Tingle.AspNetCore.Swagger/Filters/Operations/ErrorCodesOperationFilter.cs @@ -24,12 +24,17 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context) attributes.AddRange(methodAttributes); // get attributes from the controller - if (context.ApiDescription.ActionDescriptor is ControllerActionDescriptor cad) + var actionDescriptor = context.ApiDescription.ActionDescriptor; + if (actionDescriptor is ControllerActionDescriptor cad) { var controllerAttributes = cad.ControllerTypeInfo.GetCustomAttributes(inherit: true).OfType(); attributes.AddRange(controllerAttributes); } + // get attributes from the endpoint metadata + var metadataAttributes = actionDescriptor.EndpointMetadata.OfType(); + attributes.AddRange(metadataAttributes); + // make unique error codes var uniqueErrorCodes = attributes.SelectMany(attr => attr.Errors) .ToHashSet(StringComparer.OrdinalIgnoreCase); diff --git a/src/Tingle.AspNetCore.Swagger/Filters/Operations/ExtraTagsOperationFilter.cs b/src/Tingle.AspNetCore.Swagger/Filters/Operations/ExtraTagsOperationFilter.cs index d224383..5262659 100644 --- a/src/Tingle.AspNetCore.Swagger/Filters/Operations/ExtraTagsOperationFilter.cs +++ b/src/Tingle.AspNetCore.Swagger/Filters/Operations/ExtraTagsOperationFilter.cs @@ -22,21 +22,24 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context) attributes.AddRange(methodAttributes); // get attributes from the controller - if (context.ApiDescription.ActionDescriptor is ControllerActionDescriptor cad) + var actionDescriptor = context.ApiDescription.ActionDescriptor; + if (actionDescriptor is ControllerActionDescriptor cad) { var controllerAttributes = cad.ControllerTypeInfo.GetCustomAttributes(inherit: true).OfType(); attributes.AddRange(controllerAttributes); } + // get attributes from the endpoint metadata + var metadataAttributes = actionDescriptor.EndpointMetadata.OfType(); + attributes.AddRange(metadataAttributes); + // make the attributes unique by name - var uniqueAttributes = attributes.ToDictionary(attr => attr.Name, StringComparer.OrdinalIgnoreCase); + var uniqueAttributes = attributes.DistinctBy(attr => attr.Name, StringComparer.OrdinalIgnoreCase); operation.Tags ??= []; - foreach (var kvp in uniqueAttributes) + foreach (var attr in uniqueAttributes) { - var attr = kvp.Value; - operation.Tags.Add(new OpenApiTag { Name = attr.Name, diff --git a/src/Tingle.AspNetCore.Swagger/Filters/Operations/InternalOnlyOperationFilter.cs b/src/Tingle.AspNetCore.Swagger/Filters/Operations/InternalOnlyOperationFilter.cs index 32dec68..cdd8467 100644 --- a/src/Tingle.AspNetCore.Swagger/Filters/Operations/InternalOnlyOperationFilter.cs +++ b/src/Tingle.AspNetCore.Swagger/Filters/Operations/InternalOnlyOperationFilter.cs @@ -18,12 +18,15 @@ public class InternalOnlyOperationFilter : IOperationFilter /// public void Apply(OpenApiOperation operation, OperationFilterContext context) { - // Check if the method or controller has the attribute declared/annotated + // check attribute on the method var attr = context.MethodInfo.GetCustomAttribute(inherit: true); - if (attr is null && context.ApiDescription.ActionDescriptor is ControllerActionDescriptor cad) - { - attr = cad.ControllerTypeInfo.GetCustomAttribute(inherit: true); - } + + // check attribute on the controller + var actionDescriptor = context.ApiDescription.ActionDescriptor; + attr ??= (actionDescriptor as ControllerActionDescriptor)?.ControllerTypeInfo.GetCustomAttribute(inherit: true); + + // check the endpoint metadata + attr ??= actionDescriptor.EndpointMetadata.OfType().FirstOrDefault(); if (attr is null) return; // At this point, the API is internal only, so just set the extension value