Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of servers property in OpenAPI 3.0?

Tags:

openapi

In the OpenAPI 3.0 Specification, the root OpenAPI Object has the servers property which is an array of Server Objects. And the Path Item Object also allows an optional servers property.

The description given in the Specification does not give a clear idea of how servers can be helpful.

What is the significance of the servers property? Do we have any example which explains the use cases of servers both as a direct property of the root OpenAPI object and also as a property of a path item?

like image 700
codeofnode Avatar asked May 26 '18 20:05

codeofnode


People also ask

How many servers can you add to an OpenAPI Specification?

Multiple hosts are supported in OpenAPI 3.0. 2.0 supports only one host per API specification (or two if you count HTTP and HTTPS as different hosts). A possible way to target multiple hosts is to omit the host and schema from your specification and serve it from each host.

What is the purpose of the OpenAPI Specification?

The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to HTTP APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection.

What is Swagger server?

servers replaces the host , basePath and schemes keywords used in OpenAPI 2.0. Each server has an url and an optional Markdown-formatted description .

What are the three primary sections in a Swagger Specification?

Basic authentication. API key (as a header or query parameter) OAuth 2 common flows (implicit, password, application and access code)


1 Answers

servers specifies one or more target servers for the API, in other words, the base URL for API calls. The endpoint paths (e.g. /users/{id}) are defined relative to these servers. Some APIs have a single target server; others may offer several servers, e.g. sandbox vs. production, or regional servers for different geographical areas (example: AWS).

By default, all operations in an OpenAPI definition use the globally defined servers, but servers may also be overridden for specific paths and operations. This is useful for APIs where some operations use a different server than the rest of the operations. This way you can document all operations in a single API definition instead of splitting it into multiple definitions, one per server.


Example: Dropbox API

  • Most endpoints are on the api.dropboxapi.com domain.
  • Content upload/download endpoints are on content.dropboxapi.com.
  • Longpoll endpoint is on notify.dropboxapi.com.
  • OAuth endpoints are on www.dropbox.com.

The Dropbox API definition might look like this:

openapi: 3.0.0
info:
  title: Dropbox API
  version: 1.0.0

servers:
  - url: 'https://api.dropboxapi.com/2'

paths:
  # These endpoints are on api.dropboxapi.com (use global `servers`)
  /file_requests/list:
    ...
  /users/get_account:
    ...

  /files/upload:
    # File upload/download uses another target server
    servers:
      - url: 'https://content.dropboxapi.com/2'
    ...

  /files/list_folder/longpoll:
    # Longpolling uses another target server
    servers:
      - url: 'https://notify.dropboxapi.com/2'
    ...


Check out the API Host and Base Path guide for more details and examples.

like image 91
Helen Avatar answered Sep 19 '22 09:09

Helen