Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 ?

question from:https://stackoverflow.com/questions/65943482/define-all-headers-as-a-single-dictionary

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...