Skip to main content

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
ParameterTypeDescription
documentApiDocumentThe OPRA API document to convert. Must have api.transport === 'http'.
optionsOptionsOptional. See below.

Throws TypeError if document.api.transport is not 'http'.

Options​

OptionTypeDefaultDescription
version'3.0' | '3.1''3.0'Target OpenAPI spec version. Produces openapi: '3.0.3' or openapi: '3.1.0'.
scopestringundefinedOnly 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​

FieldTypeDescription
documentOpenApi.DocumentThe generated OpenAPI document.
warningsstring[]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 / SEARCH methods — 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 DataType kinds — mapped to an empty schema {}.

See also​