Exercises related to OpenAPI
OpenAPI specification
Exercice 1
Answer the following questions:
- How do you specify where parameters are possibly added to a request for a given endpoint in OpenAPI?
- Can you specify whether some parameters to a request are required or not in OpenAPI? If yes, how?
- Is it possible to describe multiple responses for an endpoint in OpenAPI? If yes, then how?
- What is a common way to avoid duplication in an OpenAPI specification document?
Solution
Answers are:
- You can specify it using the “in” field of the “parameters” field. This field takes a string value that can either be “query”, “header”, “path”, or “cookie”.
- Yes, you can use the “required” field in the parameters object to specify whether a parameter is required or not.
- Yes, it is possible to describe multiple responses for an endpoint. This can be done by using the “responses” field, followed by a list of different response codes that the endpoint could return. For each response code, a description of the corresponding response must be given.
- One way to avoid duplication is to use the
$ref
keyword. Note references can be made to external documents by referring to the document path or to other properties of the document by using the “#/…” notation.