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?
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With