API / Swagger Interview questions
What is the role of the servers object in OpenAPI 3.0?
The servers array declares the base URL(s) where the described API can actually be reached, replacing Swagger 2.0's more limited single host/basePath/schemes combination with something that can describe multiple environments and even templated, variable URL segments in one place.
servers: - url: https://api.example.com/v1 description: Production - url: https://staging-api.example.com/v1 description: Staging - url: https://{region}.api.example.com/v1 description: Regional deployment variables: region: default: us enum: [us, eu, ap]
Each entry can also declare variables — named placeholders in the URL template, each with a default value and optionally a fixed set of allowed values — which is how a single server entry can describe a whole family of environment-specific or region-specific URLs without needing a separate, fully spelled-out entry for every one.
Tools that consume the spec, like Swagger UI, present a dropdown letting a consumer pick which declared server to send "Try it out" requests against, which is especially useful for a document meant to describe the same API across multiple deployment environments without needing to maintain separate spec files for each one.
More Related questions...