Skip to content

Files

Latest commit

13371f9 · Jun 12, 2023

History

History
This branch is 1856 commits behind microsoft/typespec:main.

openapi3

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
May 25, 2023
Jun 12, 2023
Jun 2, 2023
Jan 28, 2022
Feb 16, 2023
Jul 18, 2022
Jun 6, 2023
Jun 6, 2023
Aug 27, 2021
Feb 16, 2023
Jul 18, 2022
Jun 9, 2023
Feb 24, 2022

TypeSpec OpenAPI 3.0 Emitter

This package provides the TypeSpec emitter to produce OpenAPI 3.0 output from TypeSpec source.

Install

In your typespec project root

npm install @typespec/openapi3

Emit OpenAPI 3.0 spec

  1. Via the command line
tsp compile . --emit @typespec/openapi3
  1. Via the config

Add the following to the tspconfig.yaml file.

emitters:
  @typespec/openapi3: true

For configuration see options

Use OpenAPI 3.0 specific decorators:

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,
}

Decorators

@useRef

Syntax:

@useRef(urlString)

@useRef

@useRef is used to replace the TypeSpec model type in emitter output with a pre-existing named OpenAPI schema.

@oneOf

Syntax:

@oneOf()

@oneOfemits 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:

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"

output-file

Configure the name of the swagger output file relative to the compiler output-path.

new-line

Set the newline character for emitting files. Can be either:

  • lf(Default)
  • crlf

omit-unreachable-types

Only include types referenced via an operation.

See also