Add a filter for a header in Swagger for ASP .NET Core 3.1

Viewed 10787

I have followed this:

Web Api How to add a Header parameter for all API in Swagger

and this:

How to send custom headers with requests in Swagger UI?

However, none of these IParameter, Parameter or NonBodyParameters work on ASP .NET CORE 3.1.

I would like to add a header on my swagger which takes a tenant-ID that is preferably taken from the logged user.

I have also went through this as well:

https://github.com/domaindrivendev/Swashbuckle.AspNetCore

Can anyone point me to the right direction?

using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.JsonPatch.Operations;
using Microsoft.OpenApi.Models;

namespace Intent2.Auth.Utils
{
    public class AddRequiredHeaderParameter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            
            if (operation.Parameters == null)
                operation.Parameters = new List<IParameter>();

            operation.Parameters.Add(new NonBodyParameter
            {
                Name = "X-User-Token",
                In = "header",
                Type = "string",
                Required = false
            });
        }
    }
}
services.AddSwaggerGen(options =>
{
    options.OperationFilter<AddRequiredHeaderParameter>();
}
1 Answers

With the latest version of Swashbuckle compatible with ASP.NET Core 3.1 many types have been replaced by equivalent types in the Microsoft.OpenApi.Models namespace. So you shouldn't use anymore types like NonBodyParameter or IParameter. Both of these have been replaced by a single class OpenApiParameter.

Your code should look like this

using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;

namespace Intent2.Auth.Utils
{
    public class AddRequiredHeaderParameter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {

            if (operation.Parameters == null)
                operation.Parameters = new List<OpenApiParameter>();

            operation.Parameters.Add(new OpenApiParameter()
            {
                Name = "X-User-Token",
                Description = "Access Token",
                In = ParameterLocation.Header,
                Schema = new OpenApiSchema() { Type = "String" },
                Required = true,
                Example = new OpenApiString("Tenant ID example")
            });
        }
    }
}

Then in your startup, simply inject SwaggerGen as usual

services.AddSwaggerGen(options =>
{
    options.OperationFilter<AddRequiredHeaderParameter>();
}

You can even make the Tenant ID coming from the outside like a configuration file for example. To do that, modify your AddRequiredHeaderParameter as follow

using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;

namespace Intent2.Auth.Utils
{
    public class AddRequiredHeaderParameter : IOperationFilter
    {
        private string _tenantIdExample;

        public AddRequiredHeaderParameter(string tenantIdExample)
        {
            if (string.IsNullOrEmpty(tenantIdExample )) 
                throw new ArgumentNullException(nameof(tenantIdExample ));

            _tenantIdExample = tenantIdExample;
        }

        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {

            if (operation.Parameters == null)
                operation.Parameters = new List<OpenApiParameter>();

            operation.Parameters.Add(new OpenApiParameter()
            {
                Name = "X-User-Token",
                Description = "Access Token",
                In = ParameterLocation.Header,
                Schema = new OpenApiSchema() { Type = "String" },
                Required = true,
                Example = new OpenApiString(_tenantIdExample)
            });
        }
    }
}

And call it that way from your startup

services.AddSwaggerGen(options =>
{
    options.OperationFilter<AddRequiredHeaderParameter>("Tenant ID example");
}

By the way I think if your class is called AddRequiredHeaderParameter you should actually set Required = true instead of false

Related