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

[Question]: How to add a custom header parameter at the end of the parameters list in Swagger 2.0/OpenAPI 3.0? #3161

Open
alexpantyukhin opened this issue Nov 20, 2024 · 1 comment
Labels

Comments

@alexpantyukhin
Copy link

What are you wanting to achieve?

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"
          },
        ],

What code or approach do you have so far?

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"
              }
            }
          }
        ],

Additional context

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.

@martincostello
Copy link
Collaborator

AFAIR, the ordering is controlled by the serialisation code in the Microsoft.OpenApi library that we depend on. We are not able to directly control the JSON serialisation of their primitives themselves, so if they are special-casing the body to go at the end when it's serialised, there's not really anything we can do about it.

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

No branches or pull requests

2 participants