2019-05-30 08:34:05 +02:00
|
|
|
# OpenAPI configuration
|
2017-02-24 03:39:44 +01:00
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
[OpenAPI][openapi-spec] is a popular format for describing an API. An
|
2020-06-21 04:03:51 +02:00
|
|
|
OpenAPI file can be used by various tools to generate documentation
|
|
|
|
for the API or even basic client-side bindings for dozens of
|
|
|
|
programming languages.
|
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
Zulip's API is described in `zerver/openapi/zulip.yaml`. Our aim is
|
2020-06-21 04:03:51 +02:00
|
|
|
for that file to fully describe every endpoint in the Zulip API, and
|
|
|
|
for the Zulip test suite to fail should the API every change without a
|
2021-08-20 21:53:28 +02:00
|
|
|
corresponding adjustment to the documentation. In particular,
|
2020-06-21 04:03:51 +02:00
|
|
|
essentially all content in Zulip's [REST API
|
2022-02-24 00:17:21 +01:00
|
|
|
documentation](api.md) is generated from our OpenAPI
|
2020-06-21 04:03:51 +02:00
|
|
|
file.
|
|
|
|
|
|
|
|
In an OpenAPI Swagger file, every configuration section is an object.
|
|
|
|
Objects may contain other objects, or reference objects defined
|
2017-02-24 03:39:44 +01:00
|
|
|
elsewhere. Larger API specifications may be split into multiple
|
2020-06-21 04:03:51 +02:00
|
|
|
files. See the [OpenAPI specification][openapi-spec].
|
|
|
|
|
|
|
|
[openapi-spec]: https://swagger.io/docs/specification/about/
|
2017-02-24 03:39:44 +01:00
|
|
|
|
|
|
|
This library isn't in production use yet, but it is our current plan
|
|
|
|
for how Zulip's API documentation will work.
|
|
|
|
|
|
|
|
## Working with the `zulip.yaml` file
|
|
|
|
|
2020-06-21 04:03:51 +02:00
|
|
|
An OpenAPI specification file has three general parts: information and
|
2017-02-24 03:39:44 +01:00
|
|
|
configuration, endpoint definitions, and object schemas referenced by
|
|
|
|
other objects (as an alternative to defining everything inline.)
|
|
|
|
References can either specify an individual object, using `$ref:`, or
|
|
|
|
compose a larger definition from individual objects with `allOf:`
|
|
|
|
(which may itself contain a `$ref`.)
|
|
|
|
|
|
|
|
### Configuration
|
|
|
|
|
|
|
|
These objects, at the top of `zulip.yaml`, identify the API, define
|
|
|
|
the backend host for the working examples, list supported schemes and
|
|
|
|
types of authentication, and configure other settings. Once defined,
|
|
|
|
information in this section rarely changes.
|
|
|
|
|
|
|
|
For example, the `swagger` and `info` objects look like this:
|
2021-08-20 22:54:08 +02:00
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```yaml
|
2017-02-24 03:39:44 +01:00
|
|
|
# Basic Swagger UI info
|
2020-01-28 23:29:53 +01:00
|
|
|
openapi: 3.0.1
|
2017-02-24 03:39:44 +01:00
|
|
|
info:
|
2020-01-28 23:29:53 +01:00
|
|
|
version: 1.0.0
|
2017-02-24 03:39:44 +01:00
|
|
|
title: Zulip REST API
|
2020-01-28 23:29:53 +01:00
|
|
|
description: Powerful open source group chat.
|
2017-02-24 03:39:44 +01:00
|
|
|
contact:
|
2020-06-08 23:04:39 +02:00
|
|
|
url: https://zulip.com
|
2017-02-24 03:39:44 +01:00
|
|
|
license:
|
|
|
|
name: Apache 2.0
|
|
|
|
url: https://www.apache.org/licenses/LICENSE-2.0.html
|
|
|
|
```
|
|
|
|
|
|
|
|
### Endpoint definitions
|
|
|
|
|
2020-03-27 01:32:21 +01:00
|
|
|
The [Paths Object](https://swagger.io/specification/#pathsObject)
|
2017-02-24 03:39:44 +01:00
|
|
|
contains
|
2020-03-27 01:32:21 +01:00
|
|
|
[Path Item Objects](https://swagger.io/specification/#pathItemObject)
|
2017-02-24 03:39:44 +01:00
|
|
|
for each endpoint. It describes in detail the methods and parameters
|
|
|
|
the endpoint accepts and responses it returns.
|
|
|
|
|
|
|
|
There is one Path Item Object for each supported method, containing a
|
2020-03-27 01:32:21 +01:00
|
|
|
[Parameters Definition Object](https://swagger.io/specification/#parametersDefinitionObject)
|
2017-02-24 03:39:44 +01:00
|
|
|
describing the required and optional inputs. A
|
2020-03-27 01:32:21 +01:00
|
|
|
[Response Object](https://swagger.io/specification/#responseObject)
|
2017-02-24 03:39:44 +01:00
|
|
|
similarly specifies the content of the response. They may reference
|
|
|
|
schemas from a global Definitions Object (see [Schemas](#schemas),
|
|
|
|
below.)
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
The `/users/{user}/presence` endpoint (defined in a
|
2020-03-27 01:32:21 +01:00
|
|
|
[Path Item Object](https://swagger.io/specification/#pathItemObject))
|
2017-02-24 03:39:44 +01:00
|
|
|
expects a GET request with one
|
2020-03-27 01:32:21 +01:00
|
|
|
[parameter](https://swagger.io/specification/#parameterObject), HTTP
|
2017-02-24 03:39:44 +01:00
|
|
|
Basic authentication, and returns a JSON response containing `msg`,
|
|
|
|
`result`, and `presence` values.
|
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```yaml
|
2017-02-24 03:39:44 +01:00
|
|
|
/users/{user}/presence:
|
|
|
|
get:
|
|
|
|
description: Get presence data for another user.
|
|
|
|
operationId: getPresence
|
|
|
|
parameters:
|
|
|
|
- name: user
|
|
|
|
in: path
|
|
|
|
description: Enter email address
|
|
|
|
required: true
|
|
|
|
type: string
|
|
|
|
security:
|
|
|
|
- basicAuth: []
|
|
|
|
responses:
|
|
|
|
'200':
|
|
|
|
description: The response from a successful call
|
|
|
|
schema:
|
|
|
|
type: object
|
|
|
|
required:
|
|
|
|
- msg
|
|
|
|
- result
|
|
|
|
- presence
|
|
|
|
properties:
|
|
|
|
msg:
|
|
|
|
type: string
|
|
|
|
result:
|
|
|
|
type: string
|
|
|
|
presence:
|
|
|
|
type: array
|
|
|
|
```
|
|
|
|
|
|
|
|
### Schemas
|
|
|
|
|
|
|
|
The
|
2020-03-27 01:32:21 +01:00
|
|
|
[Definitions Object](https://swagger.io/specification/#definitionsObject)
|
2017-02-24 03:39:44 +01:00
|
|
|
contains schemas referenced by other objects. For example,
|
|
|
|
`MessageResponse`, the response from the `/messages` endpoint,
|
2021-08-20 21:53:28 +02:00
|
|
|
contains three required parameters. Two are strings, and one is an
|
2017-02-24 03:39:44 +01:00
|
|
|
integer.
|
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```yaml
|
2017-02-24 03:39:44 +01:00
|
|
|
MessageResponse:
|
|
|
|
type: object
|
|
|
|
required:
|
|
|
|
- msg
|
|
|
|
- result
|
|
|
|
- id
|
|
|
|
properties:
|
|
|
|
msg:
|
|
|
|
type: string
|
|
|
|
result:
|
|
|
|
type: string
|
|
|
|
id:
|
|
|
|
type: integer
|
|
|
|
format: int64
|
|
|
|
```
|
|
|
|
|
|
|
|
You can find more examples, including GET requests and nested objects, in
|
2022-07-20 10:28:00 +02:00
|
|
|
`zerver/openapi/zulip.yaml`.
|
2017-02-24 03:39:44 +01:00
|
|
|
|
|
|
|
## Zulip Swagger YAML style:
|
|
|
|
|
|
|
|
We're collecting decisions we've made on how our Swagger YAML files
|
|
|
|
should be organized here:
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Use shared definitions and YAML anchors to avoid duplicating content
|
2017-02-24 03:39:44 +01:00
|
|
|
where possible.
|
|
|
|
|
|
|
|
## Tips for working with YAML:
|
|
|
|
|
|
|
|
You can edit YAML files in any text editor. Indentation defines
|
|
|
|
blocks, so whitespace is important (as it is in Python.) TAB
|
2021-08-20 21:53:28 +02:00
|
|
|
characters are not permitted. If your editor has an option to replace
|
2017-02-24 03:39:44 +01:00
|
|
|
tabs with spaces, this is helpful.
|
|
|
|
|
|
|
|
You can also use the
|
2020-03-27 01:32:21 +01:00
|
|
|
[Swagger Editor](https://swagger.io/swagger-editor), which validates
|
2017-02-24 03:39:44 +01:00
|
|
|
YAML and understands the Swagger specification. Download and run it
|
|
|
|
locally, or use the online version. If you aren't using a YAML-aware
|
|
|
|
editor, make small changes and check your additions often.
|
|
|
|
|
|
|
|
Note: if you are working with
|
2020-03-27 01:32:21 +01:00
|
|
|
[Swagger UI](https://swagger.io/swagger-ui/) in a local development
|
2017-02-24 03:39:44 +01:00
|
|
|
environment, it uses an online validator that must be able to access
|
|
|
|
your file. You may see a red "ERROR" button at the bottom of your API
|
|
|
|
docs page instead of the green "VALID" one even if your file is
|
|
|
|
correct.
|
|
|
|
|
|
|
|
### Formatting help:
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Comments begin with a # character.
|
2017-02-24 03:39:44 +01:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Descriptions do not need to be in quotes, and may use common
|
2017-02-24 03:39:44 +01:00
|
|
|
Markdown format options like inline code \` (backtick) and `#`
|
|
|
|
headings.
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- A single `|` (pipe) character begins a multi-line description on the
|
2021-08-20 21:53:28 +02:00
|
|
|
next line. Single spaced lines (one newline at the end of each) are
|
|
|
|
joined. Use an extra blank line for a paragraph break. We prefer
|
2020-06-21 04:03:51 +02:00
|
|
|
to use this format for all descriptions because it doesn't require
|
|
|
|
extra effort to expand.
|
2017-02-24 03:39:44 +01:00
|
|
|
|
|
|
|
### Examples:
|
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```yaml
|
2017-02-24 03:39:44 +01:00
|
|
|
Description: |
|
|
|
|
This description has multiple lines.
|
|
|
|
Sometimes descriptions can go on for
|
|
|
|
several sentences.
|
|
|
|
|
|
|
|
A description might have multiple paragraphs
|
|
|
|
as well.
|
|
|
|
```
|