@asyncapi/openapi-schema-parser
v3.1.0
Published
An AsyncAPI schema parser for OpenAPI 3.0.x and Swagger 2.x schemas.
Maintainers
Readme
OpenAPI Schema Parser
An AsyncAPI schema parser for OpenAPI 3.0.x and Swagger 2.x schemas.
Note Version >=
3.0.0of package is only supported by@asyncapi/parserversion >=2.0.0.
Note This package is maintained inside the
asyncapi/parser-jsmonorepo, underpackages/openapi-schema-parser. It is still published to npm as@asyncapi/openapi-schema-parserwith the same public API. Please open issues and pull requests in theparser-jsrepository.Package spec:
docs/openapi-schema-parser/spec.md.
Installation
npm install @asyncapi/openapi-schema-parser
// OR
yarn add @asyncapi/openapi-schema-parserUsage
import { Parser } from '@asyncapi/parser';
import { OpenAPISchemaParser } from '@asyncapi/openapi-schema-parser';
const parser = new Parser();
parser.registerSchemaParser(OpenAPISchemaParser());
const asyncapiWithOpenAPI = `
asyncapi: 2.0.0
info:
title: Example with OpenAPI
version: 0.1.0
channels:
example:
publish:
message:
schemaFormat: 'application/vnd.oai.openapi;version=3.0.0'
payload: # The following is an OpenAPI schema
type: object
properties:
title:
type: string
nullable: true
author:
type: string
example: Jack Johnson
`;
const { document } = await parser.parse(asyncapiWithOpenAPI);const { Parser } = require('@asyncapi/parser');
const { OpenAPISchemaParser } = require('@asyncapi/openapi-schema-parser');
const parser = new Parser();
parser.registerSchemaParser(OpenAPISchemaParser());
const asyncapiWithOpenAPI = `
asyncapi: 2.0.0
info:
title: Example with OpenAPI
version: 0.1.0
channels:
example:
publish:
message:
schemaFormat: 'application/vnd.oai.openapi;version=3.0.0'
payload: # The following is an OpenAPI schema
type: object
properties:
title:
type: string
nullable: true
author:
type: string
example: Jack Johnson
`;
const { document } = await parser.parse(asyncapiWithOpenAPI);It also supports referencing remote OpenAPI schemas:
import { Parser } from '@asyncapi/parser';
import { OpenAPISchemaParser } from '@asyncapi/openapi-schema-parser';
const parser = new Parser();
parser.registerSchemaParser(OpenAPISchemaParser());
const asyncapiWithOpenAPI = `
asyncapi: 2.0.0
info:
title: Example with OpenAPI
version: 0.1.0
channels:
example:
publish:
message:
schemaFormat: 'application/vnd.oai.openapi;version=3.0.0'
payload:
$ref: 'yourserver.com/schemas#/Book'
`;
const { document } = await parser.parse(asyncapiWithOpenAPI);