From 98c86a703e8fb2bc365886fb48a40ce707be7a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Gorej?= Date: Tue, 27 Dec 2022 15:15:41 +0100 Subject: [PATCH] feat(resolve): add ApiDOM JSON parser (#2739) Refs #2717 Refs #2718 --- .eslintrc | 6 +++ package.json | 3 +- .../apidom/openapi-3-1/parsers/json/index.js | 48 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/helpers/apidom/openapi-3-1/parsers/json/index.js diff --git a/.eslintrc b/.eslintrc index 23d87b56ae..74727673f4 100644 --- a/.eslintrc +++ b/.eslintrc @@ -32,6 +32,12 @@ "ignorePackages": true } ], + "import/no-unresolved": [ + 2, + { + "ignore": ["^@swagger-api/apidom-reference/configuration/empty$"] + } + ], "prettier/prettier": "error", "no-param-reassign": 0, // needs to be eliminated in future "no-use-before-define": [2, "nofunc"] // needs to be eliminated in future diff --git a/package.json b/package.json index 62959cd0c9..f194ca51f9 100644 --- a/package.json +++ b/package.json @@ -110,7 +110,8 @@ }, "dependencies": { "@babel/runtime-corejs3": "^7.11.2", - "@swagger-api/apidom-reference": "^0.58.0", + "@swagger-api/apidom-core": "^0.59.0", + "@swagger-api/apidom-reference": "^0.59.0", "cookie": "~0.5.0", "cross-fetch": "^3.1.5", "deepmerge": "~4.2.2", diff --git a/src/helpers/apidom/openapi-3-1/parsers/json/index.js b/src/helpers/apidom/openapi-3-1/parsers/json/index.js new file mode 100644 index 0000000000..5508ed841b --- /dev/null +++ b/src/helpers/apidom/openapi-3-1/parsers/json/index.js @@ -0,0 +1,48 @@ +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;