Thomas Hendrickx
Thomas Hendrickx

Reputation: 171

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

Upvotes: 3

Views: 5231

Answers (1)

Helder Sepulveda
Helder Sepulveda

Reputation: 17594

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

Upvotes: 3

Related Questions