Skip to content

Commit

Permalink
feat(resolve): add ApiDOM JSON parser (#2739)
Browse files Browse the repository at this point in the history
Refs #2717
Refs #2718
  • Loading branch information
char0n committed Jan 5, 2023
1 parent 191b5e2 commit ad33c13
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 48 additions & 0 deletions src/helpers/apidom/openapi-3-1/parsers/json/index.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit ad33c13

Please sign in to comment.