From a4e6e52768ed75deecb66aee0b09df8c6b7563d6 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 23d87b56a..74727673f 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 eb36905eb..fb6f7a0ba 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 000000000..5508ed841 --- /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;