Search code examples
c#asp.net-coreswaggerswagger-uiswashbuckle.aspnetcore

Swagger Ui(SwashBuckle for Core) is not throwing UI level Error Message for Mandatory Headers


I am currently developing a WebApi with Swagger Ui. i am facing one weird issue. I have some Header fields those i have setup as Mandatory (*required) fields when a user try to call an API.

If a user misses to pass any header it suppose to show warning as given in Image-1.(In image-1 the red marked field is Query param) which is working fine but not working for Header. enter image description here

For Headers i am getting the Issue as shown in the Image-2. Error suppose to come on the UI but its coming in the browser console window. enter image description here

Code in IOperationFilter as follows

public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
                operation.Parameters = new List<OpenApiParameter>();

            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "X-Transaction-Id",
                In = ParameterLocation.Header,
                Required = true,
                AllowEmptyValue=false,
                Description="Transaction Id to track the issue"
            });
       }

NB:- I have used IOperationFilter to set the required headers.Using Swashbuckle.AspNetCore(5.6.3). Any help on this will be really great!!


Solution

  • You must specify the Schema (data type) for header parameters, such as:

                operation.Parameters.Add(new OpenApiParameter
                {
                    Name = "X-Transaction-Id",
                    ...
    
                    Schema = new OpenApiSchema   // <-----------------
                    {
                        Type = "string"
                    }
                });