DevNet Associate (DEVASC) v1.0Understanding and Using APIsHard

A developer is documenting a new REST API using OpenAPI Specification (OAS) 3.0. This API includes an endpoint `/users/{userId}` that retrieves details for a specific user. The `userId` is a mandatory path parameter and must be an integer. How should this path parameter be correctly defined in the OpenAPI YAML specification?

  1. A```yaml parameters: - name: userId in: query required: true schema: type: integer ```
  2. B```yaml parameters: - name: userId in: path required: true schema: type: string ```
  3. C```yaml parameters: - name: userId in: header required: true schema: type: integer ```
  4. D```yaml parameters: - name: userId in: path required: true schema: type: integer format: int64 ```
Show answer & explanation

Correct answer: D. ```yaml parameters: - name: userId in: path required: true schema: type: integer format: int64 ```

For a path parameter `userId` that is mandatory and an integer, the definition must specify `in: path`, `required: true`, and `schema: type: integer`. Adding `format: int64` is good practice to specify a 64-bit integer, which is a common format for IDs.

Why the other options are wrong

  • A. Incorrect `in: query` as it's a path parameter, not a query parameter.
  • B. Incorrect `type: string` for a mandatory integer parameter.
  • C. Incorrect `in: header` as it's a path parameter, not an HTTP header parameter.

OpenAPI Path Parameters

In OpenAPI Specification, path parameters are variables defined within the URL path (e.g., `/items/{itemId}`). They are specified using the `parameters` field, with `in: path` and usually `required: true` for mandatory segments.

  • Defined within the `parameters` section of an operation or globally.
  • `in: path` indicates it's part of the URL path.
  • `required: true` for mandatory path segments.
  • Must include a `schema` defining its `type` and optional `format`.

Memory trick: Parameters need Name, In, Required, and Schema to be right.

More Understanding and Using APIs questions