Test your knowledge of API design fundamentals with a focus on OpenAPI, covering resource modeling, schema-based validation, versioning, and ensuring backward compatibility. This quiz is ideal for those aiming to build robust and maintainable APIs using standardized practices.
Which approach is most recommended when modeling a collection resource in OpenAPI, for instance, to expose a list of books?
Explanation: /books aligns with RESTful conventions where plural nouns represent collections, making it easily understandable and standard. '/bookList' and '/books-list' use inconsistent naming or unnecessary descriptors. '/getBooks' incorrectly uses an action verb instead of a resource noun, which does not align with RESTful best practices.
What is the main advantage of using schema-based validation for an API's request payload in the OpenAPI specification?
Explanation: Schema-based validation enforces rules on input structure and types, reducing runtime errors and promoting data consistency. While it does not inherently increase response speed or update databases, and it has no effect on authentication requirements, it is crucial for input validation and API reliability.
If you need to introduce a breaking change, such as removing a field from your API response, which versioning strategy is most appropriate according to OpenAPI best practices?
Explanation: Breaking changes should trigger a major version increment and clear communication through the path (e.g., '/v2/') or headers for transparency. Making silent changes or only updating documentation can break existing clients unintentionally. Deprecation without versioning is insufficient for breaking changes.
When designing shared data structures such as 'User' or 'Address' in OpenAPI schemas, what is the best practice for maintaining consistency?
Explanation: Defining schemas once and referencing them promotes consistency and easier maintenance. Duplicating schemas increases risk of inconsistency, while omitting validation or using only inline definitions can make maintenance and validation difficult.
Which HTTP status code should be specified in OpenAPI for a successful resource creation via POST, such as adding a new order?
Explanation: 201 indicates resource creation, matching the semantics of POST for creating new entities. 200 is for a general successful request but not specific to creation. 204 means 'no content', which isn't appropriate when a new resource is created. 400 is used for client errors.
In an OpenAPI schema, how do you specify that a property like 'email' must always be present in client requests?
Explanation: Listing the property in the 'required' array is the established way to enforce its presence. Setting type as 'mandatory' or 'unique:true' are not standard schema keywords. 'optionalFields' has no meaning in OpenAPI schema specifications.
Which change is generally considered backward compatible in API schema evolution?
Explanation: Adding optional properties does not affect existing clients. Renaming or removing fields, or altering types, can break clients expecting the previous structure and are not backward compatible.
Where should resource identifiers, such as 'userId', be placed in an OpenAPI path for fetching a single item, for example retrieving a user profile?
Explanation: Resource identifiers for singular resources fit best in the path as path parameters for clarity and proper URL structure. Placing IDs in the body or headers is unconventional for GET requests, and query parameters are used for filtering collections rather than fetching a specific resource.
Which of the following is a recommended practice when defining error responses in OpenAPI specifications?
Explanation: A structured error object (with code and message) facilitates error handling and debugging across clients. Plain text or HTML responses are difficult for automated processing, while leaving schemas undefined leads to unpredictable responses.
How should you indicate in an OpenAPI schema that a field, such as 'oldPhoneNumber', will be removed in the future?
Explanation: Flagging the field with 'deprecated: true' formally warns consumers it is phased out. Deleting it immediately causes breaking changes; setting the type to null or reordering fields has no deprecation effect within OpenAPI.