Importing OpenAPI
An OpenAPI or Swagger spec becomes a collection of ready to send requests: one .postino file per operation, an environment per server, auth variables kept out of git, example bodies and a status code test. A three step wizard shows everything before anything is written, and the import never overwrites a file.
Supported specs
- OpenAPI 3.1 and 3.0, and Swagger 2.0, which is read into the same model.
- YAML or JSON, detected from the text itself, not from the file extension.
$refreferences inside the spec, and to other files or URLs:schemas/pet.yamlor../common.yaml#/components/schemas/Errorare read relative to the spec. A reference that cannot be read becomes a warning, and the rest of the spec still imports.
Opening the wizard
- Dropping a file anywhere on the window, which opens on the Options step.
- The Import menu in the sidebar header: OpenAPI / Swagger….
- Import OpenAPI… in the welcome tab.
- The command palette: Import OpenAPI spec…, or Import OpenAPI from URL…, which opens on the URL source.
- The shortcut Ctrl Shift O (⌘ Shift O on macOS).
- The command palette's Update from OpenAPI: <collection>, one per imported collection, which reads its source again to update it.
Step 1: Source
The Format chips choose between OpenAPI, Postman and Bruno, and what is read decides in the end: a Postman export switches the wizard to Postman, a Bruno collection to Bruno. Five sources, chosen with the buttons below them:
- File: drop a spec file on the dashed area, or click it to pick one. External references are read from the disk, relative to the file. A
catalog-info.yamlbrings in the specs it references. - URL: type the address of the spec and click Download, or press Enter to download it and go on. The download uses the same TLS settings as requests, and external references are fetched relative to the URL. Relative server URLs such as
/v2are resolved against it too. - Paste text: paste the spec. Pasted text has no location, so external references cannot be read.
- Folder: Choose… picks a folder, and every spec in it and its subfolders is read: the YAML and JSON files whose first lines have an
openapiorswaggerkey, so schema fragments are left out, plus the specs of everycatalog-info.yaml. Hidden folders andnode_modules,target,vendor,distandbuildare skipped. The scan goes 8 folders deep and looks at 5,000 files at most, of up to 32 MB each. A spec found both on its own and through a catalog is read once. - Git repository: type the repository (
https://…,git@host:owner/repo.gitor a local path) and, optionally, a branch, then click Clone or press Enter. Only the last commit of the branch (the default branch when the field is empty) is downloaded, with the git Postino uses and your own credentials, into a temporary folder that is deleted when the wizard closes. The specs are then found as in a folder.
A spec that was read shows under Found, with its title, version, format and number of operations, and the number of warnings when there are any. A catalog file shows above the list with the number of specs it references. A file that looked like a spec but could not be read is listed in red with the reason, and the others still import. A text that is not a spec, or a folder without any, shows the reason in red and the wizard stays on this step.
Backstage catalogs
A catalog-info.yaml of Backstage describes the APIs of a repository. Every entity of kind API and type openapi is read, in every YAML document of the file; other kinds and API types (AsyncAPI, GraphQL, gRPC) are skipped. Its spec.definition can be the spec itself, or a $text, $yaml or $json substitution with a path relative to the catalog file or a URL:
apiVersion: backstage.io/v1alpha1
kind: API
metadata:
name: payments-v2
spec:
type: openapi
definition:
$text: ./specs/payments-v2.yamlStep 2: Options
- Destination: the workspace the collection goes in. See Where the collection goes.
- Collection name: the name of the new folder, the spec's title by default (the first spec's, with several).
- Versions: shown when the source gave several specs, each with its file, format and number of operations. Unchecked ones are not imported. Each spec goes in a folder of its own inside the collection:
v1,v2after the major version ofinfo.versionwhen they all differ, else the spec titles when they differ, else the file names. - Folders: Path segments puts
/customers/{id}in the foldercustomers/{id}; Tags puts each operation in the folder of its first tag, and untagged operations at the top of the collection.
What to generate, every option on by default:
| Option | What it writes |
|---|---|
| Environment from servers | One environment per server origin, with baseUrl set to it (https://api.acme.dev). The server path (/v2) stays in the request URLs. With one origin the environment is named after the collection; with several, each one is named after the server description, or its host when the description is missing or long. A name that is taken gets a (2) suffix. Without an absolute server URL, baseUrl is http://localhost, with a warning. Turned off, the requests use the first server URL as written. |
| Auth variables from securitySchemes | The auth of each request, and one empty variable per scheme in environments/<name>.local.env, a file kept out of git. |
| Example bodies | The body from the spec's example, or one built from the schema. Turned off, the body keeps its type and is empty. |
| Status code tests | A ::: post script with one test() asserting the first 2xx response the spec documents. |
| Request documentation | The ::: docs section: summary, description and the list of parameters. |
Step 3: Preview
The tree of folders and requests that will be written, every request checked. A folder's checkbox checks or unchecks everything under it, a row opens a folder or toggles a request, and Select all and None act on the whole tree. The line under the tree tells which environments and auth variables are created. Import writes the checked requests; folders left without one are not created.
The last screen tells how many requests were imported and lists the warnings. The new collection opens in the sidebar and its environments appear in the environment picker.
Where the collection goes
The Destination block of the Options step says where the collection is written. Nothing is written until Import in the Preview step: cancelling leaves nothing on disk, and an import that fails removes what it created.
- With no workspace open (a first run, for example), a new workspace is proposed: the name is the title of the spec in lowercase with hyphens, in
Documents/Postino. Both can be changed, or Open existing folder... uses a folder you pick. Initialize a Git repository is on when Git is available. The first import is Next, Next, Create workspace and import. - With a workspace open, it is the default, and the collection is added as a folder inside it. Another recent workspace, New workspace and Open existing folder... send it somewhere else.
When the source is a folder, Use the source folder as the workspace writes the collection there, next to the files it came from, instead of copying anything elsewhere. A workspace other than the open one opens when the import finishes. The folder is checked as soon as it is chosen:
| Case | What Postino offers |
|---|---|
| The new workspace's folder exists with other files | Use a free name (acme-payments-api-2), or choose another folder |
| The folder already is a Postino workspace | Add the collection to it, or choose another folder |
| A picked folder has other files | Use it anyway, which only adds the environments and the collection, or choose another folder |
| The location cannot be written | Choose another folder, or go back to Documents/Postino |
Updating an imported collection
Every import leaves a small .openapi.toml file in the collection folder. It records the source, the day and the options of the import, and for each request its file and what the spec generated: method, URL, parameter and header names and the example body. The sidebar hides it; commit it with the requests, so everyone working on the collection can update it. It goes with the folder when the collection is moved or renamed, and the collection can still be updated from inside another folder.
When a source that was already imported is read again (the same file, folder, URL, or repository and branch; pasted text never matches), the wizard takes the options of the earlier import and the Preview step shows that collection's update instead of a new tree:
- A line tells which collection was imported from this source, and when.
- Update existing or Import as new collection; the second shows the usual tree.
- The counts of added, changed, removed and unchanged operations, then one row per change: Added, Changed (with what changed, such as
query: +created_afterorbody: +phone) and Removed. Added and changed ones start checked, removed ones unchecked. A click on a row checks or unchecks it.
Update applies the checked changes:
- Added: a new request, in the folder the options put it in.
- Changed: the request file is updated in place. The method and URL come from the spec. Parameters and headers the spec added are appended and those it dropped are removed, while the values set on the others and the entries added by hand stay. The body takes the new example only when it was not edited since the import. Auth, scripts, tests and docs are kept.
- Removed: the request file is deleted, and its folder too when nothing else is left in it. An unchecked removed request stays as an ordinary request, no longer tracked, so it is not offered again.
An unchecked change is offered again the next time. Environments are not touched by an update. A spec with nothing new shows that the collection is up to date.
How a spec maps to requests
| OpenAPI | Postino |
|---|---|
| Operation | A .postino file, named after its summary, else its operationId, else METHOD /path |
Path parameter {id} | The variable {{id}} in the URL |
| Query parameter | The ::: query section, with the example or default value. Optional ones are disabled (#) |
| Header parameter | A header, disabled when optional. Accept, Content-Type and Authorization are skipped, as OpenAPI says |
| Cookie parameter | A Cookie header |
JSON body (application/json, *+json) | ::: body json, with a Content-Type header when the type is not application/json |
| URL encoded form body | ::: body form |
| Multipart body | ::: body multipart; a file field needs its @path filled in |
| XML and text bodies | ::: body xml or text, with their Content-Type |
| HTTP basic | Basic auth with {{<scheme>Username}} and {{<scheme>Password}} |
| HTTP bearer | Bearer auth with {{<scheme>}} |
| OAuth 2 | An OAuth 2.0 profile named after the scheme, from its first flow (authorization code, client credentials, password; implicit becomes authorization code with PKCE), with its URLs and every scope it lists. The client ID and secret are variables named after the scheme, left empty in .local.env |
| OpenID Connect | An authorization code profile with the scope openid, and a warning: fill its URLs from the discovery document |
| API key (header, query or cookie) | API key auth, or a Cookie header, with {{<scheme>}} |
security: [] on an operation | No auth |
Variable names come from the scheme names, with anything but letters, digits and _ replaced by _: the scheme bearerAuth becomes {{bearerAuth}}. An operation that needs several schemes at once gets the first as its auth and the API keys of the others as headers or query entries.
A body generated from a schema takes the first value that exists among example, examples, default, const and enum, then falls back to the type: "string" (or a sample value for formats such as date-time, email or uuid), 0 or the minimum, true, and one item for arrays. allOf merges its parts, oneOf and anyOf take the first, and read only properties are left out. Recursive schemas stop instead of repeating.
A GET or HEAD operation whose body would be empty gets no body: some specs declare one by mistake.
Names are made safe for every operating system as in the Postman import, and two requests with the same name in one folder get a (2) suffix.
Example
This operation:
paths:
/customers:
post:
summary: Create customer
requestBody:
content:
application/json:
example: {name: Ada}
responses:
'201': {description: Created}with a server https://api.acme.dev/v2 and a global bearer scheme named bearerAuth becomes customers/Create customer.postino:
POST {{baseUrl}}/v2/customers
::: auth bearer
token = {{bearerAuth}}
::: body json
{
"name": "Ada"
}
::: post
test("status is 201", () => {
expect(res.status).toBe(201);
});
::: docs
# Create customerFrom the command line
postino import openapi <source> runs this import with the wizard's default choices, from a file, a folder, a URL or a catalog, and updates the collection when the same source was imported before. See the command line reference.
After importing
- Fill the auth variables in the environment, in the Environments panel.
- Define the path variables (
{{id}}) in the environment, or replace them in the URL. - Read the warnings: they name every scheme or body that needs attention.