Skip to content

Commit

Permalink
Support for minimal APIs in implementations of IOperationFilter (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell authored May 10, 2024
1 parent 2be9685 commit ed4ce05
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<OperationErrorCodesAttribute>();
attributes.AddRange(controllerAttributes);
}

// get attributes from the endpoint metadata
var metadataAttributes = actionDescriptor.EndpointMetadata.OfType<OperationErrorCodesAttribute>();
attributes.AddRange(metadataAttributes);

// make unique error codes
var uniqueErrorCodes = attributes.SelectMany(attr => attr.Errors)
.ToHashSet(StringComparer.OrdinalIgnoreCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OperationExtraTagAttribute>();
attributes.AddRange(controllerAttributes);
}

// get attributes from the endpoint metadata
var metadataAttributes = actionDescriptor.EndpointMetadata.OfType<OperationExtraTagAttribute>();
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ public class InternalOnlyOperationFilter : IOperationFilter
/// <inheritdoc/>
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<InternalOnlyAttribute>(inherit: true);
if (attr is null && context.ApiDescription.ActionDescriptor is ControllerActionDescriptor cad)
{
attr = cad.ControllerTypeInfo.GetCustomAttribute<InternalOnlyAttribute>(inherit: true);
}

// check attribute on the controller
var actionDescriptor = context.ApiDescription.ActionDescriptor;
attr ??= (actionDescriptor as ControllerActionDescriptor)?.ControllerTypeInfo.GetCustomAttribute<InternalOnlyAttribute>(inherit: true);

// check the endpoint metadata
attr ??= actionDescriptor.EndpointMetadata.OfType<InternalOnlyAttribute>().FirstOrDefault();
if (attr is null) return;

// At this point, the API is internal only, so just set the extension value
Expand Down

0 comments on commit ed4ce05

Please sign in to comment.