Skip to content

Commit

Permalink
feat(reference): add OpenAPI 2.0 YAML parser plugin
Browse files Browse the repository at this point in the history
Refs #3100
  • Loading branch information
char0n committed Oct 31, 2023
1 parent 70f345d commit 1a1de72
Show file tree
Hide file tree
Showing 8 changed files with 1,016 additions and 5 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

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

19 changes: 19 additions & 0 deletions packages/apidom-reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ Supported media types are:
]
```

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

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

Supported media types are:

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

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

Wraps [@swagger-api/apidom-parser-adapter-openapi-yaml-3-0](https://github.com/swagger-api/apidom/tree/main/packages/apidom-parser-adapter-openapi-yaml-3-0) package
Expand Down Expand Up @@ -315,6 +329,7 @@ returns `true` or until entire list of parser plugins is exhausted (throws error
```js
[
OpenApiJson2Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml2Parser({ allowEmpty: true, sourceMap: false }),
OpenApiJson3_0Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml3_0Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml3_1Parser({ allowEmpty: true, sourceMap: false }),
Expand All @@ -336,6 +351,7 @@ It's possible to **change** the parser plugins **order globally** by mutating gl
```js
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';
Expand All @@ -351,6 +367,7 @@ import BinaryParser from '@swagger-api/apidom-reference/parse/parsers/binary';

options.parse.parsers = [
OpenApiJson2Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml2Parser({ allowEmpty: true, sourceMap: false }),
OpenApiJson3_0Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml3_0Parser({ allowEmpty: true, sourceMap: false }),
OpenApiJson3_1Parser({ allowEmpty: true, sourceMap: false }),
Expand All @@ -370,6 +387,7 @@ To **change** the parser plugins **order** on ad-hoc basis:
```js
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';
Expand All @@ -387,6 +405,7 @@ await parse('/home/user/oas.json', {
mediaType: 'application/vnd.oai.openapi+json;version=3.1.0',
parsers: [
OpenApiJson2Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml2Parser({ allowEmpty: true, sourceMap: false }),
OpenApiJson3_1Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml3_1Parser({ allowEmpty: true, sourceMap: false }),
OpenApiJson3_0Parser({ allowEmpty: true, sourceMap: false }),
Expand Down
10 changes: 10 additions & 0 deletions packages/apidom-reference/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@
"require": "./cjs/parse/parsers/openapi-json-2/index.cjs",
"types": "./types/parse/parsers/openapi-json-2/index.d.ts"
},
"./parse/parsers/openapi-yaml-2": {
"import": "./es/parse/parsers/openapi-yaml-2/index.mjs",
"require": "./cjs/parse/parsers/openapi-yaml-2/index.cjs",
"types": "./types/parse/parsers/openapi-yaml-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 @@ -230,22 +235,27 @@
"@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-yaml-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",
"@swagger-api/apidom-parser-adapter-openapi-yaml-3-1": "^0.81.0",
"@swagger-api/apidom-parser-adapter-yaml-1-2": "^0.81.0"
},
"devDependencies": {
"@swagger-api/apidom-error": "*",
"@swagger-api/apidom-json-pointer": "*",
"@swagger-api/apidom-ns-asyncapi-2": "*",
"@swagger-api/apidom-ns-openapi-2": "*",
"@swagger-api/apidom-ns-openapi-3-0": "*",
"@swagger-api/apidom-ns-openapi-3-1": "*",
"@swagger-api/apidom-parser-adapter-api-design-systems-json": "*",
"@swagger-api/apidom-parser-adapter-api-design-systems-yaml": "*",
"@swagger-api/apidom-parser-adapter-asyncapi-json-2": "*",
"@swagger-api/apidom-parser-adapter-asyncapi-yaml-2": "*",
"@swagger-api/apidom-parser-adapter-json": "*",
"@swagger-api/apidom-parser-adapter-openapi-json-2": "*",
"@swagger-api/apidom-parser-adapter-openapi-yaml-2": "*",
"@swagger-api/apidom-parser-adapter-openapi-json-3-0": "*",
"@swagger-api/apidom-parser-adapter-openapi-json-3-1": "*",
"@swagger-api/apidom-parser-adapter-openapi-yaml-3-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 @@ -6,6 +6,7 @@ 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 OpenApiYaml2Parser from '../parse/parsers/openapi-yaml-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 @@ -22,6 +23,7 @@ import { options } from '../index';

options.parse.parsers = [
OpenApiJson2Parser({ allowEmpty: true, sourceMap: false }),
OpenApiYaml2Parser({ 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,42 @@
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-yaml-2';

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

const OpenApiYaml2Parser: stampit.Stamp<IParser> = stampit(Parser, {
props: {
name: 'openapi-yaml-2',
fileExtensions: ['.yaml', '.yml'],
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', 'refractorOpts'], this);
return await parse(source, parserOpts);
} catch (error: any) {
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
},
},
});

export default OpenApiYaml2Parser;
Loading

0 comments on commit 1a1de72

Please sign in to comment.