Skip to content

How to add a custom header parameter at the end of the parameters list in Swagger 2.0/OpenAPI 3.0? #1948

Open
@alexpantyukhin

Description

@alexpantyukhin

I want to have my custom parameter at the end of parameters list in the swagger.json.
The example what I want to see:

        "parameters": [
          {
            "in": "path",
            "name": "Id",
            "required": true,
            "type": "integer",
            "format": "int32"
          },
          {
            "in": "body",
            "name": "body",
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/MyCustomClass"
              }
            }
          },
          {
            "in": "header",
            "name": "key",
            "description": "The key.",
            "type": "string"
          },
        ],

I used the following code :
in Program.cs:

   app.UseSwagger(p => p.SerializeAsV2 = true);
public class AddRequiredParameter: IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
            var stringOpenApiSchema = context.SchemaGenerator.GenerateSchema(typeof(string), context.SchemaRepository);
            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "key",
                In = ParameterLocation.Header,
                Schema = stringOpenApiSchema,
                Required = false,
                Description = "The key."
            });
            operation.Security = new List<OpenApiSecurityRequirement>();
    }
}

But now I have only the key parameter is placed before the body parameter:

        "parameters": [
          {
            "in": "path",
            "name": "Id",
            "required": true,
            "type": "integer",
            "format": "int32"
          },
          {
            "in": "header",
            "name": "key",
            "description": "The key.",
            "type": "string"
          },
          {
            "in": "body",
            "name": "body",
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/MyCustomClass"
              }
            }
          }
        ],

The problem I have is that the client's code generator changes the order of parameters because I used an old implementation of Swashbuckle for .NET 4.8, which placed the key parameter at the end.
Now, I just want to preserve as much as possible and maintain the same order of parameters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions