-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reference): add OpenAPI 2.0 JSON parser plugin
Refs #3100
- Loading branch information
Showing
8 changed files
with
1,333 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/apidom-reference/src/parse/parsers/openapi-json-2/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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; |
Oops, something went wrong.