Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swashbuckle add required request header

I am implementing a REST api where we use Swashbuckle as the consumer documentation. On our PUT endpoints for our resources we require that the consumer sends an If-Match header with the previous ETag of the resource.

We want to add this to the Swashbuckle documentation but do not seem to find how. How can we add to Swashbuckle that for certain endpoints the If-Match header is required?

Kr,

Thomas

like image 277
Thomas Hendrickx Avatar asked Dec 10 '25 12:12

Thomas Hendrickx


1 Answers

You can do that using an IOperationFilter here is a sample code:

    public class AddRequiredHeaderParameters : IOperationFilter
    {
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {                
            if (operation.operationId == "ValueProvider_Put")
            {
                if (operation.parameters == null)
                    operation.parameters = new List<NonBodyParameter>();
                operation.parameters.Add(HeaderParam("CID", "101"));                    
            }
        }

        public IParameter HeaderParam(string name, string defaultValue, bool required = true, string type = "string", string description = "")
        {
            return new NonBodyParameter
            {
                Name = name,
                In = "header",
                Default = defaultValue,
                Type = type,
                Description = description,
                Required = required
            };
        }
    }

I have a working sample here:

https://github.com/heldersepu/SwashbuckleTest/blob/master/Swagger_Test/App_Start/SwaggerConfig.cs#L551

And here is how it looks in the UI:

http://swashbuckletest.azurewebsites.net/swagger/ui/index?filter=ValueProvider#/ValueProvider/ValueProvider_Put

like image 85
Helder Sepulveda Avatar answered Dec 12 '25 09:12

Helder Sepulveda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!