This package provides the TypeSpec emitter to produce OpenAPI 3.0 output from TypeSpec source.
In your typespec project root
npm install @typespec/openapi3
- Via the command line
tsp compile . --emit @typespec/openapi3
- Via the config
Add the following to the tspconfig.yaml
file.
emitters:
@typespec/openapi3: true
For configuration see options
import "@typespec/openapi3";
using OpenAPI;
// Using `using`
@useRef("common.json#/components/schemas/Foo")
model Foo {}
// Using fully qualified names
@OpenAPI.oneOf
union MyUnion {
cat: Cat,
dog: Dog,
}
Syntax:
@useRef(urlString)
@useRef
@useRef
is used to replace the TypeSpec model type in emitter output with a pre-existing named OpenAPI schema.
Syntax:
@oneOf()
@oneOf
emits oneOf
keyword for a union type in the resulting OpenAPI 3.0 specification. It indicates that the value of union type can only contain exactly one of the subschemas.
@oneOf
can only be applied to a union types.
Emitter options can be configured via the tspconfig.yaml
configuration:
emitters:
'@typespec/openapi3':
<optionName>: <value>
# For example
emitters:
'@typespec/openapi3':
outputFile: my-custom-openapi.json
or via the command line with
--option "@typespec/openapi3.<optionName>=<value>"
# For example
--option "@typespec/openapi3.output-file=my-custom-openapi.json"
Configure the name of the swagger output file relative to the compiler output-path
.
Set the newline character for emitting files. Can be either:
lf
(Default)crlf
Only include types referenced via an operation.