TypeSpec For OpenAPI Developer
This guide is an introduction to TypeSpec using concepts that will be familiar to developers that either build or use API definitions in OpenAPI v2 or v3.
In many cases, this will also describe how the typespec-autorest and openapi3 emitters translate TypeSpec designs into OpenAPI.
The document is organized around the features of an OpenAPI v2 or v3 definition. The idea is that if you know how to describe some API feature in OpenAPI, you can just navigate to the section of this document for that feature.
Data Types
In OpenAPI v2/v3, data types are specified using the type
and format
fields in a schema.
The TypeSpec equivalent of OpenAPI data types are the TypeSpec primitive types or built-in models.
type and format
The following table shows how common OpenAPI types map to TypeSpec types:
type: | format: | TypeSpec type | Notes |
---|---|---|---|
integer | int32 | int32 | |
integer | int64 | int64 | |
number | float | float32 | |
number | double | float64 | |
string | string | ||
string | byte | bytes | for content-type == ‘application/json’ or ‘text/plain’ |
string | binary | bytes | for “binary” content types, e.g. ‘application/octet-stream’, ‘image/jpeg’ |
string | date | plainDate | |
string | date-time | utcDateTime | RFC 3339 date in coordinated universal time (UTC) |
string | date-time | offsetDateTime | RFC 3339 date with offset |
string | password | @secret string | |
boolean | boolean |
You can also define a property with no type specified using the TypeSpec unknown
type.
OpenAPI allows any string as a format, and there is a registry of common formats. TypeSpec supports some of these directly.
type: | format: | TypeSpec type | Notes |
---|---|---|---|
number | decimal | decimal | |
number | double-int | safeint | |
integer | int8 | int8 | |
integer | int16 | int16 | |
integer | uint8 | uint8 | |
string | uri | url |
For formats that are not supported directly, you can use the built-in @format
decorator to specify
the format explicitly.
JSON Schema assertions
OpenAPI supports a variety of “assertions” that can be used further restrict the values allowed for a data type. These are actually borrowed into OpenAPI from JSON Schema.
For type: integer
and type: number
data types:
OpenAPI/JSON Schema keyword | TypeSpec construct | Notes |
---|---|---|
minimum: value | @minValue(value) decorator | |
maximum: value | @maxValue(value) decorator |
For type: string
data types:
OpenAPI/JSON Schema keyword | TypeSpec construct | Notes |
---|---|---|
minLength: value | @minLength(value) decorator | |
maxLength: value | @maxLength(value) decorator | |
pattern: regex | @pattern(regex) decorator |
For type: array
data types:
OpenAPI/JSON Schema keyword | TypeSpec construct | Notes |
---|---|---|
minItems: value | @minItems(value) decorator | |
maxItems: value | @maxItems(value) decorator |
enum
There are two ways to define an enum
data type. One is with the TypeSpec enum
statement, e.g.:
Another is to use the union operation to define the enum values inline, e.g.:
default
A model property that specifies a default value using ”=” will produce a default
field in the schema for this property.
produces
Host / BasePath / Servers
In OpenAPI v2, the host
and basePath
fields at the top-level of the API definition combine to form the base URL for the API. The paths defined in the paths
object are appended to this base URL to form the absolute URL for an operation.
In OpenAPI v3, the top-level servers
field specifies an array of server
objects [v3] with a base URL, which may be parameterized, to which the operation path is appended.
In TypeSpec, the host
in OpenAPI v2 or servers
in OpenAPI v3 can be specified with the @server
decorator
on the namespace (from @typespec/http
library). You can use this decorator multiple times to specify multiple servers.
Paths Object
In OpenAPI, the paths
object [v2, v3] is the top-level structure for defining the operations of the API, organized with the “path” for the operation.
In TypeSpec, you can specify the path for a namespace, interface, or operation using the @route
decorator.
When the value of the @route
decorator contains path parameters, operations within the scope of the decorator
must declare parameters with the same name and type. If an operation declares a path parameter that is not present
in the route, this defines a new path that is the value from the @route
decorator with the path parameter appended.
When the @route
decorator is used within a namespace or interface that also has a @route
decorator, the path is
obtained by concatenating the routes.
Path Item Object
In OpenAPI, a path item object [v2, v3] describes the operations available on a single path. A path may have at most one get
, put
, post
, patch
, delete
, or head
operation.
In TypeSpec, operations are defined within a namespace or interface with a syntax similar to typescript functions.
The HTTP method for an operation can be specified explicitly using a decorator: @get
, @put
, @post
, @patch
, @delete
, or @head
.
If an HTTP method decorator is not specified then the default is post
if there is a body and get
otherwise.
Other path item fields:
OpenAPI pathItem field | TypeSpec construct | Notes |
---|---|---|
summary | Not currently supported. | |
description | Not currently supported. | |
parameters | Not currently supported. |
Operation Object
In OpenAPI, an operation object [v2, v3] describes an operation.
The fields in an OpenAPI operation object are specified with the following TypeSpec constructs:
OpenAPI operation field | TypeSpec construct | Notes |
---|---|---|
tags | @tag decorator | |
summary | @summary decorator | |
description | @doc decorator or doc comment | |
externalDocs | @externalDocs decorator | |
operationId | operation name or @operationId decorator | |
parameters | op parameter list | see Parameter Object |
requestBody | parameter with @body decorator | see Request Body Object |
responses | op return type(s) | see Responses Object |
callbacks | Not currently supported. | |
deprecated | #deprecated directive | |
security | see Security Schemes Object. | |
servers | @server decorator | Can be specified multiple times. |
Tags
Tags can be specified using the @tag
decorator on an operation.
The @tag
decorator can also be used on a namespace or interface to specify tags for all operations within the namespace or interface.
Tags are additive, so tags specified on an operation are added to the tags specified on the namespace or interface.
The @tag
decorator can be used multiple times to specify multiple tags on an operation, namespace, or interface.
Description
Use the @doc
decorator to specify the description for an operation. The value of the @doc
decorator can be a multi-line string
and can contain markdown formatting.
You can also use a “doc comment” to specify the description for an operation. A doc comment is a comment that begins with /**
.
Doc comments may be spread across multiple lines and may contain markdown formatting.
See documentation doc for more information.
operationId
You can specify the operationId for an operation using the @operationId
decorator.
When the @operationId
decorator is not specified, the operationId is generated from the operation name.
For an operation defined in the top-level namespace, the operationId is the just operation name.
If the operation is defined within a inner namespace or interface, then the operationId is
prefixed with the name of the innermost namespace or interface name.
Note: this approach will generally produce unique operationIds, as required by OpenAPI, but it is possible to create duplicate operationIds.
Parameter Object
In OpenAPI, a parameter object [v2, v3] describes a single operation parameter.
The following fields of a parameter object are common to both OpenAPI v2 and v3:
OpenAPI parameter field | TypeSpec construct | Notes |
---|---|---|
name | parameter name | |
in | decorator | @query , @path , @header , @body |
description | /** */ or @doc decorator | |
required | from parameter “optionality” | a ”?” following the parameter name indicates it is optional (required: false ), otherwise it is required (required: true ) |
allowEmptyValue | Not supported, this field is NOT RECOMMENDED in OpenAPI. |
OpenAPI v2
The following fields of a parameter object are specific to OpenAPI v2:
OpenAPI v2 parameter field | TypeSpec construct | Notes |
---|---|---|
type | parameter type | see Data Types |
collectionFormat | uri template in @route or expode, style |
Collection Formats
In OpenAPI v2, the collectionFormat
field of a query or header parameter object specifies how multiple values are delimited.
You can use the format
field of the @query
or @header
decorator to specify the collection format.
OpenAPI v3
The following fields of a parameter object are specific to OpenAPI v3:
OpenAPI v3 parameter field | TypeSpec construct | Notes |
---|---|---|
style | format parameter on @query or @header | |
explode | format parameter on @query or @header | |
schema | parameter schema | see Schema Object |
deprecated | #deprecated directive. | |
example | Not currently supported. | |
examples | Not currently supported. | |
content | Not currently supported. |
Request Body Object (OAS3)
In OpenAPI v3, the operation request body is defined with a Request Body object rather than as a parameter.
An OpenAPI v3 Request Body object corresponds to a TypeSpec op
parameter with the @body
decorator.
OpenAPI requestBody field | TypeSpec construct | Notes |
---|---|---|
description | @doc decorator | |
required | parameter “optionality” | a ”?” following the parameter name indicates it is optional (required: false ), otherwise it is required (required: true ) |
content | @body parameter type |
The media type of the request body is specified with a content-type
header. If content-type
has multiple values
then content
will have one entry for each value.
To get multiple content
entries with different schemas (say one structured and one binary),
you need to define two separate operations that share the same path and method.
You do with with the @sharedRoute
decorator.
Responses Object
In OpenAPI, the responses object [v2, v3] specifies the possible responses for an operation. The responses object maps a HTTP response code to the expected response.
In TypeSpec, operation responses are defined by the return types of the op
. The status code for a response can be specified as a property in the return type with the @statusCode
decorator. The value of the property with the @statusCode
decorator should be an HTTP status code or union of status codes. When the value is a union of status codes, a response is generated for each status code in the union.
If a return type does not contain a statusCode
, the default is 200
except for void
which defaults to 204
.
To get the default
response, specify the @error
decorator on the return type model.
The TypeSpec.Http package also defines several standard response types.
HTTP Status Code | TypeSpec construct |
---|---|
200 | OkResponse |
201 | CreatedResponse |
202 | AcceptedResponse |
204 | NoContentResponse |
301 | MovedResponse |
304 | NotModifiedResponse |
401 | UnauthorizedResponse |
404 | NotFoundResponse |
409 | ConflictResponse |
You can intersect these standard response types with your own response types.
Response Object
In OpenAPI, a response object [v2, v3] describes a single response for an operation. The structure of the response object changed significantly from OpenAPI v2 to OpenAPI v3, but there are many elements common to both.
The fields in an OpenAPI response object are specified with the following TypeSpec constructs:
OpenAPI response field | TypeSpec construct | Notes |
---|---|---|
description | @doc decorator | |
headers | fields in the return type with @header decorator | required or optional based on optionality of field |
schema (OAS2) | return type or type of `@body“ property | |
content (OAS3) | return type or type of `@body“ property | |
examples (OAS3) | Not currently supported. | |
links (OAS3) | Not currently supported. |
The media type of the request body is specified with a content-type
header. If content-type
has multiple values
then content
will have one entry for each value.
To get multiple content
entries with different schemas, use a union type.
Schema Object
OpenAPI schemas are represented in TypeSpec by models. Models have any number of members and can extend and be composed with other models.
Models can be defined with the model
statement and then referenced by name, which generally results in a $ref
to a schema for the model in the definitions
or components.schemas
section of the OpenAPI document.
TypeSpec supports the “spread” operator (...
), which copies the members of the source model into the target model.
But TypeSpec processes all spread transformations before emitters are invoked, so this form of reuse is not represented in the emitted OpenAPI.
The spread operation is useful if you want one or more properties to be present in several different models but in a standard fashion. For example:
additionalProperties
You can generate a schema with additionalProperties
with the TypeSpec Record
construct.
is produced as
To get a schema having both properties
and additionalProperties
, define a model that extends Record<unknown>
.
produces
To define a schema with additionalProperties
that has a specific type, use the Record
construct with a type parameter.
results in
allOf and polymorphism using allOf
TypeSpec also supports single inheritance of models with the extends
keyword. This construct can be used to produce an allOf
with a single element (the parent schema) in OpenAPI. For example:
TypeSpec does not currently provide a means to produce an allOf
with more than one element — these are generally treated as “composition” in code generators and thus better represented in TypeSpec with the spread operator.
Models with a @discriminator
decorator can be extended to produce polymorphic schemas in either OpenAPI v2 or v3 using allOf
.
This schema produced for the base model will be defined with a discriminator
property and schemas for the child models will allOf
the base schema and add additional properties.
For example:
generates:
Polymorphism using anyOf and oneOf (OAS3)
Polymorphism can also be represented in OpenAPI v3 with anyOf
or oneOf
constructs.
These can be represented in TypeSpec with a union type.
generates a Pet schema with anyOf
.
The openapi emitter uses anyOf
by default because the schemas may not be mutually exclusive.
But the @oneOf
decorator of the OpenAPI library can be used to force the use of oneOf
instead.
produces:
To make Pet a discriminated union, add the @discriminator
decorator and add the discriminator property
with a string literal value to each of the child schemas.
results in the following schema for Pet:
definitions / components
OpenAPI supports reuse of schemas, parameters, responses, and other elements with the definitions
(OAS2) or components
(OAS3) section of an OpenAPI definition.
Referencing a model by name (not with “spread”), as an op
parameter or return type or as the type of a property in another model, generally results in a $ref
to a schema for the model in the definitions
or components.schemas
section of the OpenAPI document.
Reusable parameters can be defined as members of a model and then incorporated into an operation parameter list using the spread operator. For example:
results in a $ref
to the named parameter PetId
in either parameters
or components.parameters
.
Info Object
In OpenAPI, the info
object [v2, v3] contains metadata about the API such as a title
, description
, license
, and version
.
In TypeSpec this information is specified with decorators on the namespace.
OpenAPI info field | TypeSpec decorator | Notes |
---|---|---|
title | @service({title: } | TypeSpec built-in decorator |
description | @doc | TypeSpec built-in decorator |
version | @info | |
license | @info | |
contact | @info |
Consumes / Produces (OAS2)
In OpenAPI v2, the top-level consumes
and produces
fields specify a list of MIME types an operation can consume / produce
when not overridden by a consumes
or produces
on an individual operation.
The typespec-autorest emitter previously supported @produces
and @consumes
decorators on a namespace, but these are deprecated
in favor of explicit content-type
and accept
header properties in request and response bodies.
securityDefinitions / securitySchemes Object
Use @useAuth
decorator from the `@typespec/rest” library
Specification Extensions
You can add arbitrary specification extensions (“x-” properties) to a model or an operation with the @extension
decorator.
For example:
OpenAPI decorators that map directly to an object in the openapi document also allow to provide extension.
@tagMetadata
@info