Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(reference): add OpenAPI 2.0 JSON parser plugin #3343

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 42 additions & 23 deletions packages/apidom-reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This package has two main exports suitable for different use-cases. **Empty** co

```js
import { parse } from '@swagger-api/apidom-reference/configuration/empty';
import { OpenApiJson3_1Parser } from '@swagger-api/apidom-reference/parse/parsers/openapi-json-3-1';
import OpenApiJson3_1Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-3-1';

await parse('/home/user/oas.json', {
parse: {
Expand Down Expand Up @@ -100,6 +100,20 @@ so providing it is always a better option.

Parse component comes with number of default parser plugins.

#### [openapi-json-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/openapi-json-2)

Wraps [@swagger-api/apidom-parser-adapter-openapi-json-2](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-openapi-json-2) package
and is uniquely identified by `openapi-json-2` name.

Supported media types are:

```js
[
'application/vnd.oai.openapi;version=2.0',
'application/vnd.oai.openapi+json;version=2.0',
]
```

#### [openapi-json-3-0](https://github.com/swagger-api/apidom/tree/main/packages/apidom-reference/src/parse/parsers/openapi-json-3-0)

Wraps [@swagger-api/apidom-parser-adapter-openapi-json-3-0](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-openapi-json-3-0) package
Expand Down Expand Up @@ -300,6 +314,7 @@ returns `true` or until entire list of parser plugins is exhausted (throws error

```js
[
OpenApiJson2Parser({ allowEmpty: true, sourceMap: false }),
OpenApiJson3_0Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml3_0Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml3_1Parser({ allowEmpty: true, sourceMap: false }),
Expand All @@ -320,20 +335,22 @@ It's possible to **change** the parser plugins **order globally** by mutating gl

```js
import { options } from '@swagger-api/apidom-reference';
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 { AsyncApiYaml2Parser } from '@swagger-api/apidom-reference/parse/parsers/asyncapi-yaml-2';
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';
import OpenApiJson2Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-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 AsyncApiYaml2Parser from '@swagger-api/apidom-reference/parse/parsers/asyncapi-yaml-2';
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 = [
OpenApiJson2Parser({ allowEmpty: true, sourceMap: false }),
OpenApiJson3_0Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml3_0Parser({ allowEmpty: true, sourceMap: false }),
OpenApiJson3_1Parser({ allowEmpty: true, sourceMap: false }),
Expand All @@ -352,22 +369,24 @@ To **change** the parser plugins **order** on ad-hoc basis:

```js
import { parse } from '@swagger-api/apidom-reference';
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 { AsyncApiYaml2Parser } from '@swagger-api/apidom-reference/parse/parsers/asyncapi-yaml-2';
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';
import OpenApiJson2Parser from '@swagger-api/apidom-reference/parse/parsers/openapi-json-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 AsyncApiYaml2Parser from '@swagger-api/apidom-reference/parse/parsers/asyncapi-yaml-2';
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: [
OpenApiJson2Parser({ allowEmpty: true, sourceMap: false }),
OpenApiJson3_1Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml3_1Parser({ allowEmpty: true, sourceMap: false }),
OpenApiJson3_0Parser({ allowEmpty: true, sourceMap: false }),
Expand Down
7 changes: 7 additions & 0 deletions packages/apidom-reference/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
"require": "./cjs/parse/parsers/json/index.cjs",
"types": "./types/parse/parsers/json/index.d.ts"
},
"./parse/parsers/openapi-json-2": {
"import": "./es/parse/parsers/openapi-json-2/index.mjs",
"require": "./cjs/parse/parsers/openapi-json-2/index.cjs",
"types": "./types/parse/parsers/openapi-json-2/index.d.ts"
},
"./parse/parsers/openapi-json-3-0": {
"import": "./es/parse/parsers/openapi-json-3-0/index.mjs",
"require": "./cjs/parse/parsers/openapi-json-3-0/index.cjs",
Expand Down Expand Up @@ -216,13 +221,15 @@
"@swagger-api/apidom-error": "^0.81.0",
"@swagger-api/apidom-json-pointer": "^0.81.0",
"@swagger-api/apidom-ns-asyncapi-2": "^0.81.0",
"@swagger-api/apidom-ns-openapi-2": "^0.81.0",
"@swagger-api/apidom-ns-openapi-3-0": "^0.81.0",
"@swagger-api/apidom-ns-openapi-3-1": "^0.81.0",
"@swagger-api/apidom-parser-adapter-api-design-systems-json": "^0.81.0",
"@swagger-api/apidom-parser-adapter-api-design-systems-yaml": "^0.81.0",
"@swagger-api/apidom-parser-adapter-asyncapi-json-2": "^0.81.0",
"@swagger-api/apidom-parser-adapter-asyncapi-yaml-2": "^0.81.0",
"@swagger-api/apidom-parser-adapter-json": "^0.81.0",
"@swagger-api/apidom-parser-adapter-openapi-json-2": "^0.81.0",
"@swagger-api/apidom-parser-adapter-openapi-json-3-0": "^0.81.0",
"@swagger-api/apidom-parser-adapter-openapi-json-3-1": "^0.81.0",
"@swagger-api/apidom-parser-adapter-openapi-yaml-3-0": "^0.81.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/apidom-reference/src/configuration/saturated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import OpenApi3_1ResolveStrategy from '../resolve/strategies/openapi-3-1';
import AsyncApi2ResolveStrategy from '../resolve/strategies/asyncapi-2';
import ApiDesignSystemsJsonParser from '../parse/parsers/api-design-systems-json';
import ApiDesignSystemsYamlParser from '../parse/parsers/api-design-systems-yaml';
import OpenApiJson2Parser from '../parse/parsers/openapi-json-2';
import OpenApiJson3_0Parser from '../parse/parsers/openapi-json-3-0';
import OpenApiYaml3_0Parser from '../parse/parsers/openapi-yaml-3-0';
import OpenApiJson3_1Parser from '../parse/parsers/openapi-json-3-1';
Expand All @@ -20,6 +21,7 @@ import AsyncApi2DereferenceStrategy from '../dereference/strategies/asyncapi-2';
import { options } from '../index';

options.parse.parsers = [
OpenApiJson2Parser({ allowEmpty: true, sourceMap: false }),
OpenApiJson3_0Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml3_0Parser({ allowEmpty: true, sourceMap: false }),
OpenApiJson3_1Parser({ allowEmpty: true, sourceMap: false }),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import stampit from 'stampit';
import { pick } from 'ramda';
import { ParseResultElement } from '@swagger-api/apidom-core';
import { parse, mediaTypes, detect } from '@swagger-api/apidom-parser-adapter-openapi-json-2';

import ParserError from '../../../errors/ParserError';
import { File as IFile, Parser as IParser } from '../../../types';
import Parser from '../Parser';

// eslint-disable-next-line @typescript-eslint/naming-convention
char0n marked this conversation as resolved.
Show resolved Hide resolved
const OpenApiJson2Parser: stampit.Stamp<IParser> = stampit(Parser, {
props: {
name: 'openapi-json-2',
fileExtensions: ['.json'],
mediaTypes,
},
methods: {
async canParse(file: IFile): Promise<boolean> {
const hasSupportedFileExtension =
this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension);
const hasSupportedMediaType = this.mediaTypes.includes(file.mediaType);

if (!hasSupportedFileExtension) return false;
if (hasSupportedMediaType) return true;
if (!hasSupportedMediaType) {
return detect(file.toString());
}
return false;
},
async parse(file: IFile): Promise<ParseResultElement> {
const source = file.toString();

try {
const parserOpts = pick(['sourceMap', 'syntacticAnalysis', 'refractorOpts'], this);
return await parse(source, parserOpts);
} catch (error: any) {
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
},
},
});

export default OpenApiJson2Parser;
Loading