3

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

  • Possible duplicate of [Add individual custom headers in different controllers in web api using Swasbuckle](https://stackoverflow.com/q/44492912/113116) and [Web Api How to add a Header parameter for all API in Swagger](https://stackoverflow.com/questions/41493130/web-api-how-to-add-a-header-parameter-for-all-api-in-swagger) – Helen Jan 09 '18 at 23:15

1 Answers1

3

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

Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
Helder Sepulveda
  • 15,500
  • 4
  • 29
  • 56