Forum Discussion

psychomonkey911's avatar
psychomonkey911
New Contributor
3 years ago

Swagger supports adding a Header parameter for 1 method

i have method with attribute [Authorize("AtLeast21")]

 

  builder.Services.AddAuthorization(options =>
    {
    options.AddPolicy("AtLeast21", policy =>
        policy.Requirements.Add(new MinimumAgeRequirement(21)));
    });

and i want to add new header parameter to swagger only for method with this attribute 

public class AddRequiredHeaders : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (context.MethodInfo.DeclaringType?.Name == nameof(ExportController))
            {
                return;
            }

            operation.Parameters ??= new List<OpenApiParameter>();
            operation.Parameters.Add(
                new OpenApiParameter
                {
                    Name = "CustomHeadersNames",
                    In = ParameterLocation.Header,
                    Required = true,
                    Schema = new OpenApiSchema { Type = "string" }
                });
       }
   }

this case add to all api method parameter, but i want add only for method with this attribute.

how to do this?