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

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

swagger - 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 ?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...