-
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 3.1.x YAML reference parser
- Loading branch information
Showing
17 changed files
with
371 additions
and
64 deletions.
There are no files selected for viewing
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
8 changes: 2 additions & 6 deletions
8
...om/packages/apidom-parser-adapter-openapi-json-3-1/src/parser/visitors/DocumentVisitor.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
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
4 changes: 4 additions & 0 deletions
4
apidom/packages/apidom-parser-adapter-openapi-yaml-3-1/src/adapter-browser.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 |
---|---|---|
@@ -1,2 +1,6 @@ | ||
export { default as parse, namespace } from './parser/index-browser'; | ||
export { detect, mediaTypes } from './adapter'; | ||
|
||
export { default as specification } from './parser/specification'; | ||
|
||
export { default as DocumentVisitor } from './parser/visitors/DocumentVisitor'; |
4 changes: 4 additions & 0 deletions
4
apidom/packages/apidom-parser-adapter-openapi-yaml-3-1/src/adapter-node.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 |
---|---|---|
@@ -1,2 +1,6 @@ | ||
export { default as parse, namespace } from './parser/index-node'; | ||
export { mediaTypes, detect } from './adapter'; | ||
|
||
export { default as specification } from './parser/specification'; | ||
|
||
export { default as DocumentVisitor } from './parser/visitors/DocumentVisitor'; |
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
21 changes: 21 additions & 0 deletions
21
apidom/packages/apidom-parser-adapter-yaml-1-2/src/parser/visitors/CommentVisitor.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,21 @@ | ||
import stampit from 'stampit'; | ||
import { YamlComment } from 'apidom-ast'; | ||
|
||
import { BREAK } from './index'; | ||
import SpecificationVisitor from './SpecificationVisitor'; | ||
|
||
const CommentVisitor = stampit(SpecificationVisitor, { | ||
init() { | ||
this.element = new this.namespace.elements.Comment(); | ||
}, | ||
methods: { | ||
comment(commentNode: YamlComment) { | ||
this.element.content = commentNode.content; | ||
this.maybeAddSourceMap(commentNode, this.element); | ||
|
||
return BREAK; | ||
}, | ||
}, | ||
}); | ||
|
||
export default CommentVisitor; |
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
66 changes: 66 additions & 0 deletions
66
...m/packages/apidom-reference/src/parsers/apidom-reference-parser-openapi-yaml-3-1/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,66 @@ | ||
import stampit from 'stampit'; | ||
import { assocPath, always } from 'ramda'; | ||
import { ParseResultElement } from 'apidom'; | ||
|
||
// @ts-ignore | ||
import { parse, specification } from 'apidom-parser-adapter-openapi-yaml-3-1'; | ||
|
||
import File from '../../util/File'; | ||
import { ParserError } from '../../util/errors'; | ||
import { documentVisitorFactory } from './visitors/DocumentVisitor'; | ||
|
||
interface OpenApiYaml3_1Parser { | ||
allowEmpty: boolean; | ||
sourceMap: boolean; | ||
specPath: string; | ||
|
||
canParse(file: File): boolean; | ||
parse(file: File): Promise<ParseResultElement>; | ||
} | ||
|
||
const OpenApiYaml3_1Parser: stampit.Stamp<OpenApiYaml3_1Parser> = stampit({ | ||
props: { | ||
/** | ||
* Whether to allow "empty" files. This includes zero-byte files. | ||
*/ | ||
allowEmpty: true, | ||
|
||
/** | ||
* Whether to generate source map during parsing. | ||
*/ | ||
sourceMap: true, | ||
|
||
/** | ||
* Path of the Specification object where visitor is located. | ||
*/ | ||
specPath: always(['kind']), | ||
}, | ||
init( | ||
this: OpenApiYaml3_1Parser, | ||
{ allowEmpty = this.allowEmpty, sourceMap = this.sourceMap, specPath = this.specPath } = {}, | ||
) { | ||
this.allowEmpty = allowEmpty; | ||
this.sourceMap = sourceMap; | ||
this.specPath = specPath; | ||
}, | ||
methods: { | ||
canParse(file: File): boolean { | ||
return ['.yaml', '.yml'].includes(file.extension); | ||
}, | ||
async parse(file: File): Promise<ParseResultElement> { | ||
const specObj = assocPath( | ||
['visitors', 'document', '$visitor'], | ||
documentVisitorFactory(this.specPath), | ||
specification, | ||
); | ||
|
||
try { | ||
return await parse(file.data, { sourceMap: this.sourceMap, specObj }); | ||
} catch (e) { | ||
throw new ParserError(`Error parsing "${file.url}"`, e); | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
export default OpenApiYaml3_1Parser; |
Oops, something went wrong.