$ npm install @swagger-api/apidom-reference
@swagger-api/apidom-reference package contains advanced algorithms for semantic ApiDOM manipulations.
This package is divided into three (3) main components:
After prerequisites for installing this package are satisfied, you can install it via npm CLI by running the following command:
$ npm install @swagger-api/apidom-reference
This package has two main exports suitable for different use-cases. Empty configuration and saturated configuration.
import { parse } from '@swagger-api/apidom-reference/configuration/empty';
import OpenAPIJSON3_1Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-3-1';
await parse('/home/user/oas.json', {
parse: {
mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
parsers: [new OpenAPIJSON3_1Parser({ allowEmpty: true, sourceMap: false })]
}
});
When using this approach, options object is not configured with parsers, resolvers or strategies.
This is suitable for creating web bundles and gives you total control of the contents of your bundles.
import { parse } from '@swagger-api/apidom-reference';
or
import { parse } from '@swagger-api/apidom-reference/configuration/saturaged';
Both of above imports are equivalent. This approach is suitable for Node.js environments.
options object is pre-configured with all the parsers, resolvers and strategies.
Parse component consists of implementation of default parser plugins. Defaults parser plugin is a specialized wrapper that wraps one of the ApiDOM parser adapter into specialized API. Standard ApiDOM parser adapter can only parse strings. Parser plugins are capable of parsing local filesystem URIs and network URLs.
Parsing a file localed on local filesystem:
import { parse } from '@swagger-api/apidom-reference';
await parse('/home/user/oas.json', {
parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' }
});
Parsing an HTTP(S) URL located on internet:
import { parse } from '@swagger-api/apidom-reference';
await parse('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' }
})
Notice how we explicitly pass a mediaType parse option. This option is actually not required,
but if not provided, the Parse component will try to identify appropriate parser plugin by file contents, and it's extension (.json).
What actually happens if you don't provide mediaType parse option?
import { parse } from '@swagger-api/apidom-reference';
await parse('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json');
The result of this operation is going to be generic ApiDOM structure. By analyzing the name of the file
we can identify the extension of the file as .json. At this point we only know
that this file is probably going to contain JSON string, though we have no idea what data (AsyncApi/OpenApi)
is encoded within that JSON string.
In the future, we will introduce smart algorithms for looking in the contents of a file and detecting the
mediaType automatically. Of course not explicitly providing mediaType has performance implications (running detection)
so providing it is always a better option.
Parse component comes with number of default parser plugins.
Parses dehydrated ApiDOM structure and hydrates it.
This parser plugin is uniquely identified by apidom-json name.
Supported media types are:
[
'application/vnd.apidom',
'application/vnd.apidom+json',
]
Wraps @swagger-api/apidom-parser-adapter-openapi-json-2 package
and is uniquely identified by openapi-json-2 name.
Supported media types are:
[
'application/vnd.oai.openapi;version=2.0',
'application/vnd.oai.openapi+json;version=2.0',
]
Wraps @swagger-api/apidom-parser-adapter-openapi-json-3-0 package
and is uniquely identified by openapi-json-3-0 name.
Supported media types are:
[
'application/vnd.oai.openapi;version=3.0.0',
'application/vnd.oai.openapi+json;version=3.0.0',
'application/vnd.oai.openapi;version=3.0.1',
'application/vnd.oai.openapi+json;version=3.0.1',
'application/vnd.oai.openapi;version=3.0.2',
'application/vnd.oai.openapi+json;version=3.0.2',
'application/vnd.oai.openapi;version=3.0.3',
'application/vnd.oai.openapi+json;version=3.0.3',
'application/vnd.oai.openapi;version=3.0.4',
'application/vnd.oai.openapi+json;version=3.0.4',
]
Wraps @swagger-api/apidom-parser-adapter-openapi-yaml-2 package
and is uniquely identified by openapi-yaml-2 name.
Supported media types are:
[
'application/vnd.oai.openapi;version=2.0',
'application/vnd.oai.openapi+yaml;version=2.0',
]
Wraps @swagger-api/apidom-parser-adapter-openapi-yaml-3-0 package
and is uniquely identified by openapi-yaml-3-0 name.
Supported media types are:
[
'application/vnd.oai.openapi;version=3.0.0',
'application/vnd.oai.openapi+yaml;version=3.0.0',
'application/vnd.oai.openapi;version=3.0.1',
'application/vnd.oai.openapi+yaml;version=3.0.1',
'application/vnd.oai.openapi;version=3.0.2',
'application/vnd.oai.openapi+yaml;version=3.0.2',
'application/vnd.oai.openapi;version=3.0.3',
'application/vnd.oai.openapi+yaml;version=3.0.3',
'application/vnd.oai.openapi;version=3.0.4',
'application/vnd.oai.openapi+yaml;version=3.0.4',
]
Wraps @swagger-api/apidom-parser-adapter-openapi-json-3-1 package
and is uniquely identified by openapi-json-3-1 name.
Supported media types are:
[
'application/vnd.oai.openapi;version=3.1.0',
'application/vnd.oai.openapi+json;version=3.1.0',
]
Wraps @swagger-api/apidom-parser-adapter-openapi-yaml-3-1 package
and is uniquely identified by openapi-yaml-3-1 name.
Supported media types are:
[
'application/vnd.oai.openapi;version=3.1.0',
'application/vnd.oai.openapi+yaml;version=3.1.0',
]
Wraps @swagger-api/apidom-parser-adapter-asyncapi-json-2 package
and is uniquely identified by asyncapi-json-2 name.
Supported media types are:
[
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi;version=2.1.0',
'application/vnd.aai.asyncapi;version=2.2.0',
'application/vnd.aai.asyncapi;version=2.3.0',
'application/vnd.aai.asyncapi;version=2.4.0',
'application/vnd.aai.asyncapi;version=2.5.0',
'application/vnd.aai.asyncapi;version=2.6.0',
'application/vnd.aai.asyncapi+json;version=2.0.0',
'application/vnd.aai.asyncapi+json;version=2.1.0',
'application/vnd.aai.asyncapi+json;version=2.2.0',
'application/vnd.aai.asyncapi+json;version=2.3.0',
'application/vnd.aai.asyncapi+json;version=2.4.0',
'application/vnd.aai.asyncapi+json;version=2.5.0',
'application/vnd.aai.asyncapi+json;version=2.6.0',
]
Wraps @swagger-api/apidom-parser-adapter-asyncapi-yaml-2 package
and is uniquely identified by asyncapi-yaml-2 name.
Supported media types are:
[
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi;version=2.1.0',
'application/vnd.aai.asyncapi;version=2.2.0',
'application/vnd.aai.asyncapi;version=2.3.0',
'application/vnd.aai.asyncapi;version=2.3.0',
'application/vnd.aai.asyncapi;version=2.4.0',
'application/vnd.aai.asyncapi;version=2.5.0',
'application/vnd.aai.asyncapi;version=2.6.0',
'application/vnd.aai.asyncapi+yaml;version=2.0.0',
'application/vnd.aai.asyncapi+yaml;version=2.1.0',
'application/vnd.aai.asyncapi+yaml;version=2.2.0',
'application/vnd.aai.asyncapi+yaml;version=2.3.0',
'application/vnd.aai.asyncapi+yaml;version=2.4.0',
'application/vnd.aai.asyncapi+yaml;version=2.5.0',
'application/vnd.aai.asyncapi+yaml;version=2.6.0',
]
Wraps @swagger-api/apidom-parser-adapter-asyncapi-json-3 package
and is uniquely identified by asyncapi-json-3 name.
Supported media types are:
[
'application/vnd.aai.asyncapi;version=3.0.0',
'application/vnd.aai.asyncapi;version=3.1.0',
'application/vnd.aai.asyncapi+json;version=3.0.0',
'application/vnd.aai.asyncapi+json;version=3.1.0',
]
Wraps @swagger-api/apidom-parser-adapter-asyncapi-yaml-3 package
and is uniquely identified by asyncapi-yaml-3 name.
Supported media types are:
[
'application/vnd.aai.asyncapi;version=3.0.0',
'application/vnd.aai.asyncapi;version=3.1.0',
'application/vnd.aai.asyncapi+yaml;version=3.0.0',
'application/vnd.aai.asyncapi+yaml;version=3.1.0',
]
Wraps @swagger-api/apidom-parser-adapter-arazzo-json-1 package
and is uniquely identified by arazzo-json-1 name.
Supported media types are:
[
'application/vnd.oai.workflows;version=1.0.0',
'application/vnd.oai.workflows+json;version=1.0.0',
'application/vnd.oai.workflows;version=1.0.1',
'application/vnd.oai.workflows+json;version=1.0.1',
]
Wraps @swagger-api/apidom-parser-adapter-arazzo-yaml-1 package
and is uniquely identified by arazzo-yaml-1 name.
Supported media types are:
[
'application/vnd.oai.workflows;version=1.0.0',
'application/vnd.oai.workflows+yaml;version=1.0.0',
'application/vnd.oai.workflows;version=1.0.1',
'application/vnd.oai.workflows+yaml;version=1.0.1',
]
Wraps @swagger-api/apidom-parser-adapter-json-schema-json-2020-12 package
and is uniquely identified by json-schema-json-2020-12 name.
Supported media types are:
[
'application/schema;version=2020-12',
'application/schema+json;version=2020-12',
]
The parser detects a JSON Schema 2020-12 document by its root $schema keyword when the file's media type
is not one of the supported media types. The saturated configuration turns this detection off
(detectContent: false), so a document is parsed as JSON Schema 2020-12 only when one of these
media types is given: files referenced from other specifications (e.g. OpenAPI 3.1) often declare
a root $schema, and their dereference strategies expect them as generic ApiDOM.
Wraps @swagger-api/apidom-parser-adapter-json-schema-yaml-2020-12 package
and is uniquely identified by json-schema-yaml-2020-12 name.
Supported media types are:
[
'application/schema;version=2020-12',
'application/schema+yaml;version=2020-12',
]
Like json-schema-json-2020-12, it detects documents by content unless detectContent is false,
which is how the saturated configuration registers it.
Wraps @swagger-api/apidom-parser-adapter-api-design-systsems-json package
and is uniquely identified by api-design-systems-json name.
Supported media types are:
[
'application/vnd.aai.apidesignsystems;version=2021-05-07',
'application/vnd.aai.apidesignsystems+json;version=2021-05-07'
]
Wraps @swagger-api/apidom-parser-adapter-api-design-systems-yaml package
and is uniquely identified by api-design-systems-yaml name.
Supported media types are:
[
'application/vnd.aai.apidesignsystems;version=2021-05-07',
'application/vnd.aai.apidesignsystems+yaml;version=2021-05-07'
]
Wraps @swagger-api/apidom-parser-adapter-json package
and is uniquely identified by json name.
Supported media types are:
[
'application/json'
]
Wraps @swagger-api/apidom-parser-adapter-yaml-1-2 package
and is uniquely identified by yaml-1-2 name.
Supported media types are:
[
'text/yaml',
'application/yaml'
]
Can parse any binary or non-binary file and return it's content as base64 encoded string.
This parser is uniquely identified by binary name.
All media types are supported.
It's important to understand that default parser plugins are run in specific order. The order is determined
by the options.parse.parsers option.
Every plugin is pulled from options.parse.parsers option, and it's canParse method is called to determine
whether the plugin can parse the URI. If canParse returns true, parse method of plugin is called
and result from parsing is returned. No subsequent parser plugins are run. If canParse returns
false, next parser plugin is pulled and this process is repeated until one of the parser plugins canParse method
returns true or until entire list of parser plugins is exhausted (throws error).
[
new OpenAPIJSON2Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIYAML2Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIJSON3_0Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIYAML3_0Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIJSON3_1Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIYAML3_1Parser({ allowEmpty: true, sourceMap: false }),
new AsyncAPIJSON2Parser({ allowEmpty: true, sourceMap: false }),
new AsyncAPIYAML2Parser({ allowEmpty: true, sourceMap: false }),
new AsyncAPIJSON3Parser({ allowEmpty: true, sourceMap: false }),
new AsyncAPIYAML3Parser({ allowEmpty: true, sourceMap: false }),
new ArazzoJSON1Parser({ allowEmpty: true, sourceMap: false }),
new ArazzoYAML1Parser({ allowEmpty: true, sourceMap: false }),
new JSONSchemaJSON2020_12Parser({ allowEmpty: true, sourceMap: false, detectContent: false }),
new JSONSchemaYAML2020_12Parser({ allowEmpty: true, sourceMap: false, detectContent: false }),
new APIDesignSystemsJSONParser({ allowEmpty: true, sourceMap: false }),
new APIDesignSystemsYAMLParser({ allowEmpty: true, sourceMap: false }),
new APIDOMJSONParser({ allowEmpty: true, sourceMap: false }),
new JSONParser({ allowEmpty: true, sourceMap: false }),
new YAMLParser({ allowEmpty: true, sourceMap: false }),
new BinaryParser({ allowEmpty: true }),
]
Most specific parser plugins are listed first, most generic are listed last.
It's possible to change the parser plugins order globally by mutating global parse options:
import { options } from '@swagger-api/apidom-reference';
import OpenAPIJSON2Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-2';
import OpenAPIYAML2Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-yaml-2';
import OpenAPIJSON3_0Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-3-0';
import OpenAPIYAML3_0Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-yaml-3-0'
import OpenAPIJSON3_1Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-3-1';
import OpenAPIYAML3_1Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-yaml-3-1'
import AsyncAPIJSON2Parser from '@swagger-api/apidom-reference/parse/parsers/asyncapi-json-2';
import AsyncAPIJSON3Parser from '@swagger-api/apidom-reference/parse/parsers/asyncapi-json-3';
import AsyncAPIYAML2Parser from '@swagger-api/apidom-reference/parse/parsers/asyncapi-yaml-2';
import AsyncAPIYAML3Parser from '@swagger-api/apidom-reference/parse/parsers/asyncapi-yaml-3';
import ArazzoJSON1Parser from '@swagger-api/apidom-reference/parse/parsers/arazzo-json-1';
import ArazzoYAML1Parser from '@swagger-api/apidom-reference/parse/parsers/arazzo-yaml-1';
import APIDOMJSONParser from '@swagger-api/apidom-reference/parse/parsers/apidom-json';
import APIDesignSystemsJSONParser from '@swagger-api/apidom-reference/parse/parsers/api-design-systems-json';
import APIDesignSystemsYAMLParser from '@swagger-api/apidom-reference/parse/parsers/api-design-systems-json';
import JSONParser from '@swagger-api/apidom-reference/parse/parsers/json';
import YAMLParser from '@swagger-api/apidom-reference/parse/parsers/yaml';
import BinaryParser from '@swagger-api/apidom-reference/parse/parsers/binary';
options.parse.parsers = [
new OpenAPIJSON2Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIYAML2Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIJSON3_0Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIYAML3_0Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIJSON3_1Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIYAML3_1Parser({ allowEmpty: true, sourceMap: false }),
new AsyncAPIJSON2Parser({ allowEmpty: true, sourceMap: false }),
new AsyncAPIYAML2Parser({ allowEmpty: true, sourceMap: false }),
new AsyncAPIJSON3Parser({ allowEmpty: true, sourceMap: false }),
new AsyncAPIYAML3Parser({ allowEmpty: true, sourceMap: false }),
new ArazzoJSON1Parser({ allowEmpty: true, sourceMap: false }),
new ArazzoYAML1Parser({ allowEmpty: true, sourceMap: false }),
new APIDesignSystemsJSONParser({ allowEmpty: true, sourceMap: false }),
new APIDesignSystemsYAMLParser({ allowEmpty: true, sourceMap: false }),
new APIDOMJSONParser({ allowEmpty: true, sourceMap: false }),
new YAMLParser({ allowEmpty: true, sourceMap: false }),
new JSONParser({ allowEmpty: true, sourceMap: false }),
new BinaryParser({ allowEmpty: true }),
]
To change the parser plugins order on ad-hoc basis:
import { parse } from '@swagger-api/apidom-reference';
import OpenAPIJSON2Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-2';
import OpenAPIYAML2Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-yaml-2';
import OpenAPIJSON3_0Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-3-0';
import OpenAPIYAML3_0Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-yaml-3-0'
import OpenAPIJSON3_1Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-3-1';
import OpenAPIYAML3_1Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-yaml-3-1'
import AsyncAPIJSON2Parser from '@swagger-api/apidom-reference/parse/parsers/asyncapi-json-2';
import AsyncAPIJSON3Parser from '@swagger-api/apidom-reference/parse/parsers/asyncapi-json-3';
import AsyncAPIYAML2Parser from '@swagger-api/apidom-reference/parse/parsers/asyncapi-yaml-2';
import AsyncAPIYAML3Parser from '@swagger-api/apidom-reference/parse/parsers/asyncapi-yaml-3';
import ArazzoJSON1Parser from '@swagger-api/apidom-reference/parse/parsers/arazzo-json-1';
import ArazzoYAML1Parser from '@swagger-api/apidom-reference/parse/parsers/arazzo-yaml-1';
import APIDOMJSONParser from '@swagger-api/apidom-reference/parse/parsers/apidom-json';
import APIDesignSystemsJSONParser from '@swagger-api/apidom-reference/parse/parsers/api-design-systems-json';
import APIDesignSystemsYAMLParser from '@swagger-api/apidom-reference/parse/parsers/api-design-systems-json';
import JSONParser from '@swagger-api/apidom-reference/parse/parsers/json';
import YAMLParser from '@swagger-api/apidom-reference/parse/parsers/yaml';
import BinaryParser from '@swagger-api/apidom-reference/parse/parsers/binary';
await parse('/home/user/oas.json', {
parse: {
mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
parsers: [
new OpenAPIJSON2Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIYAML2Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIJSON3_0Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIYAML3_0Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIJSON3_1Parser({ allowEmpty: true, sourceMap: false }),
new OpenAPIYAML3_1Parser({ allowEmpty: true, sourceMap: false }),
new AsyncAPIJSON2Parser({ allowEmpty: true, sourceMap: false }),
new AsyncAPIYAML2Parser({ allowEmpty: true, sourceMap: false }),
new AsyncAPIJSON3Parser({ allowEmpty: true, sourceMap: false }),
new AsyncAPIYAML3Parser({ allowEmpty: true, sourceMap: false }),
new ArazzoJSON1Parser({ allowEmpty: true, sourceMap: false }),
new ArazzoYAML1Parser({ allowEmpty: true, sourceMap: false }),
new APIDesignSystemsJSONParser({ allowEmpty: true, sourceMap: false }),
new APIDesignSystemsYAMLParser({ allowEmpty: true, sourceMap: false }),
new APIDOMJSONParser({ allowEmpty: true, sourceMap: false }),
new JSONParser({ allowEmpty: true, sourceMap: false }),
new YAMLParser({ allowEmpty: true, sourceMap: false }),
new BinaryParser({ allowEmpty: true }),
],
},
});
Parser plugins accept additional options like allowEmpty or sourceMap. It's possible to change parser plugin
options globally by mutating global parse options:
import { options, parse } from '@swagger-api/apidom-reference';
options.parser.parserOpts = {
allowEmpty: false,
sourceMap: true,
};
await parse('/home/user/oas.json', {
parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' }
});
To change the parser plugins options on ad-hoc basis:
import { parse } from '@swagger-api/apidom-reference';
await parse('/home/user/oas.json', {
parse: {
mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
parserOpts: { allowEmpty: false, sourceMap: true },
},
});
Parse component can be extended by additional parser plugins. Every parser plugin is an object that must conform to the following interface/shape:
interface ParserPlugin {
// uniquely identifies this parser plugin
readonly name: string;
allowEmpty: boolean;
sourceMap: boolean;
fileExtensions: string[];
mediaTypes: string[];
// this method is called to determine whether the parser plugin can parse the file
canParse(file: File): boolean | Promise<boolean>;
// this method actually parses the file
parse(file: File): ParseResultElement | Promise<ParseResultElement>;
}
New parser plugin is then provided as an option to a parse function:
import { parse, options, File, Parser, ParserOptions } from '@swagger-api/apidom-reference';
interface MyCustomParserPluginOptions extends Omit<ParserOptions, 'name'> {}
class MyCustomParserPlugin extends Parser {
constructor(options?: MyCustomParserPluginOptions) {
super({ ...(options ?? {}), name: 'my-custom-parser' });
}
async canParse(file: File, options: typeof options) {
return true;
}
async parse(file: File, options: typeof options) {
// implementation of parsing
}
}
await parse('/home/user/oas.json', {
parse: {
mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
parsers: [...options.parse.parsers, new MyCustomParserPlugin()],
}
});
In this particular example we're adding our custom parser plugin as the last plugin
to the available default parser plugin list, so there's a good chance that one of the
default parser plugins detects that it can parse the /home/user/oas.json file,
parses it and returns.
If you want to force execution of your custom plugin, add it as a first parser plugin:
import { parse, options, File, Parser, ParserOptions } from '@swagger-api/apidom-reference';
interface MyCustomParserPluginOptions extends Omit<ParserOptions, 'name'> {}
class MyCustomParserPlugin extends Parser {
constructor(options?: MyCustomParserPluginOptions) {
super({ ...(options ?? {}), name: 'my-custom-parser' });
}
async canParse(file: File, options: typeof options) {
return true;
}
async parse(file: File, options: typeof options) {
// implementation of parsing
}
}
await parse('/home/user/oas.json', {
parse: {
mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
parsers: [new MyCustomParserPlugin(), ...options.parse.parsers],
}
});
To override the default parser plugins entirely, set myCustomParserPlugin plugin to be the only one available:
import { parse, options, File, Parser, ParserOptions } from '@swagger-api/apidom-reference';
interface MyCustomParserPluginOptions extends Omit<ParserOptions, 'name'> {}
class MyCustomParserPlugin extends Parser {
constructor(options?: MyCustomParserPluginOptions) {
super({ ...(options ?? {}), name: 'my-custom-parser' });
}
async canParse(file: File, options: typeof options) {
return true;
}
async parse(file: File, options: typeof options) {
// implementation of parsing
}
}
await parse('/home/user/oas.json', {
parse: {
mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
parsers: [new MyCustomParserPlugin()],
}
});
Parser plugins can be added, removed, replaced or reordered.
Here are two examples of removing one of the parser plugins called asyncapi-json-2.
We're using the fact that every parser plugin is uniquely identifiable by its name.
Removing parser plugin globally for all subsequence parse calls is achieved by mutating global options:
import { parse, options, mergeOptions } from '@swagger-api/apidom-reference';
options.parse.parsers = options.parse.parsers.filter(parserPlugin => parserPlugin !== 'asyncapi-json-2')
// here you can be sure `asyncapi-json-2` plugin was disabled
await parse('/home/user/oas.json', {
parse: {
mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
}
});
Removing default parser plugin on ad-hoc basis:
import { parse, options } from '@swagger-api/apidom-reference';
await parse('/home/user/oas.json', {
parse: {
mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
parsers: options.parse.parsers.filter(parserPlugin => parserPlugin.name !== 'asyncapi-json-2'),
}
});
As you can see, these are all primitive JavaScript Array manipulation techniques. These techniques can be applied to replacing (use Array.prototype.map()) or reordering parser plugins as well.
Resolve component consists of two (2) sub-components: File resolution and External Resolution.
Resolve component is used by Parse component under the hood. Resolve component provides a resolved
file contents for a Parse component to parse.
Contains implementation of default resolver plugins. Defaults resolver plugin is an object which knows how to obtain contents of a file represented by URI or URL.
File resolution comes with two (2) default resolver plugins.
This resolver plugin is responsible for resolving a local file. It detects if the provided URI represents a filesystem path and if so, reads the file and provides its content.
WARNING: use this plugin with caution, as it can read files from a local file system. By default, this plugin will reject to read any files from the local file system, unless explicitly provided by fileAllowList option.
File allow list can be provided globally as an option to FileResolver in form
of array of glob patterns or regular expressions.
import { options } from '@swagger-api/apidom-reference';
import FileResolver from '@swagger-api/apidom-reference/resolve/resolvers/file';
import HTTPResolverAxios from '@swagger-api/apidom-reference/resolve/resolvers/http-axios';
options.resolve.resolvers = [
new FileResolver({
fileAllowList: [
'*.json',
/\.json$/,
]
}),
new HTTPResolverAxios({ timeout: 5000, redirects: 5, withCredentials: false }),
]
File allow list can also be provided on ad-hoc basis:
import { resolve } from '@swagger-api/apidom-reference';
await resolve('/home/user/oas.json', {
resolve: {
resolverOpts: {
fileAllowList: [
'*.json',
/\.json$/,
]
},
},
});
The cacheTTL option controls caching behavior for files resolved from remote URLs or local filesystem paths. When
set to a value greater than 0, resolved file contents are cached using
the Cache Storage API for the specified time-to-live (TTL)
duration in milliseconds.
import { resolve } from '@swagger-api/apidom-reference';
await resolve('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
resolve: {
resolverOpts: {
cacheTTL: 60 * 1000, // store the result in a cache for 60 seconds
},
},
});
This resolver plugin is responsible for resolving a remote file represented by HTTP(s) URL. It detects if the provided URI represents an HTTP(s) URL and if so, fetches the file and provides its content.
HttpResolverAxios plugin supports all the options available in Axios Request Config. Config options can be provided in following way:
import { resolve } from '@swagger-api/apidom-reference';
await resolve('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
resolve: {
resolverOpts: {
axiosConfig: {
timeout: 10000,
withCredentials: false,
responseType: 'json',
},
},
},
});
HttpResolverAxios plugin supports Axios Interceptors. Interceptors can be provided in following way:
import { resolve } from '@swagger-api/apidom-reference';
const requestInterceptor = (config) => config;
const responseInterceptor = (response) => response;
await resolve('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
resolve: {
resolverOpts: {
axiosConfig: {
interceptors: {
request: requestInterceptor,
response: responseInterceptor,
},
},
},
},
});
Multiple request and response interceptors can be provided in following way:
import { resolve } from '@swagger-api/apidom-reference';
const requestInterceptor1 = (config) => config;
const requestInterceptor2 = (config) => config;
const responseInterceptor1 = (response) => response;
const responseInterceptor2 = async (error) => Promise.reject(error);
await resolve('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
resolve: {
resolverOpts: {
axiosConfig: {
interceptors: {
request: [requestInterceptor1, requestInterceptor2],
response: [responseInterceptor1, responseInterceptor2],
},
},
},
},
});
File resolution on local filesystem path:
import { readFile } from '@swagger-api/apidom-reference';
await readFile('/home/user/oas.json'); // Promise<Buffer>
File resolution on HTTP(s) URL:
import { readFile } from '@swagger-api/apidom-reference';
await readFile('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json'); // Promise<Buffer>
File resolution always returns a Promise containing a Buffer.
It is responsibility of the API consumer to transform Buffer into String or any other type.
import { readFile } from '@swagger-api/apidom-reference';
const buffer = await readFile('/home/user/oas.json');
const string = buffer.toString('utf-8');
It's important to understand that default resolver plugins are run in specific order. The order is determined
by the options.resolve.resolvers option.
Every plugin is pulled from options.resolve.resolvers option, and it's canRead method is called to determine
whether the plugin can resolve the URI. If canRead returns true, read method of plugin is called
and result from reading the file is returned. No subsequent resolver plugins are run.
If canRead returns false, next resolver plugin is pulled and this process is repeated until one
of the resolver plugins canRead method returns true or until entire list of resolver plugins is exhausted (throws error).
[
new FileResolver(),
new HTTPResolverAxios({ timeout: 5000, redirects: 5, withCredentials: false }),
]
It's possible to change resolver plugins order globally by mutating global resolve option:
import { options } from '@swagger-api/apidom-reference';
import FileResolver from '@swagger-api/apidom-reference/resolve/resolvers/file';
import HTTPResolverAxios from '@swagger-api/apidom-reference/resolve/resolvers/http-axios';
options.resolve.resolvers = [
new HTTPResolverAxios({ timeout: 5000, redirects: 5, withCredentials: false }),
new FileResolver(),
]
To change resolver plugins order on ad-hoc basis:
import { readFile } from '@swagger-api/apidom-reference';
import FileResolver from '@swagger-api/apidom-reference/resolve/resolvers/file';
import HTTPResolverAxios from '@swagger-api/apidom-reference/resolve/resolvers/http-axios';
await readFile('/home/user/oas.json', {
resolve: {
resolvers: [
new HTTPResolverAxios({ timeout: 5000, redirects: 5, withCredentials: false }),
new FileResolver(),
],
},
});
Some resolver plugins accept additional options. It's possible to change resolver plugin
options globally by mutating global resolve options:
import { options, readFile } from '@swagger-api/apidom-reference';
options.resolve.resolverOpts = {
axiosConfig: {
timeout: 10000,
},
};
await readFile('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json');
To change the resolver plugins options on ad-hoc basis:
import { readFile } from '@swagger-api/apidom-reference';
await readFile('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
resolve: {
resolverOpts: {
axiosConfig: {
timeout: 10000,
},
},
},
});
Both of above examples will be using HttpResolverAxios plugin
(as we're trying to resolve HTTP(s) URL) and the timeout of resolution will increase from default 3 seconds
to 10 seconds.
Some resolver strategy plugins accept additional options. It's possible to change strategy plugin
options globally by mutating global resolve options:
import { options, resolve } from '@swagger-api/apidom-reference';
options.resolve.strategyOpts = {
apidom: { clone: true },
};
await resolve('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json');
To change the resolver strategy plugins options on ad-hoc basis:
import { resolve } from '@swagger-api/apidom-reference';
await resolve('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
resolve: {
strategyOpts: {
apidom: { clone: true },
},
},
});
Resolve component can be extended by additional resolver plugins. Every resolver plugin is an object that must conform to the following interface/shape:
interface ResolverPlugin {
// uniquely identifies this plugin
readonly name: string;
// this method is called to determine whether the resolver plugin can resolve the file
canRead(file: File, options: ReferenceOptions): boolean;
// this method actually resolves the file
read(file: File, options: ReferenceOptions): Promise<Buffer>;
}
New resolver plugin is then provided as an option to a readFile function:
import { readFile, options, File, Resolver, ResolverOptions } from '@swagger-api/apidom-reference';
interface MyCustomResolverOptions extends Omit<ResolverOptions, 'name'> {}
class MyCustomResolverPlugin extends Resolver {
constructor(options?: MyCustomResolverOptions) {
super({ ...(options ?? {}), name: 'my-custom-resolver' });
}
async canRead(file: File, options: typeof options) {
return true;
}
async read(file: File, options: typeof options) {
// implementation of file resolution
}
}
await readFile('/home/user/oas.json', {
resolve: {
resolvers: [...options.resolve.resolvers, new MyCustomResolverPlugin()],
}
});
In this particular example we're adding our custom resolver plugin as the last plugin
to the available default resolver plugin list, so there's a good chance that one of the
default resolver plugins detects that it can resolve the /home/user/oas.json file,
resolves it and returns its content.
If you want to force execution of your custom plugin, add it as a first resolver plugin:
import { readFile, options, File, Resolver, ResolverOptions } from '@swagger-api/apidom-reference';
interface MyCustomResolverOptions extends Omit<ResolverOptions, 'name'> {}
class MyCustomResolverPlugin extends Resolver {
constructor(options?: MyCustomResolverOptions) {
super({ ...(options ?? {}), name: 'my-custom-resolver' });
}
async canRead(file: File, options: typeof options) {
return true;
}
async read(file: File, options: typeof options) {
// implementation of file resolution
}
}
await readFile('/home/user/oas.json', {
resolve: {
resolvers: [new MyCustomResolverPlugin(), ...options.resolve.resolvers],
}
});
To override the default resolver plugins entirely, set myCustomResolverPlugin plugin to be the only one available:
import { readFile, options, File, Resolver, ResolverOptions } from '@swagger-api/apidom-reference';
interface MyCustomResolverOptions extends Omit<ResolverOptions, 'name'> {}
class MyCustomResolverPlugin extends Resolver {
constructor(options?: MyCustomResolverOptions) {
super({ ...(options ?? {}), name: 'my-custom-resolver' });
}
async canRead(file: File, options: typeof options) {
return true;
}
async read(file: File, options: typeof options) {
// implementation of file resolution
}
}
await readFile('/home/user/oas.json', {
resolve: {
resolvers: [new MyCustomResolverPlugin()],
}
});
New resolver plugins can be based on two predefined abstract classes: Resolver and HTTPResolver.
Resolver plugins can be added, removed, replaced or reordered. We've already covered these techniques in Manipulating parser plugins section.
External resolution is a process of resolving all external dependencies of a particular
document using a specific external resolution strategy. External resolution strategy is determined by
asserting on mediaType option. File Resolution (file content is read/fetched)
and Parse component (file content is parsed) are used under the hood.
Externally resolving a file localed on a local filesystem:
import { resolve } from '@swagger-api/apidom-reference';
await resolve('/home/user/oas.json', {
parse: { mediType: 'application/vnd.oai.openapi+json;version=3.1.0' },
}); // Promise<ReferenceSet>
Externally resolving an HTTP(S) URL located on an internet:
import { resolve } from '@swagger-api/apidom-reference';
await resolve('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
resolve: {
resolverOpts: {
axiosConfig: {
timeout: 10
},
},
},
}); // Promise<ReferenceSet>
Externally resolving an ApiDOM fragment:
When externally resolving an ApiDOM fragment, baseURI
resolve option needs to be provided to have a starting point for external dependency resolution.
mediaType parse option is unnecessary as we can directly assert the type of ApiDOM fragment.
import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
import { resolveApiDOM } from '@swagger-api/apidom-reference';
const apidom = OpenApi3_1Element.refract({
openapi: '3.1.0',
components: {
parameters: {
externalRef: {
$ref: './ex.json#/externalParameter', // file is located at /home/user/ex.json
}
}
}
});
const refSet = await resolveApiDOM(apidom, {
resolve: { baseURI: '/home/user/' },
});
for (const ref of refSet) {
console.log(ref.uri);
}
// /home/user
// /home/user/ex.json
ReferenceSet is a Set like structure containing list of Reference objects. Every Reference object represents single external dependency.
External resolution strategy determines how a document is externally resolved. Depending on document mediaType
every strategy differs significantly. Resolve component comes with two (2) default external resolution strategies.
External resolution strategy for understanding and resolving remote elements referenced with Ref Element.
Supported media types:
[
'application/vnd.apidom',
'application/vnd.apidom+json'
]
External resolution strategy for understanding and resolving external dependencies of AsyncApi 2.x.y definitions.
Supported media types:
[
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi+json;version=2.0.0',
'application/vnd.aai.asyncapi+yaml;version=2.0.0',
'application/vnd.aai.asyncapi;version=2.1.0',
'application/vnd.aai.asyncapi+json;version=2.1.0',
'application/vnd.aai.asyncapi+yaml;version=2.1.0',
'application/vnd.aai.asyncapi;version=2.2.0',
'application/vnd.aai.asyncapi+json;version=2.2.0',
'application/vnd.aai.asyncapi+yaml;version=2.2.0',
'application/vnd.aai.asyncapi;version=2.3.0',
'application/vnd.aai.asyncapi+json;version=2.3.0',
'application/vnd.aai.asyncapi+yaml;version=2.3.0',
'application/vnd.aai.asyncapi;version=2.4.0',
'application/vnd.aai.asyncapi+json;version=2.4.0',
'application/vnd.aai.asyncapi+yaml;version=2.4.0',
'application/vnd.aai.asyncapi;version=2.5.0',
'application/vnd.aai.asyncapi+json;version=2.5.0',
'application/vnd.aai.asyncapi+yaml;version=2.5.0',
'application/vnd.aai.asyncapi;version=2.6.0',
'application/vnd.aai.asyncapi+json;version=2.6.0',
'application/vnd.aai.asyncapi+yaml;version=2.6.0',
]
External resolution strategy for understanding and resolving external dependencies of OpenApi 2.0 definitions.
Supported media types:
[
'application/vnd.oai.openapi;version=2.0',
'application/vnd.oai.openapi+json;version=2.0',
'application/vnd.oai.openapi+yaml;version=2.0',
]
External resolution strategy for understanding and resolving external dependencies of OpenApi 3.0.x definitions.
Supported media types:
[
'application/vnd.oai.openapi;version=3.0.0',
'application/vnd.oai.openapi+json;version=3.0.0',
'application/vnd.oai.openapi+yaml;version=3.0.0',
'application/vnd.oai.openapi;version=3.0.1',
'application/vnd.oai.openapi+json;version=3.0.1',
'application/vnd.oai.openapi+yaml;version=3.0.1',
'application/vnd.oai.openapi;version=3.0.2',
'application/vnd.oai.openapi+json;version=3.0.2',
'application/vnd.oai.openapi+yaml;version=3.0.2',
'application/vnd.oai.openapi;version=3.0.3',
'application/vnd.oai.openapi+json;version=3.0.3',
'application/vnd.oai.openapi+yaml;version=3.0.3',
'application/vnd.oai.openapi;version=3.0.4',
'application/vnd.oai.openapi+json;version=3.0.4',
'application/vnd.oai.openapi+yaml;version=3.0.4',
]
External resolution strategy for understanding and resolving external dependencies of OpenApi 3.1.x definitions.
Supported media types:
[
'application/vnd.oai.openapi;version=3.1.0',
'application/vnd.oai.openapi+json;version=3.1.0',
'application/vnd.oai.openapi+yaml;version=3.1.0'
]
External resolution strategy for understanding and resolving external dependencies of standalone JSON Schema 2020-12 documents.
Supported media types:
[
'application/schema;version=2020-12',
'application/schema+json;version=2020-12',
'application/schema+yaml;version=2020-12'
]
It's important to understand that default external resolution strategies are run in specific order. The order is determined
by the options.resolve.strategies option.
Every strategy is pulled from options.resolve.strategies option and its canResolve method is called to determine
whether the strategy can externally resolve the URI. If canResolve returns true, resolve method of strategy is called
and result from external resolution is returned. No subsequent strategies are run. If canResolve returns
false, next strategy is pulled and this process is repeated until one of the strategy's canResolve method
returns true or until entire list of strategies is exhausted (throws error).
[
new OpenAPI2ResolveStrategy(),
new OpenAPI3_0ResolveStrategy(),
new OpenAPI3_1ResolveStrategy(),
new AsyncAPI2ResolveStrategy(),
new JSONSchema2020_12ResolveStrategy(),
new ApiDOMResolveStrategy(),
]
Most specific strategies are listed first, most generic are listed last.
It's possible to change strategies order globally by mutating global resolve option:
import { options } from '@swagger-api/apidom-reference';
import ApiDOMResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/apidom';
import AsyncAPI2ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/asyncapi-2';
import OpenAPI2ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/openapi-2';
import OpenAPI3_0ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/openapi-3-0';
import OpenAPI3_1ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/openapi-3-1';
options.resolve.strategies = [
new OpenAPI2ResolveStrategy(),
new OpenAPI3_0ResolveStrategy(),
new OpenAPI3_1ResolveStrategy(),
new AsyncAPI2ResolveStrategy(),
new ApiDOMResolveStrategy(),
];
To change the strategies order on ad-hoc basis:
import { resolve } from '@swagger-api/apidom-reference';
import ApiDOMResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/apidom';
import AsyncAPI2ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/asyncapi-2';
import OpenAPI2ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/openapi-2';
import OpenAPI3_0ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/openapi-3-0';
import OpenAPI3_1ResolveStrategy from '@swagger-api/apidom-reference/resolve/strategies/openapi-3-1';
await resolve('/home/user/oas.json', {
parse: {
mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
},
resolve: {
strategies: [
new AsyncAPI2ResolveStrategy(),
new OpenAPI2ResolveStrategy(),
new OpenAPI3_0ResolveStrategy(),
new OpenAPI3_1ResolveStrategy(),
new ApiDOMResolveStrategy(),
]
}
});
Resolve component can be extended by additional strategies. Every strategy is an object that must conform to the following interface/shape:
interface ResolveStrategy {
// uniquely identifies this plugin
readonly name: string;
// this method is called to determine whether the strategy can externally resolve the file
canResolve(file: File, options: ReferenceOptions): boolean;
// this method actually externally resolves the file
resolve(file: File, options: ReferenceOptions): Promise<ReferenceSet>;
}
New strategy is then provided as an option to a resolve function:
import { resolve, options, File, ResolveStrategy, ResolveStrategyOptions } from '@swagger-api/apidom-reference';
interface MyCustomResolverStrategyOptions extends Omit<ResolveStrategyOptions, 'name'> {}
class MyCustomResolverStrategy extends ResolveStrategy {
constructor(options?: MyCustomResolverStrategyOptions) {
super({ ...(options ?? {}), name: 'my-custom-resolver-strategy' });
}
canResolve(file: File, options: typeof options) {
return true;
}
async resolve(file: File, options: typeof options) {
// implementation of external resolution
}
}
await resolve('/home/user/oas.json', {
parse: {
mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
},
resolve: {
strategies: [...options.resolve.strategies, new MyCustomResolverStrategy()],
}
});
In this particular example we're adding our custom strategy as the last strategy
to the available default external resolution strategy list, so there's a good chance that one of the
default strategies detects that it can externally resolve the /home/user/oas.json file,
resolves it and returns ReferenceSet object.
If you want to force execution of your strategy, add it as a first one:
import { resolve, options, File, ResolveStrategy, ResolveStrategyOptions } from '@swagger-api/apidom-reference';
interface MyCustomResolverStrategyOptions extends Omit<ResolveStrategyOptions, 'name'> {}
class MyCustomResolverStrategy extends ResolveStrategy {
constructor(options?: MyCustomResolverStrategyOptions) {
super({ ...(options ?? {}), name: 'my-custom-resolver-strategy' });
}
canResolve(file: File, options: typeof options) {
return true;
}
async resolve(file: File, options: typeof options) {
// implementation of external resolution
}
}
await resolve('/home/user/oas.json', {
parse: {
mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
},
resolve: {
strategies: [new MyCustomResolverStrategy(), ...options.resolve.strategies],
}
});
To override the default strategies entirely, set MyCustomResolverStrategy strategy to be the only one available:
import { resolve, options, File, ResolveStrategy, ResolveStrategyOptions } from '@swagger-api/apidom-reference';
interface MyCustomResolverStrategyOptions extends Omit<ResolveStrategyOptions, 'name'> {}
class MyCustomResolverStrategy extends ResolveStrategy {
constructor(options?: MyCustomResolverStrategyOptions) {
super({ ...(options ?? {}), name: 'my-custom-resolver-strategy' });
}
canResolve(file: File, options: typeof options) {
return true;
}
async resolve(file: File, options: typeof options) {
// implementation of external resolution
}
}
await resolve('/home/user/oas.json', {
parse: {
mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
},
resolve: {
strategies: [new MyCustomResolverPlugin()],
}
});
New strategies can be based on a predefined abstract class called ResolveStrategy.
External resolution strategies can be added, removed, replaced or reordered. We've already covered these techniques in Manipulating parser plugins section.
Dereferencing is a process of transcluding referencing element (internal or external) with a referenced element
using a specific dereference strategy. Simply put, dereferencing is a process of reference removal.
Dereferencing strategy is determined by asserting on mediaType option. File Resolution (file content is read/fetched)
and Parse component (file content is parsed) are used under the hood.
Dereferencing a file localed on a local filesystem:
import { dereference } from '@swagger-api/apidom-reference';
await dereference('/home/user/oas.json', {
parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
}); // Promise<ParseResultElement>
Dereferencing an HTTP(S) URL located on an internet:
import { dereference } from '@swagger-api/apidom-reference';
await dereference('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.1/webhook-example.json', {
parse: { mediaType: 'application/vnd.oai.openapi+json;version=3.1.0' },
resolve: {
resolverOpts: {
axiosConfig: {
timeout: 10
},
},
},
}); // Promise<ParseResultElement>
Dereferencing an ApiDOM fragment:
When dereferencing an ApiDOM fragment, baseURI
resolve option needs to be provided to have a starting point for external dependency resolution.
mediaType parse option is unnecessary as we can directly assert the type of ApiDOM fragment.
ex.json
{
"externalParameter": {
"name": "param1",
"in": "query"
}
}
import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
import { dereferenceApiDOM } from '@swagger-api/apidom-reference';
const apidom = OpenApi3_1Element.refract({
openapi: '3.1.0',
components: {
parameters: {
externalRef: {
$ref: './ex.json#/externalParameter', // file is located at /home/user/ex.json
}
}
}
});
const dereferenced = await dereferenceApiDOM(apidom, {
resolve: { baseURI: '/home/user/' },
});
/**
* OpenApi3_1Element {
* openapi: '3.1.0',
* components: {
* parameters: {
* externalRef: {
* name: param1,
* in: query
* }
* }
* }
* }
*/
Dereference strategy determines how a document is internally or externally dereferenced. Depending on document mediaType option,
every strategy differs significantly. Dereference component comes with four (4) default dereference strategies.
Dereference strategy for dereferencing ApiDOM using Ref Element. Ref Element MAY be used to reference elements in remote documents or elements in the local document. The ref element transcludes the contents of the element into the document in which it is referenced.
Supported media types:
[
'application/vnd.apidom',
'application/vnd.apidom+json',
]
Dereference strategy for dereferencing AsyncApi 2.x.y definitions.
Supported media types:
[
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi+json;version=2.0.0',
'application/vnd.aai.asyncapi+yaml;version=2.0.0',
'application/vnd.aai.asyncapi;version=2.1.0',
'application/vnd.aai.asyncapi+json;version=2.1.0',
'application/vnd.aai.asyncapi+yaml;version=2.1.0',
'application/vnd.aai.asyncapi;version=2.2.0',
'application/vnd.aai.asyncapi+json;version=2.2.0',
'application/vnd.aai.asyncapi+yaml;version=2.2.0',
'application/vnd.aai.asyncapi;version=2.3.0',
'application/vnd.aai.asyncapi+json;version=2.3.0',
'application/vnd.aai.asyncapi+yaml;version=2.3.0',
'application/vnd.aai.asyncapi;version=2.4.0',
'application/vnd.aai.asyncapi+json;version=2.4.0',
'application/vnd.aai.asyncapi+yaml;version=2.4.0',
'application/vnd.aai.asyncapi;version=2.5.0',
'application/vnd.aai.asyncapi+json;version=2.5.0',
'application/vnd.aai.asyncapi+yaml;version=2.5.0',
'application/vnd.aai.asyncapi;version=2.6.0',
'application/vnd.aai.asyncapi+json;version=2.6.0',
'application/vnd.aai.asyncapi+yaml;version=2.6.0',
]
Dereference strategy for dereferencing AsyncApi 3.x.y definitions.
Supported media types:
[
'application/vnd.aai.asyncapi;version=3.0.0',
'application/vnd.aai.asyncapi+json;version=3.0.0',
'application/vnd.aai.asyncapi+yaml;version=3.0.0',
'application/vnd.aai.asyncapi;version=3.1.0',
'application/vnd.aai.asyncapi+json;version=3.1.0',
'application/vnd.aai.asyncapi+yaml;version=3.1.0',
]
Dereference strategy for dereferencing OpenApi 2.0 definitions.
Supported media types:
[
'application/vnd.oai.openapi;version=2.0',
'application/vnd.oai.openapi+json;version=2.0',
'application/vnd.oai.openapi+yaml;version=2.0',
]
Dereference strategy for dereferencing OpenApi 3.0.x definitions.
Supported media types:
[
'application/vnd.oai.openapi;version=3.0.0',
'application/vnd.oai.openapi+json;version=3.0.0',
'application/vnd.oai.openapi+yaml;version=3.0.0',
'application/vnd.oai.openapi;version=3.0.1',
'application/vnd.oai.openapi+json;version=3.0.1',
'application/vnd.oai.openapi+yaml;version=3.0.1',
'application/vnd.oai.openapi;version=3.0.2',
'application/vnd.oai.openapi+json;version=3.0.2',
'application/vnd.oai.openapi+yaml;version=3.0.2',
'application/vnd.oai.openapi;version=3.0.3',
'application/vnd.oai.openapi+json;version=3.0.3',
'application/vnd.oai.openapi+yaml;version=3.0.3',
'application/vnd.oai.openapi;version=3.0.4',
'application/vnd.oai.openapi+json;version=3.0.4',
'application/vnd.oai.openapi+yaml;version=3.0.4',
]
© 2010 - cnpmjs.org x YWFE | Home | YWFE