Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alexpantyukhin opened this issue Nov 21, 2024 · 0 comments

Comments

@alexpantyukhin
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant