Search code examples
swaggeropenapinswag

Define all headers as a single dictionary


I'd like to define http endpoint with "any header welcome" in specification (i.e. dictionary of strings). Right now I do have to account for every single (known) header one-by-one:

"parameters": [
      {
        "name": "x-header-1",
        "in": "header",
        "type": "string"
      },
      {
        "name": "x-header-2",
        "in": "header",
        "type": "string",
      }
]

I'd like to define them like

"parameters": [
       {
          "name": "headers"
          "in": "headers"
       }
 ]

And ultimately in C# have a code like SomeMethod([FromHeader]Dictionary<string, string> ...) for this definition. Is this even possible ?


Solution

  • OpenAPI Specification does not have a way to define arbitrary headers like in your example. Here is the corresponding enhancement request:
    Support wildcard header/parameter


    If you are designing a new API (as opposed to documenting an existing API), you can try using a single header containing comma-separated key=value pairs, as suggested in the comments in the link above:

    X-MyHeader: key1=value, key2=value, key3=value
    

    In OpenAPI 3.0, such header can be defined as an exploded object:

    # openapi: 3.0.3
    
    parameters:
      - in: header
        name: X-MyHeader
        schema:
          type: object   # Free-form object
          example:
            key1: value1
            key2: value2
        style: simple    # Default (and only) style for headers, can be omitted 
        explode: true