Describing Parameters
Describing Parameters
Parameters are defined with @pathParam
, @queryParam
, @headerParam
, @cookieParam
or @paramComponent
.
To describe a parameter, you specify its data type, name, and description.
To denote a parameter as optional, enclose its name in square brackets [ ]
.
Here is an example:
_10/**_10 * GET /users/{userId}_10 * @summary Get a user by ID._10 * @pathParam {integer} userId - Numeric ID of the user to get._10 * @queryParam {boolean} [verbose] - Return all user information._10 */
Parameter types
OpenAPI 3.0 distinguishes between the following parameter types based on the parameter location.
- path parameters, such as
/users/{id}
- query parameters, such as
/users?role=admin
- header parameters, such as
X-MyHeader: Value
- cookie parameters, which are passed in the
Cookie
header, such asCookie: debug=0; csrftoken=BUSe35dohU3O1MZvDCU
Parameter parameters
Path parameters are variable parts of a URL path.
They are typically used to point to a specific resource within a collection, such as a user identified by ID.
A URL can have several path parameters, each denoted with curly braces { }
.
_10GET /users/{id}_10GET /cars/{carId}/drivers/{driverId}_10GET /report.{format}
Each path parameter must be substituted with an actual value when the client makes an API call.
In OpenAPI, a path parameter is defined using @pathParam
.
The parameter name must be the same as specified in the path.
Also remember path parameters are always required.
For example, the /users/{id}
endpoint would be described as:
_10/**_10 * GET /users/{id}_10 * @summary Get a user by ID._10 * @pathParam {integer} id - The user ID_10 */
Query parameters
Query parameters are the most common type of parameters.
They appear at the end of the request URL after a question mark (?
),
with different name=value
pairs separated by ampersands (&
).
Query parameters can be required and optional.
Use @queryParam
to denote query parameters:
_10/**_10 * @queryParam {integer} [offset] - The number of items to skip before starting to collect the result set_10 * @queryParam {integer} [limit] - The numbers of items to return_10 */
Note: To describe API keys passed as query parameters, use
securitySchemes
andsecurity
instead. See Authentication.
Header parameters
An API call may require that custom headers be sent with an HTTP request.
OpenAPI lets you define custom request headers with @headerParam
.
For example, suppose, a call to GET /ping
requires the X-Request-ID
header:
_10GET /ping HTTP/1.1_10Host: example.com_10X-Request-ID: 77e1c83b-7bb0-437b-bc50-a7a58e5660ac
You would define this operation as follows:
_10/**_10 * GET /ping_10 * @summary Checks if the server is alive_10 * @headerParam {string} X-Request-ID_10 */
Note: Header parameters named
Accept
,Content-Type
andAuthorization
are not allowed.
Cookie parameters
Operations can also pass parameters in the Cookie
header, as Cookie: name=value
.
Multiple cookie parameters are sent in the same header, separated by a semicolon, and space.
_10GET /api/users_10Host: example.com_10Cookie: debug=0; csrftoken=BUSe35dohU3O1MZvDCUOJ
Use @cookieParam
to define cookie parameters:
_10/**_10 * @cookieParam {integer} [debug]_10 * @cookieParam {string} [csrftoken]_10 */
Note: To define cookie authentication, use
securitySchemes
andsecurity
instead. See Authentication.
Required and optional parameters
By default, all request parameters are treated as required.
You can enclose the parameter’s name in square brackets [ ]
to mark it as optional.
Path parameters are always required.
Default parameter values
Append =value
to the variable name 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.
A typical example is paging parameters such as offset and limit:
_10GET /users_10GET /users?offset=30&limit=10
Assuming offset
defaults to 0 and limit
defaults to 20, you would define these parameters as:
_10/**_10 * @queryParam {integer} [offset=0] - The number of items to skip before starting to collect the result set._10 * @queryParam {integer} [limit=20] - The number of items to return._10 */
Enum parameters
You can restrict a parameter to a fixed set of values by adding the enum
to the schema
.
The enum values must be of the same type as the parameter data type.
_10/**_10 * @queryParam {Status} [status]_10 */
_10components:_10 schemas:_10 Status:_10 type: string_10 enum:_10 - available_10 - pending_10 - sold
Reusable Parameters
Parameters can be defined in components
to be reused elsewhere.
The following parameter definition:
_12components:_12 parameters:_12 ExampleParameter:_12 in: query_12 name: limit_12 schema:_12 type: integer_12 minimum: 1_12 maximum: 100_12 default: 20_12 required: false_12 description: The number of items to return.
Can be used as:
_10/**_10 * @paramComponent {ExampleParameter}_10 */