OpenApiDocumentFactory
Generates OpenAPI 3.0 or 3.1 documents from an OPRA ApiDocument. The ApiDocument's api.transport must be 'http'.
import { OpenApiDocumentFactory } from '@opra/openapi';
generate(document, options?)
Returns the OpenAPI document as a plain JavaScript object.
static generate(
document: ApiDocument,
options?: OpenApiDocumentFactory.Options,
): OpenApi.Document
| Parameter | Type | Description |
|---|---|---|
document | ApiDocument | The OPRA API document to convert. Must have api.transport === 'http'. |
options | Options | Optional. See below. |
Throws TypeError if document.api.transport is not 'http'.
Options
| Option | Type | Default | Description |
|---|---|---|---|
version | '3.0' | '3.1' | '3.0' | Target OpenAPI spec version. Produces openapi: '3.0.3' or openapi: '3.1.0'. |
scope | string | undefined | Only include types, fields, and parameters that pass .inScope(scope). |
Example
const doc = OpenApiDocumentFactory.generate(apiDocument, { version: '3.1' });
console.log(JSON.stringify(doc, null, 2));
generateWithWarnings(document, options?)
Same as generate() but also returns the list of warnings collected during generation.
static generateWithWarnings(
document: ApiDocument,
options?: OpenApiDocumentFactory.Options,
): OpenApiDocumentFactory.GenerateResult
GenerateResult
| Field | Type | Description |
|---|---|---|
document | OpenApi.Document | The generated OpenAPI document. |
warnings | string[] | OPRA constructs with no OpenAPI equivalent that were skipped or approximated. |
Example
const { document, warnings } = OpenApiDocumentFactory.generateWithWarnings(apiDocument);
for (const w of warnings) console.warn(w);
What triggers a warning
QUERY/SEARCHmethods — not valid OpenAPI path item operations; the operation is skipped.- Duplicate operations — two operations resolving to the same
METHOD /path; only the first is kept. - Unknown
DataTypekinds — mapped to an empty schema{}.