-
Notifications
You must be signed in to change notification settings - Fork 761
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(resolve): add ApiDOM YAML 1.2 parser
Refs #2717
- Loading branch information
Showing
3 changed files
with
102 additions
and
48 deletions.
There are no files selected for viewing
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { from, ParseResultElement } from '@swagger-api/apidom-core'; | ||
import { ParserError, Parser } from '@swagger-api/apidom-reference/configuration/empty'; | ||
|
||
const JsonParser = Parser.compose(Parser, { | ||
props: { | ||
name: 'json-swagger-client', | ||
fileExtensions: ['.json'], | ||
mediaTypes: ['application/json'], | ||
}, | ||
methods: { | ||
async canParse(file) { | ||
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) { | ||
try { | ||
JSON.parse(file.toString()); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
return false; | ||
}, | ||
|
||
async parse(file) { | ||
if (this.sourceMap) { | ||
// eslint-disable-next-line no-console | ||
console.warn("json-swagger-client parser plugin doesn't support sourceMaps option"); | ||
} | ||
|
||
const source = file.toString(); | ||
|
||
try { | ||
const element = from(JSON.parse(source)); | ||
const parseResultElement = new ParseResultElement(); | ||
|
||
element.classes.push('result'); | ||
parseResultElement.push(element); | ||
return parseResultElement; | ||
} catch (error) { | ||
throw new ParserError(`Error parsing "${file.uri}"`, error); | ||
} | ||
}, | ||
}, | ||
}); | ||
export default JsonParser; |
52 changes: 52 additions & 0 deletions
52
src/helpers/apidom/reference/parse/parsers/yaml-1-2/index.js
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,52 @@ | ||
import YAML, { JSON_SCHEMA } from 'js-yaml'; | ||
import { from, ParseResultElement } from '@swagger-api/apidom-core'; | ||
import { ParserError, Parser } from '@swagger-api/apidom-reference/configuration/empty'; | ||
|
||
const YamlParser = Parser.compose(Parser, { | ||
props: { | ||
name: 'yaml-1-2-swagger-client', | ||
fileExtensions: ['.yaml', '.yml'], | ||
mediaTypes: ['text/yaml', 'application/yaml'], | ||
}, | ||
methods: { | ||
async canParse(file) { | ||
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) { | ||
try { | ||
YAML.load(file.toString(), { schema: JSON_SCHEMA }); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
return false; | ||
}, | ||
|
||
async parse(file) { | ||
if (this.sourceMap) { | ||
// eslint-disable-next-line no-console | ||
console.warn("yaml-1-2-swagger-client parser plugin doesn't support sourceMaps option"); | ||
} | ||
|
||
const source = file.toString(); | ||
|
||
try { | ||
const element = from(YAML.load(source, { schema: JSON_SCHEMA })); | ||
const parseResultElement = new ParseResultElement(); | ||
|
||
element.classes.push('result'); | ||
parseResultElement.push(element); | ||
return parseResultElement; | ||
} catch (error) { | ||
throw new ParserError(`Error parsing "${file.uri}"`, error); | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
export default YamlParser; |