1

I am using Swagger UI to test my ASP.NET Web Api app. I added a class to allow operation parameters

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

    operation.Parameters.Add(new OpenApiParameter
    {
        Name = "ApiKey",
        In = ParameterLocation.Header,
        Required = true,
        Schema = new OpenApiSchema
        {
            Type = "String"
        }
    });
    operation.Parameters.Add(new OpenApiParameter
    {
        Name = "Authentication",
        In = ParameterLocation.Header,
        Required = false,
        Schema = new OpenApiSchema
        {
            Type = "String"
        }
    });
}

In my Startup.cs, I added this line to the ConfigurationServices method

c.OperationFilter<CustomHeaderSwaggerAttribute>();

When I try and test one of the controller methods, my ApiKey string parameter always show an error no matter what I put in the textbox.

enter image description here

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Ryan
  • 650
  • 3
  • 15
  • 49
  • This probably isn't the answer but would be quick to test. Have you tried: `Type = "string"` with a lowercase S? – SBFrancies Jan 12 '21 at 15:14
  • @SBFrancies create and answer for me to mark. Changing "String" to "string" worked! – Ryan Jan 12 '21 at 17:06

1 Answers1

1

I am not sure about the Schema property but the following worked for me in past (setting the type to string):

operation.Parameters.Add(new Parameter
        {
            name = "ApiKey",
            @in = ParameterLocation.Header,
            required = true,
            type = "string"                
        });

For more details, refer to this post

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160