Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger / Open API 2.0 can I declare a common response header?

Is it possible to declare a custom response header which would be present in all responses without copying it in each of the response structure?

like image 201
Phani Avatar asked Feb 05 '16 07:02

Phani


People also ask

What are the formats supported by OpenAPI Swagger for their definitions?

Format. An OpenAPI document that conforms to the OpenAPI Specification is itself a JSON object, which may be represented either in JSON or YAML format.

Is OpenAPI the same as Swagger?

Although the terms once referred to the same thing, they can no longer be used interchangeably…even though some people still do. In 2021, OpenAPI refers to the industry-standard specification for RESTful API design. Swagger refers to a set of SmartBear tools.

How do you specify optional parameters in Swagger?

You can use the default key to specify the default value for an optional parameter. The default value is the one that the server uses if the client does not supply the parameter value in the request. The value type must be the same as the parameter's data type.


2 Answers

This was somewhat improved in OpenAPI 3.0 – you can now can define common headers in the global components/headers section and then $ref these definitions instead of repeating the inline definitions. You can also $ref whole responses (e.g. 400) to reduce the code duplication a bit. However, there's still no way to set common headers for all paths – you'll need to list the headers explicitly in each response.

openapi: 3.0.1
...
paths:
  /:
    get:
      responses:
        '200':
          description: OK
          headers:
            X-RateLimit-Limit:
              $ref: '#/components/headers/X-RateLimit-Limit'
            X-RateLimit-Remaining:
              $ref: '#/components/headers/X-RateLimit-Remaining'
  /something:
    get:
      responses:
        '200':
          description: OK
          headers:
            X-RateLimit-Limit:
              $ref: '#/components/headers/X-RateLimit-Limit'
            X-RateLimit-Remaining:
              $ref: '#/components/headers/X-RateLimit-Remaining'

components:
  headers:
    X-RateLimit-Limit:
      description: Request limit per hour
      schema:
        type: integer
      example: 100
    X-RateLimit-Remaining:
      schema:
        type: integer
      example: 96
like image 188
Helen Avatar answered Oct 02 '22 07:10

Helen


According to section 2.3 Response’s headers of Writing OpenAPI (Swagger) Specification Tutorial – Part 5 – Advanced Input And Output Modeling the answer is no. Which is what I understand from the 2.0 spec too.

Vote/comment on Structural improvements: enhance headers handling · Issue #690 · OAI/OpenAPI-Specification.

like image 24
Everett Toews Avatar answered Oct 02 '22 05:10

Everett Toews