Import: OpenAPI
Creates a data contract from the responses of GET or POST operations in an OpenAPI 3.0 or 3.1 document, in YAML or JSON. You can then test the endpoints against the contract.
datacontract import openapi --source openapi.yaml --operation listOrders
What is imported
- Operation: select the operation with
--operation, by itsoperationId(listOrders), its method and path (GET /orders,POST /orders/search), or the path of a GET operation (/orders). Repeat--operationto import several operations. In a terminal,--operationcan be omitted to pick the operations from a list of those with an importable response. Otherwise, if the document has only one GET operation,--operationcan be omitted. A POST operation is never selected by its path alone, because a POST may change data: import only POST operations that read, such as a search. - Schema: the contract gets one schema per operation, named after the
operationId(or the path), described by the operation'ssummaryordescription. Its properties come from the response schema of the first success response (200first, then other2xxcodes) with a JSON (application/json,application/*+json) or YAML (application/yaml,text/yaml) body. - Arrays: if the response is an array, the schema describes its items, which are the records.
- Types: the response schema is mapped like a JSON Schema. Local
$refs (to#/components/...) are resolved andallOfis merged. OpenAPI 3.0'sexampleis imported asexamples. A property that admitsnull(nullable: true, ortype: [string, "null"]) is notrequired, as ODCSrequiredmeans not null. - Servers: every absolute
serversURL becomes anapiserver whoselocationis the endpoint's URL. Server variables take theirdefault. With several operations, thelocationis the server URL, and each schema has anendpointcustom property with its path, and its ownmethod,paramsandbody. Relative server URLs (such as/v1) are skipped with a warning, because they cannot be tested. - Parameters: path parameters become variables in the
location(with several operations, in the schema'sendpoint), and required query parameters variables in theparamscustom property, defaulting to the parameter'sexampleordefault. For example,/orders/{orderId}withexample: ORD-1001becomes/orders/${orderId:-ORD-1001}. SetorderIdin the environment to test another order. - Request body: for a POST operation, the server (with several operations, the schema) gets
method: POSTand abodycustom property with the request body's JSON example: the media type'sexample, else its first entry inexamples, else the schema'sexample. Without one, the contract has nobodyand a warning asks you to add it.
Swagger 2.0 documents are not supported, and $refs to other files are not followed.
Example
Given this orders.yaml:
openapi: 3.1.0
info:
title: Orders API
version: 1.0.0
servers:
- url: https://api.example.com/v1
description: Production
- url: https://staging.api.example.com/v1
description: Staging
paths:
/orders:
get:
operationId: listOrders
summary: All orders of the last 30 days.
responses:
"200":
description: The orders.
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Order"
/orders/{orderId}:
get:
operationId: getOrder
parameters:
- name: orderId
in: path
required: true
example: ORD-1001
schema:
type: string
responses:
"200":
description: The order.
content:
application/json:
schema:
$ref: "#/components/schemas/Order"
components:
schemas:
Order:
type: object
required: [order_id, order_timestamp, status]
properties:
order_id:
type: string
description: Unique identifier of the order.
pattern: "^ORD-[0-9]+$"
order_timestamp:
type: string
format: date-time
customer_id:
type: string
order_total:
type: integer
minimum: 0
status:
type: string
enum: [placed, shipped, delivered]
Run:
datacontract import openapi --source orders.yaml --operation listOrders
to produce the data contract:
version: 1.0.0
kind: DataContract
apiVersion: v3.2.0
id: my-data-contract
name: Orders API
status: draft
servers:
- server: production
type: api
description: Production
location: https://api.example.com/v1/orders
- server: staging
type: api
description: Staging
location: https://staging.api.example.com/v1/orders
schema:
- name: listOrders
physicalType: object
description: All orders of the last 30 days.
logicalType: object
physicalName: listOrders
properties:
- name: order_id
physicalType: string
description: Unique identifier of the order.
logicalType: string
logicalTypeOptions:
pattern: ^ORD-[0-9]+$
required: true
- name: order_timestamp
physicalType: string
logicalType: string
logicalTypeOptions:
format: date-time
# …
All options: datacontract import openapi.