From f9f9222c52fb6870fd702b38c41aef682075b1f0 Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Tue, 27 Dec 2022 16:03:51 +0100 Subject: [PATCH] feat(resolve): add ApiDOM YAML 1.2 parser Refs #2717 --- .../apidom/openapi-3-1/parsers/json/index.js | 48 ----------------- .../reference/parse/parsers/json/index.js | 50 ++++++++++++++++++ .../reference/parse/parsers/yaml-1-2/index.js | 52 +++++++++++++++++++ 3 files changed, 102 insertions(+), 48 deletions(-) delete mode 100644 src/helpers/apidom/openapi-3-1/parsers/json/index.js create mode 100644 src/helpers/apidom/reference/parse/parsers/json/index.js create mode 100644 src/helpers/apidom/reference/parse/parsers/yaml-1-2/index.js diff --git a/src/helpers/apidom/openapi-3-1/parsers/json/index.js b/src/helpers/apidom/openapi-3-1/parsers/json/index.js deleted file mode 100644 index 5508ed841..000000000 --- a/src/helpers/apidom/openapi-3-1/parsers/json/index.js +++ /dev/null @@ -1,48 +0,0 @@ -import { from, ParseResultElement } from '@swagger-api/apidom-core'; -import { ParserError } from '@swagger-api/apidom-reference/configuration/empty'; - -const JsonParser = { - name: 'json-swagger-client', - fileExtensions: [], - mediaTypes: ['application/json'], - - 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 (e) { - 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; diff --git a/src/helpers/apidom/reference/parse/parsers/json/index.js b/src/helpers/apidom/reference/parse/parsers/json/index.js new file mode 100644 index 000000000..15d1277f6 --- /dev/null +++ b/src/helpers/apidom/reference/parse/parsers/json/index.js @@ -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; diff --git a/src/helpers/apidom/reference/parse/parsers/yaml-1-2/index.js b/src/helpers/apidom/reference/parse/parsers/yaml-1-2/index.js new file mode 100644 index 000000000..5b4b3495d --- /dev/null +++ b/src/helpers/apidom/reference/parse/parsers/yaml-1-2/index.js @@ -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;