Skip to content

Feature request: Common request parameters for multiple routes #1515

@OscarVanL

Description

@OscarVanL

Is your feature request related to a problem? Please describe.
In my org, we have a common header that is used for internal log correlation between microservices, X-TraceId. This means that every API route should accept this header so it can be propagated accordingly.

I would like this to be included on our generate OpenAPI spec.

However, I cannot find a DRY way to include the same request header on every route.

It would be helpful to be able to define ninja request parameters, such as query args or headers on a router (or even NinjaAPI) level. This would avoid unnecessary repetition.

Describe the solution you'd like
Currently, it is possible to add these request parameters in two ways.

Method 1: Function parameters

@api.get("/ready", tags=["k8s"], response={200: None})
def ready(request, trace_id: str = Header(alias="X-TraceId")):
    return 200, None

Method 2: openapi_extra

@api.get("/ready", tags=["k8s"], response={200: None}, openapi_extra={
    "parameters": [
        {
            "in": "header",
            "name": "X-TraceId",
            "required": False,
            "schema": {"type": "string"},
            "description": "Tracking ID for log correlation"
        }
    ]
})
def ready(request):
    return 200, None

It would be great to be able to define either:

  • request method parameters that are automatically "inherited" by all routes in that router
  • openapi_extra that are "inherited" by all routes in the router

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions